-- Paul Reinheimer <p...@phparch.com> wrote
(on Thursday, 08 January 2009, 10:21 PM -0500):
> Do you think you could point me in the direction of an example  
> demonstrating building a form like this
> <?php
>
>
> class Form_Upload extends Zend_Form
> {
>     public function init()
>     {
>         $this->setMethod('post');
>         $this->setAction('/images/uploadImage');
>         $this->setAttrib('enctype', 'multipart/form-data');
>
>         $this->addElement('text', 'name', array(
>             'label' => 'Name',
>             'required' => true,
>             'validators' => array(
>                 array('validator' => 'StringLength', 'options' =>  
> array(3, 20)))
>             )
>         );
>
>
>
>
> That Sets a custom error message for failure, and/or uses a regex  
> validator? I've been playing around a lot
> and I'm having trouble with those two.
>
> I can do
> $this->name->getValidator('StringLength')->setMessage('Names must be  
> between 3 and 20 characters long');
>
> But it seems as though there should be something in that array I can  
> set.

There is; pass a "messages" key to the validator options:

    'validators' => array(
        array('StringLength', true, array(
            3, 20,
            'messages' => array(
                'stringLengthTooShort' => 'Names must be at least 3 characters 
long',
                'stringLengthTooLong'  => 'Names must be no more than 20 
characters long',
            ),
        )),
    ),

As for regex validation:

    'validators' => array(
        array('Regex', true, array(
            '/^[a-z\'. -]{3,20}$/i', 
            'messages' => array(
                'regexNotMatch' => 'Names must be between 3 and 20 characters 
long',
            ),
        )),
    ),

-- 
Matthew Weier O'Phinney
Software Architect       | matt...@zend.com
Zend Framework           | http://framework.zend.com/

Reply via email to