0

我正在努力使我的验证工作。我有数据张贴在这样的格式控制器:ZF2嵌套数据验证

[ 
    'property' => 'value', 
    'nested_property' => [ 
     'property' => 'value', 
     // ... 
    ] 
] 

我在这里把字段/过滤器和形成为不同类别,只是聚集一起在窗体的控制器,看起来像这样:

public function __construct($name, $options) 
{ 
    // ... 
    $this->add(new SomeFieldset($name, $options)); 
    $this->setInputFilter(new SomeInputFilter()); 
} 

但它不能正常工作,看起来像只是忽略嵌套数组(或忽略所有内容)。我错过了什么?

谢谢。

回答

1

如果您使用InputFilter类,则需要像设置包括字段集在内的表单一样设置inputfilter。

所以当你遇到这样的结构:

  1. MyForm的
    1.1 NestedFieldset
    1.2 AnotherFieldset

你inputfilters需要具有相同的结构:

  1. MyFormInputFilter
    1.1 NestedFielsetInputFilter
    1.2 AnotherFieldsetInputFilter

一些示例代码:

class ExampleForm extends Form 
{ 
    public function __construct($name, $options) 
    { 
     // handle the dependencies 
     parent::__construct($name, $options); 

     $this->setInputFilter(new ExampleInputFilter()); 
    } 

    public function init() 
    { 
     // some fields within your form 

     $this->add(new SomeFieldset('SomeFieldset')); 
    } 
} 

class SomeFieldset extends Fieldset 
{ 
    public function __construct($name = null, array $options = []) 
    { 
     parent::__construct($name, $options); 
    } 

    public function init() 
    { 
     // some fields 
    } 
} 

class ExampleInputFilter extends InputFilter 
{ 
    public function __construct() 
    { 
     // configure your validation for your form 

     $this->add(new SomeFieldsetInputFilter(), 'SomeFieldset'); 
    } 
} 

class SomeFieldsetInputFilter extends InputFilter 
{ 
    public function __construct() 
    { 
     // configure your validation for your SomeFieldset 
    } 
} 

所以配置您的输入过滤针对这些情况的重要组成部分,是需要重用你的字段集的名称使用时:$this->add($input, $name = null)在你的InputFilter类。

+0

谢谢@Kwido,现在它工作正常。 – pandomic