Zend framework 限制zend multiselect中的最大选择数

Zend framework 限制zend multiselect中的最大选择数,zend-framework,zend-form-element,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'

您好,我创建了一个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个以上的值。我在($maxSelections)之间尝试了Zend_Validate_,如上所示。但我仍然没有收到任何错误消息


有任何帮助吗?

要获取错误消息,请使用:

 $result = $selectValid->isValid($currently_selected_value);
    if($result){
         $selectValid->getMessages();
    }
如果
$result
当前选定的值(多选框中选择的值)不在指定范围内,则
$result
将包含false,以获取错误消息


.

谢谢你的回复,只有一个疑问。我正在检查indexAction()中值的有效性。因此,在索引操作中获取post数据后,我应该执行$maxSelections=array('min'=>3,'max'=>4)$selectValid=new Zend\u Validate\u Between($maxSelections)$选择Valid->setMessage(“”);然后检查验证。是并使用您的选择输入(在您的帖子数据中)检查验证