Monday, July 4, 2011

2 related models in 1 form but validation is working for only 1 model

Hi,

I'm having 2 related models in 1 form (add.ctp) but validation is
working only for 1 model. ie. validation is working fine for Student
model but not MerryParent model.

Can anyone tell me on what i'm doing wrong? thank you.

relationships:
Student hasOne MerryParent
MerryParent hasMany Student

student.php
<?php
class Student extends AppModel{
var $name='Student';
var $hasOne=array(
'MerryParent' => array(
'className' => 'MerryParent',
'foreignKey'=>'student_id',
'dependent' => true)
);
var $belongsTo='MerryClass';

var $validate=array(
'name'=>array('rule'=>array('minLength',1),'message'=>'Name is
required!'),
'dob'=>array('rule'=>'notEmpty','message'=>'Date of Birth is
required!'),
'class_id'=>array('rule'=>'notEmpty', 'message'=>'Which class are
you enquiring about?')
);

}
?>

students_controller.php
<?php
class StudentsController extends AppController{

function add(){
if (!empty($this->data)){
var_dump($this->data);
die(debug($this->Student->validationErrors));
if ($this->Student->saveAll($this->data))
{
$this->Session->setFlash('Your child\'s admission has been
received. We will send you an email shortly.');
$this->redirect(array('controller'=>'pages', 'action'=>'home'));

}

/* else
{ //die(debug($this->Student->validationErrors));

$this->Session->setFlash('Your child\'s admission failed to
save.');
//$this->redirect(array('controller'=>'pages',
'action'=>'home'));

}*/
} //for if (!empty....
}//end function
}
?>

merryparent.php
<?php
class MerryParent extends AppModel{
var $name='MerryParent';
var $hasMany=array(
'Student'=>array(
'className'=>'Student',
'foreignKey'=>'parent_id'
)
);
var $belongsTo='State';
var
$validate=array('initial'=>array('rule'=>'notEmpty','message'=>'Please
select your initial'),
'name'=>array('rule'=>array('minLength',1),'message'=>'Name is
required!'),
'email'=>array('rule'=>'email','message'=>'Valid email address
required!','required'=>true, 'allowEmpty'=>false),
'landline'=>array('rule'=>array('custom','/(0[0-9]{2,4}-[2-9][0-9]
{5,7})/'), 'required'=>false, 'allowEmpty'=>true, 'message'=>'Invalid
phone number! phone number format: eg 020-22345678 OR 0544-7573758 OR
02345-874567 '),
'mobile'=>array('rule'=>array('custom','/([89]{1}[0-9]{9})/'),
'required'=>true, 'allowEmpty'=>false, 'message'=>'Invalid mobile
number! mobile number format: eg 9876543211'),
'address'=>array('rule'=>array('alphaNumeric',array('minLength',
1),'required'=>true, 'allowEmpty'=>false, 'message'=>'Please enter
your address.'),
'city'=>array('rule'=>'notEmpty','message'=>'Please select your
city','required'=>true, 'allowEmpty'=>false),
'state'=>array('rule'=>'notEmpty','message'=>'Please select your
state','required'=>true, 'allowEmpty'=>false),
'postal_code'=>array('rule'=>array('numeric',6),'message'=>'valid
postal code required!','required'=>true, 'allowEmpty'=>false)
?>

merryparents_controller.php
<?php
class MerryParentsController extends AppController{

var $name='MerryParents';

function index(){
var_dump($this->data);
die(debug($this->MerryParent->ValidationErrors));
}

}
?>

add.ctp
<?php
echo $form->create('Student', array('action'=>'add'));
echo '<fieldset>';
echo '<legend>Student Information</legend>';
echo $form->input('Student.name');

$options = array('Male'=>'Male','Female'=>'Female');
$attributes = array('value'=>'Male');
echo $form->radio('Student.gender',$options,$attributes);

echo $form->input('Student.dob', array('label'=>'Date of Birth',
'dateFormat'=>'DMY',
'empty'=>true,
'timeFormat' => '',
'minYear' => (
date('Y') - 5
),
'maxYear' => (
date('Y') - 2
)
));
echo $form->input('Student.class_id',
array(
'label'=>'Enquiry Class for',
'empty'=>true,
'options'=>array('1'=>'Playgroup','2'=>'Nursery','3'=>'LKG',
'4'=>'UKG')
)
);
echo '</fieldset>';

echo '<fieldset>';
echo '<legend>Parent Information</legend>';
/* echo $form->input('MerryParent.id', array('type'=>'hidden'));
*/ echo $form->input('MerryParent.initial',
array('empty'=>true,
'options'=>array('Dr'=>'Dr',
'Mr'=>'Mr',
'Mrs'=>'Mrs',
'Ms'=>'Ms')
)
);
echo $form->input('MerryParent.name', array('label'=>'Parent/Guardian
Name'));
echo $form->input('MerryParent.email');
echo $form->input('MerryParent.landline');
echo $form->input('MerryParent.mobile');
echo $form->input('MerryParent.address');
echo $form->input('MerryParent.city');
echo $form->input('MerryParent.state', array('empty'=>true));
echo $form->input('MerryParent.postal_code');
echo '</fieldset>';
//pr ($this->validationErrors);
echo $form->end('Submit');
?>

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: