2014-05-06 43 views
0

我對錶單有一個表單和驗證。爲了保存數據,我使用了一個函數來使用ajax保存它。這是我的形式在Zend中保存之前確認驗證框架工作1.12

<form name="enquiry_form" method="post" id="enquiry_form"> 
     Full Name: <input name="name" id="name" type="text" pattern="[A-Za-z ]{1,20}" oninvalid="setCustomValidity('Plz enter only Alphabets ')" onchange="try{setCustomValidity('')}catch(e){}"> 
     Email: <input name="email" id="email" type="email" oninvalid="setCustomValidity('Plz enter valid email ')" onchange="try{setCustomValidity('')}catch(e){}" required > 
     Phone: <input name="mobile" id="mobile" type="text" pattern="[0-9]{10,12}" oninvalid="setCustomValidity('Plz enter valid Mobile Number ')" onchange="try{setCustomValidity('')}catch(e){}" required > 
     Query: <textarea name="query" id="query" class="" required></textarea></li> 
     <input type="submit" value="SUBMIT" id="enq_submit" onclick="getEnquiryForm(); ">       
      </form> 

這是我getEnquiryForm()函數

getEnquiryForm: function() 
{ 
     var url = window.location.protocol+'//'+window.location.host+'/'+path.base_path+'/ajax/save-enquiry'; //url path 
      new Ajax.Request(url, 
      { 
       parameters: $('enquiry_form').serialize(), 
       method:'POST', 
       onSuccess: function(transport) { 
        //alert(transport.responseText); 

       }, 
       onFailure: function(transport) { 
        alert('Could not connect to Propladder Server for this request'); 
       }, 
       onComplete: function(transport) { 
       } 
      }); 
}, 

然後我ajaxController中,我有saveEnquiry()行動在url上面提到的功能

public function saveEnquiryAction() 
{ 
    $data = array(); 
      $data['name'] = $this->_getParam('name'); 
      $data['email'] = $this->_getParam('email'); 
      $data['mobile'] =$this->_getParam('mobile'); 
      $data['query'] =$this->_getParam('query'); 
    $mapper = new Application_Model_EnquiryMapper(); 
     $mapper->save($data); 
} 

當我點擊提交按鈕,如果驗證是錯誤的,它立即移動到函數並保存在數據庫中,並顯示驗證警報,並在輸入驗證後真正的數據再次被保存。由此我的表單被多次保存。取而代之的是光標應該被移動到getEnquiryForm()saveEnquiryAction()只有當所有的表單驗證爲真

回答

1

你爲什麼不創建一個Zend_Form和驗證呢?

http://framework.zend.com/manual/1.12/en/zend.form.html

或者你可以直接使用抽象類Zend_Validate的檢查,如果驗證通過與否。並建立一個錯誤數組來顯示視圖

$errors = array(); 

$name = $this->_getParam('name'); 

/** @see Zend_Validate */ 
// Zend_Validate::is($value,$baseClassName); 
// baseClassName: NotEmpty, EmailAddress, Uri, GreaterThan, LessThan 

if(Zend_Validate::is($name,'NotEmpty')) { 
    $data['name'] = $name; 
} 
else { 
    $errors['name'] = 'Empty'; 
} 
if(Zend_Validate::is($name,'EmailAddress')) { 
    $data['email'] = $email; 
} 
else { 
    $errors['email'] = 'Not an email'; 
} 
... 

$enquiryMapper = new Application_Model_EnquiryMapper(); 
//check if existing? 
$enquiry = $enquiryMapper->fetchByEmail($email); 
if($enquiry) { 
    $errors['email'] = 'Email existing'; 
} 
... 
//check if no errors are occured 
if(!count($errors)) { 
    //save your model 
    $data = array(); 
    $data['name'] = $this->_getParam('name'); 
    $data['email'] = $this->_getParam('email'); 
    $data['mobile'] =$this->_getParam('mobile'); 
    $data['query'] =$this->_getParam('query'); 

    $enquiry = $enquiryMapper->save($data); 
} 
... 
$this->view->enquiry = $enquiry; //used to check if saved correctly 
$this->view->errors = $errors; //used to show errors in the view (foreach) 

用戶錯誤,但我強烈建議您使用Zend_Form的對象和驗證器和過濾器。

您將獲得過濾的乾淨值並已自動轉換(如果設置了Zend_Locale)錯誤消息。

在控制器經由新的Zend_Form

$form = new Zend_Form(); 
$form->setAction(""); 
$form->setMethod('POST'); 

$name = $form->createElement('text','name',array(
    //'label' => 'Name:', 
    'placeholder' => 'Name', 
    'required' => true, 
    'validators' => array(
     array(new Zend_Validate_NotEmpty(), true), 
     array(new Zend_Validate_StringLength(array('min' => 1,'max' => 64)),true) 
    ), 
    'filters' => array() 
)); 

$form->addElement($name); 
... 

$form->addElement('button', 'submit', array(
    'label' => "Save" 
)); 

$this->view->form = $form; 

或者在應用/形式延伸的Zend_Form/test.php的

class Application_Form_Test extends Zend_Form { 
    public function init() { 
     $this->setAction(""); 
     $this->setMethod("POST"); 

     $name = $this->createElement('text','name',array(
      //'label' => 'Name:', 
      'placeholder' => 'Name', 
      'required' => true, 
      'validators' => array(
       array(new Zend_Validate_NotEmpty(), true), 
       array(new Zend_Validate_StringLength(array('min' => 1,'max' => 64)),true) 
      ), 
      'filters' => array() 
     )); 

     $this->addElement($name); 
     //... 
     $this->addElement('button', 'submit', array(
      'label' => "Save" 
     )); 
    } 
} 

在控制器

$form = new Application_Form_Test(); //or directly create like shown above 

$request = $this->getRequest(); 

if($request->isPost()) { 
    //validate form (auto render errors) 
    if($form->isValid($request->getPost())) { 
     //form is valid... 
     //check for existing objects by email, name, phone, etc 
     //save your object to db 
    } 
} 
$this->view->form = $form; 

在查看

echo $this->form;