在Zend Frame 1.12中保存之前确认验证


Confirm Validation before save in Zend Frame work 1.12

我有一个表单和对表单的验证。为了保存数据,我使用了一个使用ajax保存数据的函数。这是我的表单

    <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()只有当所有的表单验证

为真时,才应该移动到

为什么不创建一个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 via new in controller

$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;

或者在application/forms/Test.php中扩展Zend_Form

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;