【问题标题】:How to validate a form with radio group in CodeIgniter如何在 CodeIgniter 中使用单选组验证表单
【发布时间】:2015-07-02 00:56:26
【问题描述】:

我有一个表格,用于显示调查中的问题和答案。用户可以在 5 个单选按钮之间进行选择 - 选择问题的答案。它们是数组,键是question_id - 每个问题都有这 5 个单选按钮,它们在这个键上有所不同。你能帮我验证表单吗? 现在,如果您为其中一个问题选择答案,表单就会提交。只有当所有问题都有答案时,它才必须提交。这是我的看法:

<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>";
	echo validation_errors();
 $index = 0;
	foreach ($question as $row)
	{
		echo "<tr id='$index'>";
		$index++;
	?>
		
	   	<td> 
		<?php echo "$row->question"; ?><br/>
		<?php echo "<input type='hidden' name='question_id' value='$row->question_id' />"; ?>
		<?php 
		 
		$data=array(
			'name' =>  'answer['.$row->question_id.']',
			'value' => '5',
			'class' => 'answer'

		);

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

?>		

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

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

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

    		}
        }
    }

【问题讨论】:

    标签: arrays forms codeigniter validation


    【解决方案1】:

    两种方式:

    1) 在数据数组中设置所需的属性,如

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

    2) 为每个 answer[$question_id] 变量设置表单验证。

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

    我会坚持使用 1。

    【讨论】:

    • 谢谢!它有效,我使用了第二个,因为它给了我第一个示例的 question_id 未定义。我在控制器中编写表单验证并使用示例 2 并在那里编写查询 $questions。 :)
    • 很高兴我能帮上忙!并且未定义?你还在传递你的其他价值观吗?我很想知道第一个不适合你的地方。
    • 因为在我编写表单验证的那个函数中,我没有这个查询,这就是'question_id'未定义的原因。我补充说: $query = $this->user_model->getSurvey($this->uri->segment(3), $this->uri->segment(4)); $问题= $查询->结果(); foreach($questions as $question){ $this->form_validation->set_rules('answer['.$question->question_id.']', '问题'.$question->question_id, '必需'); } 现在好了。 :)
    • 哦,不不不。我的意思是您在视图中传递给 form_radio 的 $data 数组为 &lt;?php echo form_radio($data); ?&gt; 很抱歉造成混淆。
    • 太棒了!那将是最好的处理方式。 :)
    猜你喜欢
    • 1970-01-01
    • 2012-12-06
    • 1970-01-01
    • 2016-12-14
    • 2012-12-19
    • 1970-01-01
    • 1970-01-01
    • 2011-10-24
    相关资源
    最近更新 更多