duankanyi6539 2018-10-17 16:39
浏览 37

在cakephp中,如何访问其他模型字段的自定义验证中的模型字段?

I have a model in cakephp 1.3. I want to set up a custom validation for my example_id field, which uses the value of example_type field. I only want to have the validation fail if example_type is a specific type (1, not 2 or 3).

Here is example validate variable:

var $validate = array(
    'example_type' => array(
        'notempty' => array(
            'rule' => array('notempty'),
            'message' => 'Example Type: This is a required field'
        ),
        'numeric' => array(
            'rule' => array('numeric'),
            'message' => 'Example Type: Please choose from the drop down.'
        ),
        'isSpecificType' => array(
            'rule' => array('isSpecificType')
        )
    ),
    'example_id' => array(
        'range' => array(
            'rule' => array('range', -1, 2147483648),
            'message' => 'Example ID: Enter number from 0 to 2147483647',
            'allowEmpty' => true
        ),
        'numeric' => array(
            'rule' => array('numeric'),
            'message' => 'Example ID: Numeric only.',
            'allowEmpty' => true
        ),
        'is_type' => array(
            'rule' => array('exampleNotEmpty', $specific_type_var),
            'message' => 'Example ID: You must have an example id if the example type is 1'
        )
    )
);

Then I was going to have two custom validation functions, isSpecificType and exampleNotEmpty. See the following:

function isSpecificType($check) {
    $specific_type_var = $check['example_type'];
    return true;
};

function exampleNotEmpty($check, $type) {
    if ($type === 1) {
        if (is_null($check['example_id'])) {
            return false; //by the way, i realize is_null may not be correct here, but I have the capability to correct this after I am able to run it a few times
        }
    }
    return true;
};

Finally, at the top of my AppModel, just before the $validate declaration, I made the variable $specific_type_var. See the following:

var $specific_type_var = 1;

So, now, my issue. I get an error on this line:

'rule' => array('exampleNotEmpty', $specific_type_var),

The error is: Parse error: syntax error, unexpected '$specific_type_var' (T_VARIABLE), expecting ')' in /cakephpproject/app/models/example.php on line ##

My final questions in this post are: am I going about this the right way? Do I just have a syntax error? Alternatively, can I access the example_type field from directly inside the custom validation function that is being called by another field? Is what I'm doing even possible in the model, or do I have to do it at the controller layer?

  • 写回答

1条回答 默认 最新

  • dongzaocheng3214 2018-10-18 15:30
    关注

    I had a few problems. One was how do I use variables in a cakephp model. Second was how do I pass fields to other field's custom validators using these variables. Third was how do I pass null fields to custom validators.

    Answer to first question: Overriding the constructor allows you to add variables to your model.

    public function __construct($id = false, $table = null, $ds = null){
        parent::__construct($id, $table, $ds);
        $this->example_type_var = 1;
    }
    

    Answer to second question: Create a custom validator on your field that you need to pass, and use that function to set it. Note that (I think) there is an order to when rules happen. Make sure you put your custom rule as the first rule, and you put this field before the second field that you need to have access to this field's value.

    var $validate = array(
        'example_type' => array(
            'updateActionne' => array(
                'rule' => array('_updateTheOleType')
            )
        )
    )
    

    Then in the custom validator:

    function _updateTheOleType($check) {
        $this->example_type_var = $check['example_type'];
        return true;
    }
    

    In the second custom validator, use $this->example_type_var again. See the following:

    function _exampleNotEmpty($check) {
        if ($this->example_type_var === '3') {
            if (empty($check['example_id']) && $check['example_id'] !== '0') {
                return false;
            }
        }
        return true;
    }
    

    Answer to the third question: I was mistakenly thinking that allowEmpty is what I needed to pass an empty field to the custom validator. That is not how it works. AllowEmpty set to true means that null fields return true on the rule. Your custom validator will not even run. AllowEmpty set to false means that null fields will fail validation, again without ever going into the rule's custom validator function. The correct attribute is required. Setting required to true forces the rule to always run.

    Wrong:

    var $validate = array(
        'example_id' => array(
            'exampleNotEmpty' => array(
                'rule' => array('_exampleNotEmpty'),
                'message' => 'Example ID: You must have an example ID if the event is example type.',
                'allowEmpty' => true
            )
        )
    )
    

    Wrong:

    var $validate = array(
        'example_id' => array(
            'exampleNotEmpty' => array(
                'rule' => array('_exampleNotEmpty'),
                'message' => 'Example ID: You must have an example ID if the event is example type.',
                'allowEmpty' => false
            )
        )
    )
    

    Correct:

    var $validate = array(
        'example_id' => array(
            'exampleNotEmpty' => array(
                'rule' => array('_exampleNotEmpty'),
                'message' => 'Example ID: You must have an example ID if the event is example type.',
                'required' => true
            )
        )
    )
    
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么