cakephp:提交空表单给出 HTTP 404 页面未找到错误

发布于 2024-11-28 07:24:58 字数 5382 浏览 0 评论 0原文

如何防止我的用户提交空表单? 当我这样做时,我在 IE 中收到 HTTP 404 未找到错误,在 Firefox 中收到空白页面。 没有显示任何验证错误。

我什至尝试将 else stmt 放入 StudentsController 的 add 函数中,但这也没有触发。 die(debug($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
  }
  ?>

以下是我的经过验证的模型: 学生.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
   }
  ?>    

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟─花易冷 2024-12-05 07:24:58
if (!empty($this->data)){
  if ($this->学生->saveAll($this->data)){
     $this->Session->setFlash('您孩子的录取通知书已收到。我们很快就会向您发送电子邮件。');
     $this->redirect(array('controller'=>'pages', 'action'=>'home'));
  }别的{
     $this->Session->setFlash(__('您的入场信息无法保存。请重试。', true));  
  }
}

编辑:当你收到 404 时,网址是什么?因为在默认的 cake 配置中,你只会遇到缺少控制器或缺少操作的情况,而不会出现 404 错误。你不需要发布模型,但需要发布你拥有的路线。

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));  
  }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文