How to validate values within an array with Validator?

1

I have the following javascript: arreglos_grados fix, which implements it in the following way: arreglos_grados = [] but I would like to validate with the Validator of Laravel 5.5 if a field that I am inserting in the javascript fix is already registered . I am sending my fix to the controller in the following way.

$('#agregar_grado').click(function (e) {

            e.preventDefault();

           var grado = {
                id_count: 'id_' + count_grados,
                grados: $('#grados option:selected').val(),
                fecha_examen_grado: $("#fecha_examen_grado").val(),
                fecha_obtencion_grado: $('#fecha_obtencion_grado').val(),
             };

             arreglos_grados.push(grado);
             count_grados++;


            $.ajax({
                type: 'POST',
                url: '{{ route('regMiembros.agregargrados') }}',
                data: {
                    arreglos_grados.length ? arreglos_grados: null
                },

                success: function (data) {

                    if ($.isEmptyObject(data.errors)) {


                        $.smkAlert({
                            text: data.success,
                            type: 'success'
                        });

                        limpiarErroresGrados();

                        $('#lista_grados').append('<tr id="' + grado.id_count + '"><td style="text-align: center">' + $('#grados option:selected').text() + '</td><td style="text-align: center">' + $('#fecha_obtencion_grado').text() + '</td><td style="text-align: center">' + $('#fecha_examen_grado').text() + '</td><td style="text-align: center;"><button class="glyphicon glyphicon-trash" onclick="eliminarGrado(\'' + grado.id_count + '\');"></buton></td></tr>');

                    } else {

                        limpiarErroresGrados();

                        $.each(data.errors, function (index, value) {
                            $('#_' + index).text(value);
                        });

                        $.smkAlert({
                            text: "Existen errores de validación en los grados masónicos, por favor revise ",
                            type: 'danger'
                        });
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    arreglos_grados.splice(arreglos_grados.length - 1, 1);
                    alerta(jqXHR.responseText);
                }
            });
        });

In my controller I have raised my Validator in the following way in my function of controller

$validator = Validator::make($request->all(), $rules, $messages, $attributes);

How can I validate elements that are within that array?

    
asked by albertolg89 13.08.2018 в 16:27
source

1 answer

1

According to the documentation :

  

Validation of Arrays

     

Validating an array based on fields in a form should not be complicated. You can use "dot notation"   to validate attributes that are array. For example, if the request   HTTP contains in field photos[profile] , you can validate the   following way:

$validator = Validator::make($request->all(), [
    'photos.profile' => 'required|image',
]);
     

You can also validate each element of an array . For example,   to validate that each email in a given field of   array is unique, you can do the following:

$validator = Validator::make($request->all(), [
    'person.*.email' => 'email|unique:users',
    'person.*.first_name' => 'required_with:person.*.last_name',
]);
     

In the same way, you can use the * character to specify the   validation messages in the language files, so it's very   easy to use a single validation message for fields based on   array:

'custom' => [
    'person.*.email' => [
        'unique' => 'Each person must have a unique e-mail address',
    ]
],
    
answered by 13.08.2018 в 17:03