问题描述
我正在尝试在实际表单本身中验证表单,但它根本没有得到验证.如果我输入someemail&*!([]@gmail.com",它并没有真正废话并继续前进.形式:
I'm trying to validate a form in the actual form itself, but it doesn't get validated at all. If I type "someemail&*!([]@gmail.com", it doesn't really give a crap and moves on.Form:
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,
));
}
}
我如何获得(或试图获得)控制器中的过滤器和验证器:
How I'm getting (or trying to get) the filters and validators in the controller:
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;
}
推荐答案
我认为是因为验证器的实现错误.
I think it's because of wrong implementation of validators.
您可以尝试在表单上实现 InputProviderInterface
吗?文档.它设置在 Element 上,但您也可以在表单上执行此操作.这个接口定义了 getInputSpecification
,你必须在其中返回一个带有过滤器和验证器规范的数组.
Could you try to implement InputProviderInterface
on your form? Basic example is given in the docs. There it is set on an Element, but you can do this on your form too. This interface define getInputSpecification
where you have to return an array with filter and validator specification.
这篇关于Zend - 表单未得到验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!