我试图在实际表单中验证表单,但它根本没有得到验证。如果我输入" someemail& *!([] @ gmail.com",它并没有真正给出废话并继续前进。 表格:
class RecoverPasswordForm extends Form {
public function __construct($options = null) {
parent::__construct("RecoverPassword");
$username = new Element("username");
$username->setLabel("Email");
$username->setAttributes(
array('filters' => array(
array('name' => 'StringTrim'),
array('name' => 'StripTags')),
'validators' => array(
array(
'name' => 'Regex',
'options' => array(
'pattern' => '/^[a-zA-Z0-9.!#$%&\'*+\/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/',
'messages' => array(
Regex::NOT_MATCH => 'The email your provided has invalid characters.',
),
),
'break_chain_on_failure' => true
),
array(
'name' => 'EmailAddress',
'options' => array(
'messages' => array(
EmailAddress::INVALID_FORMAT => "Email address is invalid",
EmailAddress::INVALID => "",
EmailAddress::INVALID_LOCAL_PART => "",
EmailAddress::INVALID_HOSTNAME => "",
EmailAddress::INVALID_SEGMENT => "",
EmailAddress::DOT_ATOM => "",
EmailAddress::INVALID_MX_RECORD => "",
),
),
),
),
'label' => 'Email'));
$this->add(array(
$username,
));
}
}
我如何获得(或试图获得)控制器中的过滤器和验证器:
public function recoverPasswordAction() {
$this->cache = new Container("cache");
$request = $this->getRequest();
$form = new RecoverPasswordForm();
// $form->get("submit")->setValue("Send mail");
$this->view->form = $form;
if ($request->isPost()) {
$user = new User();
$form->getInputFilter()->getValues();
$form->setInputFilter($form->getInputFilter());
$data = $request->getPost();
$form->setData($data);
$username = $data['username'];
$userData = $this->getServiceLocator()->get("UserTable")->fetchList("`username` LIKE '$username'");
if (!count($userData) > 0) {
$this->cache->error = "A user with this email does not exist, plese try again.";
}
if ($form->isValid()) {
foreach ($userData as $user) {
$user->sendMail(6);
$this->cache->success = "A message has been send to this email, containing your password.";
}
}
}
return $this->view;
}
答案 0 :(得分:0)
试试这个......
$request = $this->getRequest();
$form = new Form();
if($request->isPost()) {
if($form->isValid($reguest->getPost())) {
$values = $form->getValues(); //filtered valid array of values
$username = $form->getValue('username');
}
}
$this->view->form = $form;
答案 1 :(得分:0)
我认为这是因为验证器的实施错误。
您可以尝试在表单上实施InputProviderInterface
吗?基本示例在docs中给出。它在元素上设置,但您也可以在表单上执行此操作。此接口定义getInputSpecification
,您必须返回一个包含过滤器和验证器规范的数组。