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?