2011-08-05 49 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