donv29560 2016-04-20 05:28
浏览 32
已采纳

Zend 2 Form Validator'InArray' - 复杂的数组返回无效

I am trying to implement the 'InArray' validator in Zend 2 on a form and it keeps on returning invalid. Here is the code:

The Form Element setup:

    $enquiryType = new Element\Select('enquiryType');
    $enquiryType->setLabel('* What is your enquiry about?')
                    ->setLabelAttributes(array('class' => 'sq-form-question-title'))
                    ->setAttribute('class', 'sq-form-field required')
                    ->setValueOptions($enquiryTypes);

The Filter/Validator setup:

    $enquiryType = new Input('enquiryType');
    $enquiryType->getValidatorChain()
             ->addByName('InArray', array('haystack' => $enquiryTypes));

And here is the array that gets passed into $enquiryTypes via the module.config.php file

        'enquiryCategories' => array(
                                'empty_option' => 'Please select an option',
                                array(
                                    'label' => 'General enquiries', 
                                    'options' => array( 
                                        'skype' => 'Skype with your library feedback',
                                        'question' => 'Ask a question',
                                        'feedback' => 'Library feedback',
                                        'eresource' => 'Electronic resources (e.g. e-book, e-journal, database)',
                                        'webbridge' => 'WebBridge Problems',
                                        'dro' => 'DRO'
                                    )
                                ),
                                array(
                                    'label' => 'Application to review',
                                    'options' => array(
                                        '10400' => 'I\'ve returned the item',
                                        '10401' => 'I didn\'t know about overdue points but now I have some',
                                        '10402' => 'Why did I get this invoice?',
                                        '10403' => 'The item I borrowed is lost',
                                        '10404' => 'The item I borrowed has been damaged',
                                        '10405' => 'I never got/had the book',
                                        '10406' => 'Other'
                                    )
                                )
                            ),

I have tried different variations of this (using the recursive validation also) but have not been able to work this out.

  • 写回答

2条回答 默认 最新

  • doufu6423 2016-05-02 06:18
    关注

    I finally got a working solution for this. I feel there should be a better solution but I could not find one.

    Here is the filter I used:

    $enquiryType = new Input('enquiryType');
    $enquiryType->getValidatorChain()
            ->addByName('InArray', array('haystack' => array_keys(
                                        $enquiryTypes[0]['options']+$enquiryTypes[1]['options']
                                    )));
    $enquiryType->getFilterChain()
             ->attach(new Filter\StringToLower())
             ->attachByName('StripTags');
    

    Basically I had to disect the array options into a straight associative array. As the array remains static in this instance then this works well.

    If the data becomes dynamic then something more would be required (loop through and find the 'option' key, add children to filter array etc...)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料