Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Zend framework2 ZF2-表单创建缺少验证程序_Zend Framework2_Zend Form - Fatal编程技术网

Zend framework2 ZF2-表单创建缺少验证程序

Zend framework2 ZF2-表单创建缺少验证程序,zend-framework2,zend-form,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 = ar

我在向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; 
当我开始测试它时,它完全忽略了StringLength和Regex验证器,只关注最后一个表单元素所需的验证器


有人知道出了什么问题吗?

您真的应该使用InputFilter类。。。将其更改为输入过滤器,工作正常。谢谢
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
                )

            )

        )
    )
)