poashoas wrote
> So here I go, I have a form, I tried to tell the field it has a validator
> or something,
> and I got no idea where to start if I wanna show a message that the email
> address ain't valid.
> 
> So far I have a form working:
*
> The controller:
*
> 
> public function indexAction ()
> {
>     $form = new LoginForm();
>     return array('form' => $form);
> }
> 
> class Login extends Form
> {
>     public function __construct ()
>     {
>        parent::__construct('myform');
> 
>        $this->setAttributes(array(
>           'id' => 'loginForm',
>           'method' => 'post'
>        ))
>        ->add(array(
>                'name' => 'username',
>                'attributes' => array(
>                'type' => 'text',
>            ),
>           'filters' => array(
>              array('StringTrim')
>           ),
>           'validators' => array(
>              array('name' => 'EmailAddress')
>           ),
>           'options' => array(
>                'required' => true,
>                'label'    => 'Email'
>           )
>        ))
>        ->add(array(
>             'name' => 'submit',
>             'attributes' => array(
>                 'type' => 'submit',
>                 'value' => 'Login'
>            ),
>        ));
>     }
> }
*
> The view:
*
> 
> echo $this->form()->openTag($form);
> echo $this->formRow($form->get('username'));
> echo $this->formSubmit($form->get('submit'));
> echo $this->form()->closeTag($form);

Try this code for your Email address form element:

        $this->add(array(
            'name' => 'username',
            'options' => array(
                'label' => 'Email',
            ),
            'attributes' => array(
                'required' => 'required'
            ),
            'filters' => array(
                array('name' => 'StringTrim'),
            ),
            'validators' => array(
                array(
                    'name' => 'EmailAddress',
                    'options' => array(
                        'messages' => array(
                            \Zend\Validator\EmailAddress::INVALID_FORMAT =>
'Email address format is invalid'
                        )
                    )
                )
            )
        ));

The above will first apply a filter StringTrim which will remove empty
spaces and then it will validate an Email address and override a standard
message for invalid format. You can remove that option if you want to use a
standard message. For more information about forms you can read here:
http://www.michaelgallego.fr/blog/?p=190
<http://www.michaelgallego.fr/blog/?p=190>  



-----
Cheers,
--
Luke Mierzwa
--
View this message in context: 
http://zend-framework-community.634137.n4.nabble.com/zf2-form-validation-tp4656907p4656911.html
Sent from the Zend Framework mailing list archive at Nabble.com.

-- 
List: [email protected]
Info: http://framework.zend.com/archives
Unsubscribe: [email protected]


Reply via email to