Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i have a form where some fields are dynamically generated.

<table class="insideform">
                            <tr>
                                <td>
                                <script>
                                    $(document).ready(function() {
                                        $('#addrange').click(function(){
                                            var value = '<tr><td><input type="number" size="10" id="from" name="from[]" value=""></td>';
                                                value += '<td><input type="text" size="10" id="to" name="to[]" value=""></td>';
                                                value += '<td><input type="text" id="disprice" name="disprice[]" /></td>';
                                                value += '<td valign="middle" id="removerange">x</td>';
                                                value += '<td id="to_err" class="err"></td></tr>';

                                            $('.discounttable').append(value);
                                        });

                                        $('body').on("click","#removerange",function(){
                                            $(this).parent().remove();
                                        });

                                        $('#maxqty').change(function(){
                                            var value = $('#maxqty').val() + " Above";
                                            $('#maxabove').text(value);
                                        });

                                    });
                                </script>
                                    <table class="discounttable">
                                        <tr>
                                            <th>From</th>
                                            <th>To</th>
                                            <th>Price</th>
                                        </tr>
                                        <tr>
                                            <td colspan="2" align="right"><span id="maxabove">10 Above</span></td>
                                            <td><input type="text" name="maxaboveinput" id="maxaboveinput" /></td>
                                            <td id="maxaboveinput_err" class="err"></td>
                                        </tr>
                                        <tr>
                                            <td><input type="text" size="10" id="from" name="from[]" value=""></td>
                                            <td><input type="text" size="10" id="to" name="to[]" value=""></td>
                                            <td><input type="text" /></td>
                                            <td valign="middle" id="removerange">x</td>
                                            <td id="to_err" class="err"></td>
                                        </tr>
                                    </table> <input type="button" name="addrange" id="addrange" value="Add Row"/>

i want to validate all the to[], from[] fields in codeignitor, i use ajax call to validate the form here is the code:

$('#submit').click(function(){
        console.log($("#form").serialize());
        $.ajax({
                url:'<?php echo base_url(); ?>index.php/placeorder/valids',
                type:'POST',
                data:$("#form").serialize()
                }).done(function(data){
                    $("#validations").html(data);});

i tried to write this code in the controller:

$this->load->helper(array('form', 'url'));
 $this->load->library('form_validation');
 $this->form_validation->set_message('%s required', '*required');



 $this->form_validation->set_rules('to[]', 'To field', 'required|xss_clean');
$this->form_validation->set_rules('from[]', 'From field', 'required|xss_clean');

 $errors = array();

 if ($this->form_validation->run() == FALSE)
    {
        echo validation_errors();

    }

it doesnt validate my fields... and validation_errors doesnt show any thing i tried to parse it to json and echo the json code... the 'to' and 'from' shows empty in json. can anybody help?

share|improve this question

1 Answer 1

Sample controller :

  public function sample_controller() {

    if ($this->input->post()) {
      $this->load->library('form_validation');
      $this->form_validation->set_rules('from[]', 'From field', 'required|xss_clean');
      if ($this->form_validation->run() == TRUE) {
        echo 'success';
      } else {
        echo validation_errors();
      }
    }
    $this->load->view('sample_view');
  }

Sample view :

<form method="post">
  <input type="text" size="10" id="from" name="from[]" value="dino"/>
  <input type="text" size="10" id="from" name="from[]" value="babu"/>
  <input type="text" size="10" id="from" name="from[]" value="kannampuzha"/>
  <input type="submit" value="Go" />
</form>

// Output

1) if any text box is empty

The From field field is required.

2) if all text boxes filled

success
share|improve this answer
    
doesnt work... :-/ –  Wasif Khalil Apr 9 '13 at 6:24
    
i tested n works fine. can u pls check ? –  Dino Apr 9 '13 at 6:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.