Zend Framework

Zend_Form_Element NotEmpy validation when required is set to true

Details

  • Type: Bug Bug
  • Status: Resolved Resolved
  • Priority: Minor 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.

Activity

Hide
Matthew Weier O'Phinney added a comment -

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.

Show
Matthew Weier O'Phinney added a comment - 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.
Hide
Hristo Angelov added a comment -

Yes i don't add NotEmpty validation second time to show that i cant costume the message for empty string. And i want to show that when i don't have NotEmpty validation Zend_Validate_Alnum::STRING_EMPTY message is not shown. But when i have it is shown. I think it is kind of mess there. When i insert Alnum validation this means that the string in field cannot be empty and when is empty Zend_Validate_Alnum::STRING_EMPTY message sholud be shown instead of non costuming NotEmpty validation message .

Show
Hristo Angelov added a comment - Yes i don't add NotEmpty validation second time to show that i cant costume the message for empty string. And i want to show that when i don't have NotEmpty validation Zend_Validate_Alnum::STRING_EMPTY message is not shown. But when i have it is shown. I think it is kind of mess there. When i insert Alnum validation this means that the string in field cannot be empty and when is empty Zend_Validate_Alnum::STRING_EMPTY message sholud be shown instead of non costuming NotEmpty validation message .

People

Vote (0)
Watch (1)

Dates

  • Created:
    Updated:
    Resolved: