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
TalentProfilemodel, you require data to be of the form: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 theTalentProfilemodel. This means that CakePHP is going to validate your input data against the validation rules in that model, so it expects theTalentProfilekey to be set in the array (but dies down quietly if it’s not).In your
$validatesarray, you have set up validation rules for a key calledfirst_name. CakePHP takes this key as a field name that is being input from a form.Right now, you are taking inputs as
fnameinstead offirst_name. Your inputs are also associated with theUsermodel, and notTalentProfile.If you explain further what you’re trying to do with your form, I can be of more help.