我正在试图弄清楚如何让这种zend形式进行验证。我不明白:
addValidator()参数是否是特定的验证器?这些验证器的某处是否有列表?
我在表格/ contact.php中有这个:
类Application_Form_Contact扩展了Zend_Form {
public function init()
{
$this->setAction('index/process');
$this->setMethod('post');
$name = new Zend_Form_Element_Text('name');
$name->setLabel('Name:');
// $name->addValidator('alnum');
$name->setRequired(true);
$email = new Zend_Form_Element_Text('email');
$email->setLabel('Email:')->setRequired(true);
$confirm = new Zend_Form_Element_Text('confirm');
$confirm->setLabel('Confirm Email:')->setRequired(true);
$phone = new Zend_Form_Element_Text('phone');
$phone->setLabel('Phone:')->setRequired(true);
$subject = new Zend_Form_Element_Select('subject');
$subject->setLabel('Subject:')->setRequired(true);
$subject->setMultiOptions(array('Performance'=>'Performance',
'Workshop'=>'Workshop',
'Other'=>'Other'
));
$message = new Zend_Form_Element_Textarea('message');
$message->setLabel('Message:')->setRequired(true);
$message->setAttrib('rows','6');
$message->setAttrib('cols','30');
$submit = new Zend_Form_Element_Submit('Submit');
$this->addElements(array( $name,
$email,
$confirm,
$phone,
$subject,
$message,
$submit
));
$this->setElementDecorators(array
('ViewHelper',
array(array('data' => 'HtmlTag'), array('tag' => 'td')),
array('Label' , array('tag' => 'td')),
array(array('row' => 'HtmlTag') , array('tag' => 'tr'))
));
$submit->setDecorators(array('ViewHelper',
array(array('data' => 'HtmlTag'), array('tag' => 'td')),
array(array('emptyrow' => 'HtmlTag'), array('tag' => 'td', 'placement' => 'PREPEND')),
array(array('row' => 'HtmlTag') , array('tag' => 'tr'))
));
$this->setDecorators(array(
'FormElements',
array('HtmlTag' , array('tag' => 'table' , 'class' => 'formTable')),
'Form'
)
);
}
}
我的控制器是:
public function indexAction()
{
$this->view->form = new Application_Form_Contact();
}
public function processAction()
{
// $this->view->form = new Application_Form_Contact();
//
if ($this->_request->isPost()) {
$formData = $this->_request->getPost();
// echo 'success';
$this->view->data = $formdata;
} else {
// $form->populate($formData);
}
}
我是新手,所以我可能会犯一些我看不到的明显错误。我正在尝试进行基本验证:
非常感谢任何帮助!
答案 0 :(得分:5)
您是否尝试过isValid()?:
$form = new forms_ContactForm();
if ($this->_request->isPost()) {
$formData = $this->_request->getPost();
if ($form->isValid($formData)) {
echo 'success';
exit;
} else {
$form->populate($formData);
}
}
$this->view->form = $form;
关于验证人:
$firstName = new Zend_Form_Element_Text('firstName');
$firstName->setLabel('First name')
->setRequired(true)
->addValidator('NotEmpty');
$lastName = new Zend_Form_Element_Text('lastName');
$lastName->setLabel('Last name')
->setRequired(true)
->addValidator('NotEmpty');
$email = new Zend_Form_Element_Text('email');
$email->setLabel('Email address')
->addFilter('StringToLower')
->setRequired(true)
->addValidator('NotEmpty', true)
->addValidator('EmailAddress');
以下是关于Zend fiorms和验证器的Zend文档的链接。 Creating Form Elements Using Zend_Form_Element