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

时间:2014-05-06 12:51:49

标签: javascript php jquery validation zend-framework

我有表格的表格和验证。为了保存数据,我使用了一个函数来使用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,其中我在函数

中的url中提到了 saveEnquiry() 操作
    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); 
}

单击提交按钮后,如果验证为false,则立即转移到该功能并保存在数据库中,并显示验证警报,并在输入验证后为true,数据将再次保存。通过这个我的表格被多次保存。除此之外,光标应仅在所有表单中移动到 getEnquiryForm() saveEnquiryAction() 验证是真的

1 个答案:

答案 0 :(得分: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;

或者在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;