2

I've added the EmailAddress validator to my form element and when I'm trying to submit something like test, it shows me 2 errors:

* The input does not match against pattern '/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/'  
* The input is not a valid email address. Use the basic forma local-part@hostname

But I don't want to fright my users with such a terrible validation information. Of course I can redeclare the message for each validation error, but the perfect for me would be to set the only one error message like The input is not a valid email address. Use the basic forma local-part@hostname.

I can't believe that there is no way in zf2 to do that.

4

1 回答 1

0

我发现这段代码对我有用。我希望这也适用于你。

'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    => 'Please provide a valid email address.',
                ),
            ),
            'break_chain_on_failure' => true
        ),
        array(
            'name' => 'EmailAddress',
            'options' => array(
                'messages' => array(
                    EmailAddress::INVALID_FORMAT   => 'Please provide a valid email address.',
                    EmailAddress::DOT_ATOM         => '',
                    EmailAddress::INVALID_FORMAT   => '',
                    EmailAddress::INVALID_LOCAL_PART => '',
                    EmailAddress::QUOTED_STRING => '',
                )
            ),
        ),
    ),
于 2014-03-22T09:45:28.620 回答