2

我有一个带有复选框的表格,用于接受 TOS。问题是我需要为此复选框添加自定义错误,

    <form>

    <input type="text" name="fname" placeholder="Name" /><?php echo form_error('fname') ?>
    <input type="text" name="email" placeholder="Email" /><?php echo form_error('email') ?>
    <input type="text" name="password" placeholder="Password" /><?php echo form_error('password') ?>

    <input type="checkbox" name="accept_terms" value="yes" /> Accept TOS<br>
    <?php echo form_error('accept_terms') ?>

    </form>

PHP

<?php 

 $this->form_validation->set_rules('fname','First Name','trim|required|xss_clean');
 $this->form_validation->set_rules('email','Email','trim|required|xss_clean|valid_email');
 $this->form_validation->set_rules('password','Password','trim|required|xss_clean');
 $this->form_validation->set_rules('accept_terms','TOS','trim|required|xss_clean'); // Need to add custom error message 

if ( $this->form_validation->run() === TRUE ) {

}else{

}
?>

当用户没有选择TOS,那我就不得不说

请阅读并接受我们的条款和条件。

注意:我添加form_error了显示单个错误的功能

4

2 回答 2

10

我会做这样的事情,

if ($this->form_validation->run() === TRUE ) {
   if(!$this->input->post('accept_terms')){
      echo "Please read and accept our terms and conditions.";
      // Redirect
   }
}
else{

}

对于自定义消息,您可以调用自定义验证函数,例如,

$this->form_validation->set_rules('accept_terms', '...', 'callback_accept_terms');

然后在控制器中设置这个方法:

function accept_terms() {
    if (isset($_POST['accept_terms'])) return true;
    $this->form_validation->set_message('accept_terms', 'Please read and accept our terms and conditions.');
    return false;
}
于 2013-05-31T09:40:22.887 回答
0

您可以将 value=1 并使用 codeigniter 进行检查,如下所示:

<input type="checkbox" name="accept_terms" value="1" /> Accept TOS<br>
$this->form_validation->set_rules('accept_terms','TOS','trim|required|xss_clean|greater_than[0]');

]

于 2017-06-12T05:28:57.510 回答