如何在OctoberCMS中为转发器的内部字段设置所需的验证?

时间:2016-09-16 13:03:40

标签: validation octobercms

form_image

我试过下面的代码,但它对我不起作用。

fields.yaml

fields:
    repetor_name:
        label: Repeater
        prompt: 'Add new item'
        span: auto
        type: repeater
        required: 1
        form:
            fields:
                required_text:
                    label: Text
                    span: auto
                    required: 1
                    type: text

模型

   public $rules = [
   'repetor_name' => 'required',
   'required_text' =>'required'
    ];

如果有人有解决方案,请与我分享您的知识。感谢

2 个答案:

答案 0 :(得分:1)

您可以在模型文件

中添加以下代码
 public $rules = [
       'repetor_name' => 'required', //repeater field is required
      // 'required_text' =>'required' You have to remove this line
        ];  



/* We have to create custom function in model to validate inner fields of  repeater*/


public function beforeValidate()
        {
         foreach ($this->repetor_name as $key => $value) {
         $this->rules['repetor_name.'.$key.'.required_text'] = 'required';
         }

答案 1 :(得分:1)

与其使用beforeValidate()函数并在转发器之间循环,不如通过在规则部分中使用通配符来实现此功能,这是更方便的方法:

public $rules = [
   'repetor_name' => 'required',
   'repetor_name.*.required_text' =>'required'
];