gpt4 book ai didi

php - 复选框的 CodeIgniter 表单验证规则

转载 作者:行者123 更新时间:2023-12-02 06:18:53 25 4
gpt4 key购买 nike

我有一个带有复选框的表单,用于接受 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,那我不得不说

Please read and accept our terms and conditions.

注意:我添加了 form_error 函数来显示个别错误

最佳答案

我会做这样的事情,

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

然后在 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;
}

关于php - 复选框的 CodeIgniter 表单验证规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16854232/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com