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

How to set required validation for inner field of repeater in OctoberCMS?

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

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'
    ];

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

您可以在模型文件中添加以下代码

 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';
         }

除了使用 beforeValidate() 函数并循环遍历中继器之外,还有更方便的方法来完成它 - 通过在规则部分使用通配符:

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