2013-04-09 33 views
0

我有一個窗體,其中一些字段是動態生成的。驗證在codeigniter動態生成的字段數組

<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"/> 

我想驗證所有爲[],從codeignitor []字段, 我使用Ajax調用來驗證這裏的形式是碼:

$('#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);}); 

我試着寫這個在控制器代碼:

$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(); 

    } 

它不驗證我的場......和VALIDATION_ERRORS犯規節目我試圖把它解析爲JSON任何東西,呼應JSON代碼...的「到」和「從」秀在json中空蕩蕩的。 任何人都可以幫忙嗎?

回答

0

樣品控制器:

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'); 
    } 

樣品視圖:

<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> 

//輸出

1)如果任何文本框是空

The From field field is required. 

2)如果所有文本框填充

success 
+0

does not work ...: -/ – 2013-04-09 06:24:50

+0

我測試n工作正常。你可以檢查嗎? – Dino 2013-04-09 06:33:12