CodeIgniter表单验证规则复选框 [英] CodeIgniter form validation rules for checkbox
本文介绍了CodeIgniter表单验证规则复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有用于接受TOS的复选框的表单。问题是我需要为此复选框添加自定义错误,
I've a form with checkbox for accepting TOS. The problem is I need to add custom error for this checkbox,
<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时,我必须说
When the user does not select TOS, Then I have to say
请阅读并接受我们的条款及细则。
Please read and accept our terms and conditions.
注意: ve添加 form_error
函数以显示单个错误
Note: I've added form_error
function to display individual errors
推荐答案
这样,
if ($this->form_validation->run() === TRUE ) {
if(!$this->input->post('accept_terms')){
echo "Please read and accept our terms and conditions.";
// Redirect
}
}
else{
}
对于自定义消息,您可以调用自定义验证函数,如
And for custom message you can call a custom validate function like,
$this->form_validation->set_rules('accept_terms', '...', 'callback_accept_terms');
然后在控制器中设置此方法:
And then set up this method in the controller:
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;
}
这篇关于CodeIgniter表单验证规则复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文