Details
-
Type:
Bug
-
Status:
Resolved
-
Priority:
Minor
-
Resolution: Not an Issue
-
Affects Version/s: 1.5.0RC1
-
Fix Version/s: None
-
Component/s: Zend_Form
-
Labels:None
-
Fix Version Priority:Must Have
Description
I add to Zend_Form_Element_Text Alnum validation and set this field as required. Then automatically is applied NotEmpty validation. There is two problems i found.
First you are not able to change the default message for NotEmpty validation.
Second Alnum validation has himself check for empty string but this is not applied until i add NotEmpty validation.
$form = new Zend_Form(); $form->addElement('text', 'username', array('label'=>'Потербител')); $userAlnum = new Zend_Validate_Alnum(); $userAlnum->setMessage( 'Моля въведете потребителско име!', Zend_Validate_Alnum::STRING_EMPTY ); $userAlnum->setMessage( 'Потебителкото Ви име трябва да съдържа само букви и числа', Zend_Validate_Alnum::NOT_ALNUM ); $userNotEmpty = new Zend_Validate_NotEmpty(); $userNotEmpty->setMessage( 'Моля въведете потребителско име!', Zend_Validate_NotEmpty::IS_EMPTY ); $form->username->addValidator($userNotEmpty); $form->username->addValidator($userAlnum); $form->username->setRequired(true);
See screen shot 1.
$form = new Zend_Form(); $form->addElement('text', 'username', array('label'=>'Потербител')); $userAlnum = new Zend_Validate_Alnum(); $userAlnum->setMessage( 'Моля въведете потребителско име!', Zend_Validate_Alnum::STRING_EMPTY ); $userAlnum->setMessage( 'Потебителкото Ви име трябва да съдържа само букви и числа', Zend_Validate_Alnum::NOT_ALNUM ); $userNotEmpty = new Zend_Validate_NotEmpty(); $userNotEmpty->setMessage( 'Моля въведете потребителско име!', Zend_Validate_NotEmpty::IS_EMPTY ); //$form->username->addValidator($userNotEmpty); $form->username->addValidator($userAlnum); $form->username->setRequired(true);
See screen shot 2.
First, in your second example you never associated your custom NotEmpty validator with the form – you only added the $userAlnum validator. If you add your own NotEmpty validator to the element, then Zend_Form_Element will not attempt to add one itself.
Second, you can actually disable having required autoload a NotEmpty validator by calling:
$element->setAutoInsertNotEmptyValidator(false);(this can be called from a configuration key as well as the key 'autoInsertNotEmptyValidator')
Finally, I'd recommend that you use translation files to set your messages instead of setting them manually. This will work globally and not require changes to your code.
$element->setAutoInsertNotEmptyValidator(false);