我有一个带有复选框的表单,用于接受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
功能来显示个别错误
答案 0 :(得分:9)
我会做这样的事情,
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;
}
答案 1 :(得分:0)
你可以把值= 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]');
&#13;