The following is my complete code with validation:
students_controller.php
<?php
class StudentsController extends AppController{
var $name='Students';
var $helpers = array('Html','Form','Ajax','Javascript');
var $components=array('Security','RequestHandler');
function beforeFilter()//executed before any controller action
logic
{
//parent::beforeFilter();
if(isset($this->Security) && $this->RequestHandler->isAjax() &&
$this->action = 'getcities'){
$this->Security->enabled = false;
}
}
if (!empty($this->data)){
die(debug($this->Student->validationErrors));
$student=$this->Student->saveAll($this->data);
if (!empty($student))
{$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{
$this->Session->setFlash(__('Your admission could not be saved.
Please, try again.', true));
}
} //for if (!empty....
$states=$this->Student->MerryParent->State->find('list');
$this->set('states',$states);
$cities=array();
//$this->set(compact('states','cities'));
}//end function
}
?>
Following are my models with validation:
student.php
<?php
class Student extends AppModel{
var $name='Student';
var $belongsTo=array('MerryParent','MerryClass','State','City');
var $validate=array(
'name'=>array(
'rule'=>array('minLength',3),
'required'=>true,
'allowEmpty'=>false,
'message'=>'Name is required!'
),
'dob'=>array(
'rule'=>'date',
'required'=>true,
'allowEmpty'=>false,
'message'=>'Enter a valid birth date!'
),
'class_id'=>array(
'rule'=>'notEmpty',
'message'=>'Which class are you
enquiring about?'
)
//'city_id'=>array('rule'=>'notEmpty','message'=>'Please select
your city','required'=>true, 'allowEmpty'=>false)
);
}
?>
merry_parent.php
<?php
class MerryParent extends AppModel{
var $name='MerryParent';
var $hasMany=array(
'Student'=>array(
'className'=>'Student',
'foreignKey'=>'merry_parent_id'
)
);
var $belongsTo=array('State','City','MerryClass');
var $validate=array(
'initial'=>array(
'rule'=>'notEmpty',
'message'=>'Please select your initial'
),
'name'=>array(
'rule'=>array('minLength',3),
'required'=>true,
'allowEmpty'=>false,
'message'=>'Name is required!'
),
'email'=>array(
'rule'=>'email',
'required'=>true,
'allowEmpty'=>false,
'message'=>'Valid email address
required!'
),
'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.'
),
'state_id'=>array(
'rule'=>'notEmpty',
//'required'=>true,
'message'=>'Please select your state'
),
'postal_code'=>array(
'rule'=>array('numeric',6),
'required'=>true,
'allowEmpty'=>false,
'message'=>'valid postal code
required!'
)
);//closing bracket for $validate array
}
?>
thank you.
On Aug 5, 11:10 am, varai <[email protected]> wrote:
> By the way, I also have a beforeFilter in my StudentsController:
>
> function beforeFilter()//executed before any controller action logic
> {
> //parent::beforeFilter();
> if(isset($this->Security) && $this->RequestHandler->isAjax() &&
> $this->action = 'getcities'){
> $this->Security->enabled = false;
> }
>
> }
>
> but i don't think that is causing any prob coz' i already tried
> testing my webiste by commenting the beforeFilter.
>
> On Aug 4, 3:49 pm,varai<[email protected]> wrote:
>
>
>
> > Hi,
>
> > How do I prevent my user from submitting an empty form?
> > When I do that, I'm getting a HTTP 404 not found error in IE and a
> > blank page in Firefox.
> > None of the validation errors are showing up.
>
> > I even tried putting an else stmt in the StudentsController's add
> > function and that too is not firing. Nothing shows up for
> > die(debug($this->Student->validationErrors)) either:
>
> > [CODE]if (!empty($this->data)){
> > [COLOR="red"]die(debug($this->Student->validationErrors)); [/COLOR]
> > $student=$this->Student->saveAll($this->data);
>
> > if (!empty($student))
> > {
> > $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{
> > [COLOR="Red"]$this->Session->setFlash(__('Your admission could
> > not be saved. Please, try again.', true));[/COLOR]
> > }
> > } //for if (!empty....
> > [/CODE]
>
> > thank you.
--
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
[email protected] For more options, visit this group at
http://groups.google.com/group/cake-php