jeudi 27 septembre 2018

Laravel validate required_if when current input equals to a value that is inside an array (checkbox with input text)

I got a form with a list of checkboxes. The last one says "other", when clicked, an input text is enabled.

I have this rule where the user can check up to three options.

As you already know, checkboxes are stored in an array.

Should the user check on "other" option, without typing in the input, I want to prompt the user through an error message (validation) that they need to type in the input text, as well.

Here is options_list.blade.php view:

@section('content')
    @if($errors->any())
        <div class="alert alert-danger" role="alert">
            <strong><i class="fas fa-exclamation-triangle"></i>&nbsp;Warning</strong>: The following errors have been found:
            <ul>
                @foreach($errors->all() as $error)
                    <li></li>
                @endforeach
            </ul>
        </div>
    @endif
    <div class="card">
        <div class="card-body">
            <div class="shadow p-3 mb-5 bg-white rounded">
                <p class="h6">
                    Here goes the question text
                </p>
                <p class="text-primary">You can choose up to three options</p>
            </div>
            <div class="shadow">
                <form action="" method="post" id="myForm">
                    <div class="col-lg">
                        @foreach($lineasdeinvestigacion as $lineadeinvestigacion)
                            <div class="custom-control custom-checkbox my-1 mr-sm-2">
                                <input type="checkbox" class="custom-control-input" id="customControlInline" name="lineasdeinvestigacion[]" value="" >
                                <label class="custom-control-label" for="customControlInline"></label>
                            </div>
                        @endforeach
                            <div class="custom-control custom-checkbox my-1 mr-sm-2">
                                <input type="checkbox" class="custom-control-input" id="customControlInlineOtro" name="lineasdeinvestigacion[]" value="other" >
                                <label class="custom-control-label" for="customControlInlineOtro">Other</label>
                                <input placeholder="" type="text" class="form-control form-control-sm" id="fortalecer_otro" name="fortalecer_otro" maxlength="255" value="" disabled>
                            </div>
                            @include('path.to.partials.buttons._continue')
                    </div>
                </form>
            </div>
        </div>
    </div>
@endsection

And here is the optionsController.php:

public function store(Token $token, Request $request){

        //dd($request->lineasdeinvestigacion);

        //Validating input data
        $this->validate($request,[
            'lineasdeinvestigacion'  =>  'nullable|max:3',
            'fortalecer_otro'        =>  'required_if:lineasdeinvestigacion.*,other|max:255',
        ],[
            'lineasdeinvestigacion.max' => 'You cannot choose more than :max options.',
        ]);
}

This is the array of values chosen from the checkboxes list (dd($request->lineasdeinvestigacion);):

array:4 [▼
  0 => "Procesos socio-culturales"
  1 => "Ciencia, Innovación tecnológica y Educación"
  2 => "Nuevas formas de movilidad"
  3 => "other"
]

However, the validation is not working as it should, as it allows the input text #fortalecer_otro to be empty, when the "other" checkbox option is checked.

How do I fix this? Any ideas?




Aucun commentaire:

Enregistrer un commentaire