Search code examples
jquerylaravel-5bootstrapvalidator

Validate an array of inputs bootstrap validator - Laravel 5.2


I have problems to validate in the client-side an array of inputs. Validations do not look right. I think I'm doing the right thing. I'm using the plugin bootstrap validator and laravel 5.2

client-side validation photo:

js code:

$('#forma').bootstrapValidator({
        message: 'Valor no valido',
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        }, 
        fields: {
            'vtcanp[]': {
                validators: {
                    notEmpty: {
                        message: 'Codigo de moneda requerido'
                    },
                    stringLength: {
                        min: 3,
                        max: 4,
                        message: 'Código debe ser minimo de 3 y maximo 4 caracteres'


                    },
                }
            },
            'vtprep[]': {
                validators: {
                    notEmpty: {
                        message: 'Descripción de moneda requerido'
                    }

                }
            }




        }
    });

table view code:

<td width="17%">
                                        <input type="text" class="form-control" id="cantidad" name="vtcanp[]" /> 

                                    </td>
                                    <td width="17%">
                                        <input type="text" class="form-control" id="precio" name="vtprep[]"/>

                                    </td>

Now It's validating but one tr only. When I add more tr it' not validating.

Error tr


Solution

  • Try this:-

    <form id="formid">
    <div class="form-group" id="validatetr">
        <table>
            <tr>
                <td width="17%">
                <input type="text" class="form-control" id="cantidad" name="vtcanp[]" /> 
                </td>
                <td width="17%">
                <input type="text" class="form-control" id="precio" name="vtprep[]"/>
    
                </td>
            <tr>
        </table>
    </div>
    </form> //manage form according to your structure
    

    Now where you add dynamically tr in response add this

    var $template = $('#validatetr'),
    $clone    = $template
    $('#formid')
    .formValidation('addField', $clone.find('[name="vtcanp[]"]'))
    .formValidation('addField', $clone.find('[name="vtprep[]"]'))
    

    Also Replace top of the part from this:-

     $('#forma')
        .formValidation({
            framework: 'bootstrap',
            icon: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
    

    And this link might help you :- http://formvalidation.io/examples/adding-dynamic-field/

    Hope it helps