0
votes

I like to ask if anyone knows how to validate 3 fields but you must fill only one, not the other 2.

Example :

<input name="field1" type="numeric">
<input name="field2" type="numeric">
<input name="field3" type="numeric">

field1,field2,field2 can be filled but if i fill field1, field2 and field3 passes, but if i fill field1 and field2, validation triggered and only one of them can be filled.

Thanks

3
is this in the view.blade.php or in the controller?Udhayan Nair
Please make rules and use this code "Validator::make(Input::all(), $rules)". Please follow this link for more help laravel.com/docs/6.x/validation#rule-required-without-allRockers Technology
I tested that , but let me fill all the fields ..Sebastiangperez

3 Answers

1
votes

try this:

Controller

public function store() {
  User::create(request()->validate([
    'first_name' => 'required|min:3',
    'middle_name' => 'nullable',
    'last_name' => 'nullable',
  ]))
}

EDIT

public function store(Request $request) {
  Validator::make($request()->all, [
    'number_1' => Rule::requiredIf(!$request->number_2 && !$request->number_3);
    'number_2' => Rule::requiredIf(!$request->number_1 && !$request->number_3);
    'number_3' => Rule::requiredIf(!$request->number_1 && !$request->number_2);
  ])
}
0
votes

Put the must filled field as required.For others just put the validation rule,don't put required rule..

 $this->validate($request, [
    'first_name' => 'required|string|max:255',
    'second_name' => 'string|max:255',
    'email' => 'required|string|email|max:255|unique:users',
    ]);
0
votes

sometimes will validate if it passed

'field1' => 'sometimes|required|max:255',
'field2' => 'sometimes|required|max:255',
'field3' => 'sometimes|required|max:255',