php - Validating Dynamically generated field arrays in codeigniter -
i have form fields 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 validate to[], from[] fields in codeignitor, use ajax call validate form here 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 write code in 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 fields... , validation_errors doesnt show thing tried parse json , echo json code... 'to' , 'from' shows empty in json. can help?
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 text box empty
the field field required.
2) if text boxes filled
success
Comments
Post a Comment