Home > OS >  Conditional validation not working properly
Conditional validation not working properly

Time:03-18

I'm trying to set a conditional validation which must check this:

  • If riskfield->type is equals to min_max then the values[min] and values[max] must be provided.
  • If riskfield->type is equals to number then values[number] must be provided

So in my blade view:

@if ($riskfield->type == 'min_max')
<x-boilerplate::input type="number" name="values[min]" label="medicalrecords.min" required
    placeholder="medicalrecords.min_plc" autofocus="true" min="0" prepend-text="fas fa-signature">
</x-boilerplate::input>

<x-boilerplate::input type="number" name="values[max]" label="medicalrecords.max"
    placeholder="medicalrecords.max_plc" autofocus="true" min="0" prepend-text="fas fa-signature">
</x-boilerplate::input>
@elseif($riskfield->type == 'number')
<x-boilerplate::input type="number" name="values[number]" label="medicalrecords.number" required
    placeholder="medicalrecords.number_plc" autofocus="true" min="0"
    prepend-text="fas fa-signature">
</x-boilerplate::input>
@endif

and also at the top of my form I have an hidden field:

<input type="hidden" name="riskfield_type" value="{{ $riskfield->type }}">

Then in the store method there is this validation config:

    $this->validate($request, [
        'riskfield_id'      => 'required',
        'riskarea_id'       => 'required',
        'values[min]'       => 'required_if:riskfield_type,==,min_max',
        'values[max]'       => 'required_if:riskfield_type,==,min_max',
        'values[number]'    => 'required_if:riskfield_type,==,number'
    ]);

Now the problem's that if I fill both values[min] and values[max] I get:

The values [min] field is required when the riskfield type is min_max.

Is this a bug or I did something wrong?

CodePudding user response:

Try to write validation like this:

'values.min' => 'required_if:riskfield_type,==,min_max',
  • Related