php - Zend_Validate_Db_RecordExists or empty value -
i've got (hopefuly) simple task, , google fu has failed me. basically, i've got form select contains empty value , number of ids given content can belong to. want - validate if given ids exist, if value set. this:
$field = new zend_form_element_select('field'); $field->addvalidator( new zend_validate_db_recordexists( array( 'table' => 'categories', 'field' => 'id' ) ));
takes care of checking if given id exists, i'm not able find way omit if value empty. 1 way move logic isvalid
method, i'm hoping there's nicer way accomplish task.
try set form element not required:
$field->setrequired(false);
when element not required , not filled, validators queue won't run.
quick example works me:
// zend_form form body $this->addelement('select', 'category', array( 'label' => 'choose category', 'required' => false, 'multioptions' => array( null => 'no category selected', '1' => 'one', '2' => 'two', ), 'validators' => array( array('db_norecordexists', false, array( 'schema' => 'public', 'table' => 'category', 'field' => 'id', )), ), ));
Comments
Post a Comment