2

I am passing array input to store function .

  <div class="col-md-4 form-group-sub mt-2 date_">
   <input type="text" name="date[]" class="form-control datepicker" placeholder="Date">
  </div>
                                               

this array should be have atleast one field filled . If all the values are null i don't to submit this form how shoult i validate in request

public function rules()
    {
        return [
           
            'date.*' => '?'

        ];
    }

2 Answers 2

1

I suppose you can try to make sure date is an array with a minimum size and then try to validate the elements of it:

'date' => 'array|min:1',
'date.*' => 'filled',

Laravel 7.x Docs - Validation - Available Rules array

Laravel 7.x Docs - Validation - Available Rules min

Laravel 7.x Docs - Validation - Available Rules filled

Sign up to request clarification or add additional context in comments.

Comments

0

this array should be have atleast one field filled . If all the values are null i don't to submit this form

This will pass
['some date', NULL, NULL, NULL]


This will fail
[NULL, NULL]

This will fail
[]

Anything other than array will fail.

Use a combination of array, min:1 and a custom rule that checks the null condition.

https://laravel.com/docs/7.x/validation#rule-array

https://laravel.com/docs/7.x/validation#rule-min

https://laravel.com/docs/7.x/validation#custom-validation-rules

public function rules()
    {
        return [
            'date' => ['array', 'min:1', new DateCheck]
        ];
    }

DateCheck rule will check, that there should be at least one element in the array, that is not null.

Inside DateCheck Rule

    public function passes($attribute, $value)
    {
        $dates = $value;
        foreach($dates as $date){
           if(isset($date)){
               return TRUE;
           }                      
        }
        return FALSE;
    }

     public function message()
    {
        return 'There should be at least one date that is not NULL.';
    }


Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.