2015-04-23 64 views
1

我有一個表單,用於顯示調查中的問題和答案。用戶可以選擇5個單選按鈕 - 選擇問題答案。他們是陣列,關鍵是question_id - 每個問題都有這5個單選按鈕,它們在這個鍵上有所不同。你能幫我進行表單驗證嗎? 現在,如果您選擇其中一個問題的答案,請提交表單。只有在所有問題都有答案的情況下才能提交。這是我的觀點:如何使用CodeIgniter中的廣播組驗證表單

<html> 
 
    <head></head> 
 
    <body> 
 
    <?php 
 
$survey_id = $this->uri->segment(3); 
 
$question_id = $this->uri->segment(4); 
 
$att=array('id'=>'form'); 
 
echo form_open('index/survey_fill/' .$survey_id .'/'. $question_id , $att); 
 
    echo "<table border="0" id='questionsTable' >"; 
 
echo "<tr><th>Въпрос</th></tr>"; 
 
\t echo validation_errors(); 
 
$index = 0; 
 
\t foreach ($question as $row) 
 
\t { 
 
\t \t echo "<tr id='$index'>"; 
 
\t \t $index++; 
 
\t ?> 
 
\t \t 
 
\t  \t <td> 
 
\t \t <?php echo "$row->question"; ?><br/> 
 
\t \t <?php echo "<input type='hidden' name='question_id' value='$row->question_id' />"; ?> 
 
\t \t <?php 
 
\t \t 
 
\t \t $data=array(
 
\t \t \t 'name' => 'answer['.$row->question_id.']', 
 
\t \t \t 'value' => '5', 
 
\t \t \t 'class' => 'answer' 
 

 
\t \t); 
 

 
\t \t echo "<input type='hidden' name='survey_id' value='$row->survey_id'>"; 
 
\t \t 
 
\t \t echo form_radio($data); 
 
\t \t echo " 5 "; 
 
\t \t $data=array(
 
\t \t \t 'name' => 'answer['.$row->question_id.']', 
 
\t \t \t 'value' => '4', 
 
\t \t \t 'class' => 'answer' 
 
\t \t); 
 
\t \t echo form_radio($data); 
 
\t \t echo " 4 "; 
 
\t \t $data=array(
 
\t \t \t 'name' => 'answer['.$row->question_id.']', 
 
\t \t \t 'value' => '3', 
 
\t \t \t 'class' => 'answer' 
 
\t \t \t 
 
\t \t); 
 
\t \t echo form_radio($data); 
 
\t \t echo " 3 "; 
 
\t \t $data=array(
 
\t \t \t 'name' => 'answer['.$row->question_id.']', 
 
\t \t \t 'value' => '2', 
 
\t \t \t 'class' => 'answer' 
 
\t \t); 
 
\t \t echo form_radio($data); 
 
\t \t echo " 2 "; 
 
\t \t $data=array(
 
\t \t \t 'name' => 'answer['.$row->question_id.']', 
 
\t \t \t 'value' => '1', 
 
\t \t \t 'class' => 'answer' 
 
\t \t); 
 
\t \t echo form_radio($data); 
 
\t \t echo " 1 "; 
 
\t \t ?> 
 
\t \t </td></tr> 
 
    
 
\t <?php 
 
\t } 
 

 
?> \t \t 
 

 
\t 
 
</table> 
 

 
<?php echo "<input type='hidden' name='question_id' value='$row->question_id' />"; ?> 
 
    <?php echo '<input type="submit" id="button" name = "submit" value="Submit" class="btn btn-success">'; 
 
    ?> 
 

 
</form> 
 
</div> 
 
    
 
</body> 
 
</html>

我的控制器:

public function survey_fill() 
 
    { 
 
    
 
     $this->form_validation->set_rules('answer[]', 'Answer', 'required');  
 
    
 
     if ($this->form_validation->run()==FALSE) 
 
     { 
 

 
      $this->survey_show(); 
 
     } 
 
     else 
 
     { 
 

 
\t  \t if ($this->user_model->survey_fill()) 
 
\t   { 
 
       
 
       header('Refresh: 2; url=/survey/index.php/index/surveys_show'); \t   \t 
 
\t   } 
 

 
      else 
 
      { 
 
       $this->load->model('user_model'); 
 
       $data['survey'] =$this->user_model->survey_show(); 
 
       $data['dynamic_view'] = 'survey_show'; 
 
       $data['menu']=$this->menu_model->get_menu(); 
 
       $this->load->view('templates/main',$data); 
 

 
    \t \t } 
 
     } 
 
    }

回答

1

兩種方式:

1)設置所需要的屬性,在DA ta array like

$data = array('name' => 'answer['.$row->question_id.']', 'required' => 'required'); 

2)爲每個答案[$ question_id]變量設置表單驗證。

foreach($questions as $question){ 
    $this->form_validation->set_rules('answer['.$question->id.']', 'Question '.$question->id, 'required'); 
} 

我會堅持與1

+1

謝謝!它起作用了,我用了第二個,因爲它給了我第一個例子中的undefined for question_id。我在控制器中編寫表單驗證並使用示例2並在那裏寫入query $ questions。 :) –

+0

很高興我能幫忙!還有未定義?你還在傳遞你的其他價值觀嗎?我有興趣知道什麼對你來說第一個沒用。 –

+0

Becaurse在我寫表單驗證的函數中,我沒有這個查詢,這就是爲什麼'question_id'是未定義的。我添加了:$ query = $ this-> user_model-> getSurvey($ this-> uri-> segment(3),$ this-> uri-> segment(4)); $ questions = $ query-> result(); foreach($ questions as $ question){ $ this-> form_validation-> set_rules('answer ['。$ question-> question_id。']','Question'。$ question-> question_id,'required') ; }現在沒關係。 :) –