views:

777

answers:

3

Hello,

I'm using Zend Framework 1.62 (becuase we are deploying the finished product to a Red Hat instance, which doesn't have a hgih enough PHP version to support > ZF1.62).

When creating a Form using Zend Form, I add a select element, add some multi options. I use the Zend Form as an in-object validation layer, passing an objects values through it and using the isValid method to determine if all the values fall within normal parameters.

Zend_Form_Element_Select works exactly as expected, showing invalid if any other value is input other than one of the multi select options I added.

The problem comes when I want to display the form at some point, I cant edit the error message created by the pre registered 'InArray' validator added automatically by ZF. I know I can disable this behaviour, but it works great apart from the error messages. I've tryed the following:

$this->getElement('country')->getValidator('InArray')->setMessage('The country is not in the approved lists of countries');

// Doesn't work at all.

$this->getElement('country')->setErrorMessage('The country is not in the approved lists of countries');

// Causes a conflict elswhere in the application and doesnt allow granular control of error messages.

Anyone have any ideas?

Ben

+1  A: 

I usually set validators as per my example below:

$this->addElement('text', 'employee_email', array(
            'filters'    => array('StringTrim'),
            'validators' => array(                
                array('Db_NoRecordExists', false, array(
                    'employees',
                    'employee_email',
                    'messages' => array(Zend_Validate_Db_Abstract::ERROR_RECORD_FOUND => 'A user with email address %value% already exists')
                ))
            ),
            'label'     => 'Email address',
            'required'  => true,
            ));

The validators array in the element options can take a validator name (string) or an array.

When an array is passed, the first value is the name, and the third is an array of options for the validator. You can specify a key messages with custom messages for your element in this array of options.

David Caunt
Yes indeed.... however, In this instance the validator is set up in the background. From the manual:By default, this element registers an InArray validator which validates against the array keys of registered options. You can disable this behavior by either calling setRegisterInArrayValidator(false), or by passing a false value to the registerInArrayValidator configuration key.My question relates to accessing this particular validators error message.Thanks for your responce, any further insight? Ben
Ben Waine
A: 
    $el = $this->addElement($name, $label, $require, 'select');

    $validator = new Zend_Validate_InArray(array_keys(AZend_Geo::getStatesList()));
    $validator->setMessage('Invalid US State.');

    $el
    ->setMultiOptions(AZend_Geo::getStatesList())
    ->setRegisterInArrayValidator(false)

    ->addValidator($validator)

    ->addFilter(new Zend_Filter_StringToUpper())
    ->addFilter(new T3LeadBody_Filter_SetNull())

    ->setDescription('US State. 2 char.');
Anton Panfilov
A: 

If your using Zend_Form_Element_Select (or any of the Multi subclasses), on validation the InArray validator will only be automatically added if there is not one present.

You can set a validator as so:

$options = array(...);
$this->addElement('select', 'agree', array(
    'validators' => array(
        array('InArray', true, array(
            'messages' => array(
                Zend_Validate_InArray::NOT_IN_ARRAY => 'Custom message here',
             ),
             'haystack' => array_keys($options),
        )),
    'multiOptions' => $options,
));

and then your validator will be used instead of the automatically attached one.

Vaughn A