在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;
相关文章:
- 在提交之前验证表单,jQuery发布到javaScript,然后提交到确认页面
- 密码并确认密码验证
- 密码确认验证未按预期显示
- 必填字段验证器,JavaScript 确认函数未验证
- 有没有一个工具来验证 ECMAScript 并确认它与 ECMAScript 语言规范第 3 版兼容
- 正在使用Javascript来验证“确认密码”输入,增加了任何安全风险
- Spring Security Core和用于确认密码字段的自定义验证器是可能的
- 密码确认验证失败
- 通过确认的角度形式验证
- 使用引导程序验证程序进行验证后,停止表单提交并打开引导模式(确认对话框)
- 如何在asp.net中验证表单数据后获得javascript确认框
- js表单验证-确认表单提交时没有发现错误
- 带有密码确认的 Javascript 表单验证
- 使用parsley js进行表单验证.字母数字和密码确认
- jQuery和javascript表单验证:密码字段参数未定义,密码确认ok
- 单击按钮,我想验证数据,如果为真,我想要删除确认对话框.这可能吗?
- Javascript确认验证表单
- 在确认模式和提交表单之前验证表单字段
- Javascript:验证多个密码和确认密码字段
- 在Zend Frame 1.12中保存之前确认验证