Issues

ZF-6354: Doubt about addError - Zend_Form

Issue Type: Docs: Improvement Created: 2009-04-20T05:47:57.000+0000 Last Updated: 2012-07-30T15:08:11.000+0000 Status: Closed Fix version(s): Reporter: Maurício Vinicius (vaza) Assignee: Frank Brückner (frosch) Tags: - Zend_Form

Related issues: Attachments:

Description

Hello, I tried to find something about custom errors depending the value and validator of the form.... Can I set my own messages errors depending the validation of the form? for example: If I set a StringLength validator, I show the message: "You wrote less than 6 chars." On the same textfield, if the user write any number, show another message like "stupid! only chars!".

Comments

Posted by Frank Brückner (frosch) on 2012-07-30T15:06:17.000+0000

You don't need custom error messages!

Use another validator:

<pre class="highlight">
$form->addElement(
    'text',
    'foo',
    array(
        'label' => 'Foo',
        'validators' => array(
            array(
                'Digits',
                true,
                array(
                    'messages' => array(
                        Zend_Validate_Digits::NOT_DIGITS => 'stupid! only chars!',
                    )
                ),
            ),
            array(
                'StringLength',
                true,
                array(
                    'min' => 6,
                    'messages' => array(
                        Zend_Validate_StringLength::TOO_SHORT => 'You wrote less than 6 chars.',
                    ),
                )
            ),
        ),
    )
);

Or use regex:

<pre class="highlight">
$form->addElement(
    'text',
    'foo',
    array(
        'label' => 'Foo',
        'validators' => array(
            array(
                'Regex',
                true,
                array(
                    'pattern'  => '/^[0-9]+$/', 
                    'messages' => array(
                        Zend_Validate_Regex::NOT_MATCH => 'stupid! only chars!',
                    )
                ),
            ),
            array(
                'StringLength',
                true,
                array(
                    'min' => 6,
                    'messages' => array(
                        Zend_Validate_StringLength::TOO_SHORT => 'You wrote less than 6 chars.',
                    ),
                )
            ),
        ),
    )
);

Have you found an issue?

See the Overview section for more details.

Copyright

© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts