在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?

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
        )
    )
)