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

- 1 answer

Ad

form_image

I have tried below code but it's not working for me.

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

Model

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

Please Share your knowledge with me if anyone has the solution for it. Thanks

Ad

Answer

Ad

You can add Below code in model file

 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';
         }
Ad
source: stackoverflow.com
Ad