ZF2 - 表单创建缺少验证器

时间:2015-01-07 12:10:40

标签: zend-framework2 zend-form

我在将验证器添加到ZF2表单对象时遇到了一些麻烦。我是从数据库中的模式集构建表单,因此我可以快速验证一组用户输入。

这是生成表单对象的代码

    //initialise the form
    $form = new Form();

    //need to loop through the schema to create the form
    for($i=0; $i < count($schema); $i++)
    {
        $form_options = array();
        //add the basics to the form
        $form_options['name'] = $schema[$i]['field_name'];
        $form_options['type'] = $schema[$i]['field_type'];
        //check if this is a required field
        if($schema[$i]['is_required'] == 'true')
        {
            $form_options['required'] = true;
        }

        //add functions to filter the input form
        $function_filters = explode(',', $schema[$i]['function_filter']);
        if(!empty($function_filters))
        { 
            $filters = array();
            for($j=0; $j < count($function_filters); $j++)
            {
                $filters = array('name' => $function_filters[$j]);
            }

            $form_options['filters'] = $filters;
        }
        //add validators to the field array
        $validators = array();
        if(!is_null($schema[$i]['min_length']) && !is_null($schema[$i]['max_length']))
        {
            $validators[] = array(
                'name' => 'StringLength',
                'options' => array(
                        'encoding' => 'UTF-8',
                        'min' => (int) $schema[$i]['min_length'],
                        'max' => (int) $schema[$i]['max_length'],
                )
            );
        }

        //our regex validator if it exists
        if(!is_null($schema[$i]['regex_filter']) || strlen($schema[$i]['regex_filter']) != 0)
        {
            $validators[] = array(
                'name' => 'regex',
                'options' => array(
                        'pattern' => $schema[$i]['regex_filter'],
                        'messages' => array(
                            \Zend\Validator\Regex::INVALID => $schema[$i]['regex_invalid'],
                            \Zend\Validator\Regex::NOT_MATCH => $schema[$i]['regex_not_match'],
                            \Zend\Validator\Regex::ERROROUS => $schema[$i]['regex_errorus'],
                        )
                )
            );
        }

        //only add the validators if theres something in there
        if(!empty($validators))
        {
            $form_options['validators'] = $validators;
        }
        $form->add($form_options);
    }

    //return our form object
    return $form; 

代码块的行为与我期望的方式相同,$ form_option在执行上述代码后的输出如下所示: -

Array 
(
    [name] => username
    [type] => Text
    [required] => 1
    [filters] => Array
    (
        [name] => StripTags
    )

    [validators] => Array
    (
        [0] => Array
        (
            [name] => StringLength
            [options] => Array
            (
                [encoding] => UTF-8
                [min] => 3
                [max] => 50
            )

        )

        [1] => Array
        (
            [name] => regex
            [options] => Array
            (
                [pattern] => /^[a-zA-Z0-9_]{3,50}$/
                [messages] => Array
                (
                    [regexInvalid] => L_REGEX_INVALID
                    [regexNotMatch] => L_REGEX_USERNAME
                    [regexErrorous] => L_REGEX_ERRORUS
                )

            )

        )
    )
)

当我来测试时,它忽略了StringLength&amp;完全正则表达式验证器,并且只关注最后一个表单元素所需的验证器。

任何人都知道出了什么问题?

0 个答案:

没有答案