Welcome Guest, Not a member yet? Register   Sign In
Array input validation help
#1

I am working on this application which i need to achieve something like this, in the system we are allowing the system administrator to add customers and their vehicles,

Customers can have one or more vehicles, by default there are 8 fields to add the information of customer's vehicle.


here are the fields for first entry. 

Code:
<input type="text" name="number[0]" id="number" class="form-control name_list"/>

<input type="text" name="make[0]" id="make"  class="form-control name_list" />

<input type="text" name="model[0]" placeholder="" class="form-control name_list" />

<input type="text" name="chassis[0]" placeholder="" class="form-control name_list" />

<input type="text" name="engine_no[0]" placeholder="" class="form-control name_list" />

<input type="text" name="color[0]" placeholder="" class="form-control name_list" />

<input type="text" name="tyre[0]" placeholder="" class="form-control name_list" />

<input type="text" name="battery[0]" placeholder="" class="form-control name_list" />

and my interface is like this.

[Image: sBIpkVa.jpg]
by clicking "Add another" user will get another set of same fields (same 8 default fields ) field names  of this set will be  like with array[1]

Code:
<input type="text" name="number[1]" id="number" class="form-control name_list"/>

<input type="text" name="make[1]" id="make"  class="form-control name_list" />

.......

note that the index of the array changes with the each vehicle, if a person have 5 vehicles then the number field will be like number[0],number[1], number[2], number[3], number[4], number[5] same will be for other 7 fields.

i am using following jquery  & codeigniter validation rules for validation.

here's my jquery script.

Code:
$( document ).ready(function() {

$('#form-user').submit(function(e) {
        e.preventDefault();

        var me = $(this);

        // perform ajax
        $.ajax({
            url: me.attr('action'),
            type: 'post',
            data: me.serialize(),
            dataType: 'json',
            success: function(response) {
                if (response.success == true) {
                    // if success we would show message
                    // and also remove the error class
                
                    $('.form-group').removeClass('has-error')
                                    .removeClass('has-success');
                    $('.text-danger').remove();

                    // reset the form
                    me[0].reset();
                    window.location.href = "customers";

                    // close the message after seconds
                    $('.alert-success').delay(500).show(10, function() {
                        $(this).delay(3000).hide(10, function() {
                            $(this).remove();
                            //window.location.href = "http://localhost/motorlinknew/customers";
                        });
                    })
                }
                else {
                    $.each(response.messages, function(key, value) {
                        var element = $('#' + key);
                        
                        element.closest('div.form-group')
                        .removeClass('has-error')
                        .addClass(value.length > 0 ? 'has-error' : 'has-success')
                        .find('.text-danger')
                        .remove();
                        
                        element.after(value);
                    });
                }
            }
        });
    });

});
this script validates the page without refreshing the page. thing is with this script the input fields name & id attributes has to be the same.

ex: name="number[0]" id="number[b][0]" [/b]

but

when use [b]name="number[0]" id="number[b][0]" [/b][/b]  the validations not working. 

even i tired 

$this->form_validation->set_rules('number[]', 'Number', 'required|min_length[7]');
[b]$this->form_validation->set_rules('number[0]', 'Number', 'required|min_length[7]');[/b]

[b]no luck.[/b]

please help me here. i have a deadline to meet. all i need is validate first basic 8 fields and the ones users adding using "Add another" button
Reply


Messages In This Thread
Array input validation help - by greenarrow - 07-28-2016, 09:34 AM
RE: Array input validation help - by mwhitney - 07-28-2016, 10:42 AM
RE: Array input validation help - by greenarrow - 07-28-2016, 11:31 AM
RE: Array input validation help - by mwhitney - 07-28-2016, 01:27 PM
RE: Array input validation help - by greenarrow - 07-29-2016, 02:26 AM
RE: Array input validation help - by mwhitney - 07-29-2016, 06:22 AM
RE: Array input validation help - by PaulD - 07-28-2016, 12:03 PM
RE: Array input validation help - by greenarrow - 07-28-2016, 12:30 PM
RE: Array input validation help - by greenarrow - 07-28-2016, 11:19 PM
RE: Array input validation help - by dave friend - 07-29-2016, 07:57 AM
RE: Array input validation help - by PaulD - 07-29-2016, 08:17 AM



Theme © iAndrew 2016 - Forum software by © MyBB