Accessing Error Messages for InArray Authentication Using Zend_Form_Element_Select

I am using Zend Framework 1.62 (because we are deploying the off-the-shelf product to a Red Hat instance that does not have enough hgih version to support> ZF1.62).

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

Zend_Form_Element_Select works exactly as expected, showing invalid if any other value is entered other than one of the multiple selection options I added.

The problem occurs when I want to render the form at some point, I cannot edit the error message generated by the pre-registered InArray validator automatically added by ZF. I know I can turn this behavior off, but it works great apart from the error messages. I've tried 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');

      

// Conflicts elswhere in the application and does not allow granular control over error messages.

Does anyone have any idea?

Ben

+2


source to share


3 answers


I usually set validators according to 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,
            ));

      



An array of validators in element parameters can take a validator name (string) or an array.

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

+1


source


If you are using Zend_Form_Element_Select (or any of the Multi subclasses), then the InArray validation will only be added automatically if not present.

You can install the validator like this:



$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 auto-attached one.

+1


source


$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.');

      

0


source







All Articles