限制zend multiselect中的最大选择数

时间:2012-02-29 20:05:40

标签: zend-framework zend-form-element

您好我创建了一个包含多个选项的zend multiselect框。这是代码。

$time = new Zend_Form_Element_Multiselect('time');
$time->setLabel('Select Your Notification Timings: ')
->setMultiOptions(
    array(
        '0' => "Select", 
        '00:00' => '00:00', '00:30' => '00:30', '01:00' => '01:00', '01:30' => '01:30', '02:00' => '02:00', '02:30' => '02:30',  
        '03:00' => '03:00', '03:30' => '03:30', '04:00' => '04:00', '04:30' => '04:30', '05:00' => '05:00', '05:30' => '05:30',   
        '06:00' => '06:00', '06:30' => '06:30', '07:00' => '07:00', '07:30' => '07:30', '08:00' => '08:00', '08:30' => '08:30',
        '09:00' => '09:00', '09:30' => '09:30', '10:00' => '10:00', '10:30' => '10:30', '11:00' => '11:00', '11:30' => '11:30', 
        '12:00' => '12:00', '12:30' => '12:30', '13:00' => '13:00', '13:30' => '13:30', '14:00' => '14:00', '14:30' => '14:30',
        '15:00' => '15:00', '15:30' => '15:30', '16:00' => '16:00', '16:30' => '16:30', '17:00' => '17:00', '17:30' => '17:30',
        '18:00' => '18:00', '18:30' => '18:30', '19:00' => '19:00', '19:30' => '19:30', '20:00' => '20:00', '20:30' => '20:30',
        '21:00' => '21:00', '21:30' => '21:30', '22:00' => '22:00', '22:30' => '22:30', '23:00' => '23:00', '23:30' => '23:30' 
    ))
->setRequired(TRUE)
->addValidator('NotEmpty', true, array('integer', 'zero'));
$maxSelections = array('min' => 3, 'max' => 4);
$selectValid = new Zend_Validate_Between($maxSelections);
$selectValid->setMessage("Number of selected values should be minimum of '%min%' or maximum of '%max%'");
$time->size = 12;     
$this->addElement($time);

现在我想将最大选择数限制为4.我必须将所选值存储在数据库中。目前,如果用户选择超过4个,假设为6,则数据库将仅存储前4个值。但我想向用户显示错误消息,他选择了超过4个值。我尝试了Zend_Validate_Between($ maxSelections),如上所示。但我仍然没有收到任何错误消息。

任何帮助?

1 个答案:

答案 0 :(得分:0)

获取错误消息:

 $result = $selectValid->isValid($currently_selected_value);
    if($result){
         $selectValid->getMessages();
    }
如果$result(多选框中的值选择)不在指定范围内,

$currently_selected_value将包含false,在这种情况下会收到错误消息。