总是得到zf2 select元素中的haystack中找不到输入

时间:2014-12-30 06:31:29

标签: php zend-framework2

我是zf2的新手

当我从下拉菜单中选择语言时,我总是收到错误The input was not found in the haystack。这是我的代码:

在我的控制器中

function singleAction(){
—
—-
—-
$language = array();
$languages = $this->getManageTable()->getLanguage();

foreach($languages as $lang){
$language[”] = ‘Select Language';
$language[$lang[‘id_language’]] = $lang[‘name’];
}
—
—-
—-
return new ViewModel(array(
‘form’ => $form,
‘language’ => $language,
));
}

在manageForm.php中:

// Language Input
$language = new Element\Select(‘language’);
$language->setAttributes(array(
‘id’ => ‘language’,
‘class’ => ‘form-control’,
));
$this->add($language);

$language = new InputFilter\Input(‘language’);
$language->setRequired(false);
//$language->removeValidator(“NotEmpty”); // not working giving error
// not working giving error:
//$language->setAllowEmptysetRegisterInArrayValidator(false); 
//$language->setErrorMessage(‘Please select language’);
$inputFilter->add($language);

在我的视图中,我这样称呼:

语言

<?php echo formSelect($form->get(‘language’)->setValueOptions($language)); ?>

现在当我提交而没有从下拉列表中选择值时工作正常..

..但如果我选择某种语言并提交,我会收到此错误The input was not found in the haystack

我查看了我在发布的语言数据,但如果($form->isValid()) {—-}给了我一个错误。

我如何解决这个问题,我检查了很多链接,但没有找到任何解决方案。请帮我。我不想验证这个下拉元素。

2 个答案:

答案 0 :(得分:5)

这对我有用:

$this->add(
            array(
                'type'=> 'Zend\Form\Element\Select',
                'name' => 'language',
                'required' => true,
                'attributes' => array(
                    'class' => 'form-control',
                    'value' => 'en' ,
                ),
                'options' => array(
                    'disable_inarray_validator' => true,
                    )
            ));

答案 1 :(得分:2)

感谢上帝,

我得到了解决方案

在manageForm.php中:

    $language = new Element\Select('language');
    $language->setAttributes(array(
            'id' => 'language',
            'class' => 'form-control',
        ));
    $language->setDisableInArrayValidator(true);
    $this->add($language);
    $language = new InputFilter\Input(‘language’);
    $language->setRequired(false);
     $language->setErrorMessage(‘Please select language’);
   $inputFilter->add($language);

它,现在工作正常。可能它会帮助某人。 谢谢