2011-08-05 89 views
0

如何防止我的用户提交空表单? 当我这样做时,IE浏览器中出现HTTP 404未找到错误,Firefox中出现空白页。 没有任何验证错误出现。cakephp:提交空表单提供HTTP 404页面未找到错误

我甚至试着在StudentsController的add函数中放入一个其他的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) 
     ); 

} >

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有什么网址?因为在默认的Cake配置中,你只会缺少控制器或缺少动作,但不会有404。你不需要发布模型,但是发布你拥有的路线。

+0

你是什么意思的“正确结构中的数据”?顺便说一句,没有任何验证错误发射。 – vaanipala

+0

是的,我确实有模型的验证。现在我已将它们包含在我的代码中。请看一下。 – vaanipala

+0

你甚至没有复制我的代码... –

0

您是否看了此帖子? CakePHP route URL not found!

如果添加

function beforeFilter() { 
    $this->Security->validatePost = false; 
} 

到控制器应当一切正常。

+0

你可以在http://blog.endpoint.com/2009/12/using-security-component-and.html上找到更多信息 – Hopiu

+0

非常感谢。你的解决方案确实锻炼了。现在所有的验证错误都在触发,并且我没有再收到http 404错误。我也检查了你给出的链接。这是否意味着,动态修改在POST请求中提交的字段(例如,通过JavaScript禁用,删除或创建新字段)不会触发对请求的黑洞?因为'validatePost被设置为false。尽管我并没有真正理解blog.endpoint.com链接。 – vaanipala

+0

是的,因为“validatePost”设置为false,所以黑洞和404重定向不再发生。我不确定如果你通过这种方法失去安全限制。我没有测试这些东西。可能你可以在你的表单中手动设置安全令牌,并打开“validatePost”。 – Hopiu