Zend Framework 2 仅显示所需的自定义验证错误消息
Zend Framework 2 only show required custom validation error message
我有一个必填的 ISBN 字段,我想为其更改一般错误消息。我已经设置了自定义错误消息并且它们有效。过滤器是模型 fieldSet
的 getInputFilterSpecification()
函数的一部分,它可以正确验证和显示自定义错误消息:
use Zend\Form\Element;
use Zend\Form\Fieldset;
use Zend\Validator;
use Zend\InputFilter\InputFilterProviderInterface;
use Zend\Stdlib\Hydrator\ClassMethods as ClassMethodsHydrator;
class BookItemFieldset extends Fieldset implements InputFilterProviderInterface
{
public $Types;
public function __construct($itemName, $Types)
{
parent::__construct($itemName, $Types);
$this->Types = $Types;
// .. Other fields
$bookISBN = new Element\Number('bookISBN');
$bookISBN->setLabel('Book ISBN ')
->setAttribute('id', 'bookISBN');
// .. more fields
$this->add($bookISBN);
}
public function getInputFilterSpecification()
{
return array(
'bookISBN' => array(
'required' => true,
'validators' => array(
new Validator\NotEmpty(array(
'setMessage'=> 'ISBN is Required'
)
),
new Validator\Isbn(array(
'setMessage'=> 'ISBN is Invalid'
)),
)
),
//... more input filters
);
}
}
但是当该字段留空时。 "ISBN is Invalid" 和 "ISBN is required" 消息都会出现。
有没有办法在该字段留空时只显示所需的错误消息?
类似于以下内容:
'bookISBN' => array(
'required' => array(
'required' => true,
'setMessage' => 'ISBN is required' // Only this shows if field is empty
),
'validators' => array(
new Validator\Isbn(array(
'setMessage'=> 'ISBN is Invalid' // only this shows if the input is invalid
)),
)
),
谢谢。
您需要通过将其设置为 true 来打破链条,默认情况下 Zend 将其设置为 false。你能试试看下面的代码是否有效吗?
public function getInputFilterSpecification()
{
return array(
'bookISBN' => array(
'required' => true,
'validators' => array(
array(
'name' => 'not_empty',
'break_chain_on_failure' => true,
'options' => array(
'messages' => array(
\Zend\Validator\NotEmpty::IS_EMPTY => 'ISBN is required',
),
),
),
new Validator\Isbn(array(
'setMessage'=> 'ISBN is Invalid'
)),
)
),
//... more input filters
);
}
或者可能是 'NotEmpty'。我无法测试它。
public function getInputFilterSpecification()
{
return array(
'bookISBN' => array(
'required' => true,
'validators' => array(
array(
'name' => 'NotEmpty',
'break_chain_on_failure' => true,
),
new Validator\Isbn(array(
'setMessage'=> 'ISBN is Invalid'
)),
)
),
//... more input filters
);
}
我有一个必填的 ISBN 字段,我想为其更改一般错误消息。我已经设置了自定义错误消息并且它们有效。过滤器是模型 fieldSet
的 getInputFilterSpecification()
函数的一部分,它可以正确验证和显示自定义错误消息:
use Zend\Form\Element;
use Zend\Form\Fieldset;
use Zend\Validator;
use Zend\InputFilter\InputFilterProviderInterface;
use Zend\Stdlib\Hydrator\ClassMethods as ClassMethodsHydrator;
class BookItemFieldset extends Fieldset implements InputFilterProviderInterface
{
public $Types;
public function __construct($itemName, $Types)
{
parent::__construct($itemName, $Types);
$this->Types = $Types;
// .. Other fields
$bookISBN = new Element\Number('bookISBN');
$bookISBN->setLabel('Book ISBN ')
->setAttribute('id', 'bookISBN');
// .. more fields
$this->add($bookISBN);
}
public function getInputFilterSpecification()
{
return array(
'bookISBN' => array(
'required' => true,
'validators' => array(
new Validator\NotEmpty(array(
'setMessage'=> 'ISBN is Required'
)
),
new Validator\Isbn(array(
'setMessage'=> 'ISBN is Invalid'
)),
)
),
//... more input filters
);
}
}
但是当该字段留空时。 "ISBN is Invalid" 和 "ISBN is required" 消息都会出现。
有没有办法在该字段留空时只显示所需的错误消息?
类似于以下内容:
'bookISBN' => array(
'required' => array(
'required' => true,
'setMessage' => 'ISBN is required' // Only this shows if field is empty
),
'validators' => array(
new Validator\Isbn(array(
'setMessage'=> 'ISBN is Invalid' // only this shows if the input is invalid
)),
)
),
谢谢。
您需要通过将其设置为 true 来打破链条,默认情况下 Zend 将其设置为 false。你能试试看下面的代码是否有效吗?
public function getInputFilterSpecification()
{
return array(
'bookISBN' => array(
'required' => true,
'validators' => array(
array(
'name' => 'not_empty',
'break_chain_on_failure' => true,
'options' => array(
'messages' => array(
\Zend\Validator\NotEmpty::IS_EMPTY => 'ISBN is required',
),
),
),
new Validator\Isbn(array(
'setMessage'=> 'ISBN is Invalid'
)),
)
),
//... more input filters
);
}
或者可能是 'NotEmpty'。我无法测试它。
public function getInputFilterSpecification()
{
return array(
'bookISBN' => array(
'required' => true,
'validators' => array(
array(
'name' => 'NotEmpty',
'break_chain_on_failure' => true,
),
new Validator\Isbn(array(
'setMessage'=> 'ISBN is Invalid'
)),
)
),
//... more input filters
);
}