Alex Pliutau
Alex Pliutau

Reputation: 21957

Zend Form Validating of required elements


I have the required element in my Zend Form:

$name = new Zend_Form_Element_Text('name');
$name->setLabel('Name')
   ->setValue(isset($plan)?$plan['name']:'')
   ->setRequired()
   ->setAttribs(array('class' => 'required', 'maxlength' => 50))
   ->addValidators(array(new Zend_Validate_StringLength(array('min' => 1, 'max' => 50)),
      new Zend_Validate_Db_NoRecordExists(array('table' => 'plan', 'field' => 'name')))
   ->addFilters(array(new Zend_Filter_StringTrim, new Zend_Filter_StripTags));

All validators work perfect. But it is one problem. In controller I check form using getValidValues (it is required for me). And if Record in DB is exist, element is invalid and Zend clear this element. And I take the message 'Value is required'. How can I get message about existing of row in DB?
Sorry for my english. Thank you in advance.

Upvotes: 0

Views: 2765

Answers (2)

DeveloperItsMe
DeveloperItsMe

Reputation: 19

try

setRequired(true)

this is right ;)

Upvotes: 1

Alex Pliutau
Alex Pliutau

Reputation: 21957

It was helpful for me:
->setAllowEmpty(false) instead of ->setRequired()

Upvotes: 1

Related Questions