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
I also have a beforeFilter in StudentsController. But I don’t think that is causing any prob coz’ i have already tried running this website by commenting the beforeFilter.
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;
}
}
function add(){
if (!empty($this->data)){
//$student=$this->Student->saveAll($this->data,array('validate'=>'first'));
//var_dump($student);
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{
$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();
}//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'
),
/*'city_id'=>array('rule'=>'notEmpty','message'=>'Please select your city','required'=>true, 'allowEmpty'=>false),
'state_id'=>array('rule'=>'notEmpty','message'=>'Please select your state','required'=>true, 'allowEmpty'=>false),*/
'postal_code'=>array(
'rule'=>array('numeric',6),
'required'=>true,
'allowEmpty'=>false,
'message'=>'valid postal code required!'
)
);//closing bracket for $validate array
}
?>
thank you.
Edit: What’s the url when you get the 404? Because in default cake config, you’ll only get missing controller or missing action, but not 404. You don’t need to post the model, but do post the routes you have.