Hi I have been trying to access the validation rules of an associated model from my main controller. It doesn't seem to be working and I haven't been able to find any reference about this. Here is what I am trying to do:
User Controller snip:
$this->User->TalentProfile->set( $this->data );
if ($this->User->TalentProfile->validates()) {
//it always validates and doesn't seem to see model's validation rules
}
TalentProfile Model snip:
var $validate = array (
'first_name' => array(
'maxLength' => array(
'rule' => array('maxLength', 20),
'message' => 'Can not be longer than 20 characters.',
'last' => true
),
'first_name_not_empty' => array(
'rule' => 'notEmpty',
'message' => 'This field is required',
'last' => true
),
),
);
I have also tried rewrapping the array my thought being that the model name could be screwing with the validation:
$this->User->TalentProfile->set( array('TalentProfile',$this->data) ) );
I have also tried this hoping but still same results:
$this->loadModel('TalentProfile');
$this开发者_开发知识库->TalentProfile->set( $this->data) );
There is something I am missing. Please help! Thanks
EDIT:
I tried to save the form data which is giving me blanks in the SQL. I think this might be a simpler problem than I thought.
By setting the $validate array in the TalentProfile
model, you require data to be of the form:
Array(
[TalentProfile] => Array(
[first_name] =>
)
)
A little explanation on how this works:
In the call $this->User->TalentProfile->validates()
and $this->TalentProfile->validates()
you are using the validation methods in the TalentProfile
model. This means that CakePHP is going to validate your input data against the validation rules in that model, so it expects the TalentProfile
key to be set in the array (but dies down quietly if it's not).
In your $validates
array, you have set up validation rules for a key called first_name
. CakePHP takes this key as a field name that is being input from a form.
Right now, you are taking inputs as fname
instead of first_name
. Your inputs are also associated with the User
model, and not TalentProfile
.
If you explain further what you're trying to do with your form, I can be of more help.
精彩评论