如何阻止我的用户提交空表单? 当我这样做时,我在IE中找到HTTP 404未找到错误,在Firefox中找到空白页面。 没有出现任何验证错误。
我甚至尝试在StudentsController的add函数中添加一个else stmt,而且也没有触发。什么都没有显示出来(调试($ this-> Student-> validationErrors))
我还在StudentsController中有一个beforeFilter。但我不认为这会引起任何疑问因为我已经尝试通过评论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
}
?>
以下是我的模型验证: 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)
);
} ?&GT;
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
}
?>
谢谢。
答案 0 :(得分:0)
if (!empty($this->data)){ 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)); } }
编辑:获得404后的网址是什么?因为在默认蛋糕配置中,您只会丢失控制器或缺少操作,但不会丢失404.您不需要发布模型,但会发布您拥有的路线。
答案 1 :(得分:0)
你读过这篇文章了吗? CakePHP route URL not found!
如果你添加
function beforeFilter() {
$this->Security->validatePost = false;
}
到你的控制器一切都应该正常。