Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend_Form radio element

Below is sample code to create a radio button element with Yes/No options in Zend_Form. Any ideas on how to set the required answer to Yes, so if No is selected, it'll fail validation? The code below will accept either Yes or No.

    $question= new Zend_Form_Element_Radio('question');
    $question->setRequired(true)
        ->setLabel('Are you sure?')
        ->setMultiOptions(array('Yes', 'No'));
like image 359
dittonamed Avatar asked Jan 16 '09 22:01

dittonamed


1 Answers

Not sure if this is the best way, but it worked for me:

$questionValid = new Zend_Validate_InArray(array('Yes'));
$questionValid->setMessage('Yes is required!');

$question = new Zend_Form_Element_Radio('question');
$question->setRequired(true)
    ->setLabel('Are you sure?')
    ->setMultiOptions(array('Yes'=>'Yes', 'No'=>'No'))
    ->addValidator($questionValid);
like image 140
Gilean Avatar answered Sep 22 '22 19:09

Gilean