控制器功能:
function signup()
{
$bool1 = $this->form_validation->run('username');
$bool2 = $this->form_validation->run('email');
$bool3 = $this->form_validation->run('pass');
var_dump($bool1);
var_dump($bool2);
var_dump($bool3);
$this->load->view('myform');
}
视图代码:
<?php $this->load->view('includes/header'); ?>
<?php
$data_username = array(
'name' => 'username',
'onBlur' => 'username_val()',
'maxlength' => 50,
);
$data_email = array(
'name' => 'email',
'onBlur' => 'email_val()',
'maxlength' => 50
);
$data_pass = array(
'name' => 'pass',
'onBlur' => 'pass_val()',
'maxlength' => 50,
'autocomplete' => 'off'
);
?>
<div id="body">
<div id="bodycontent">
<table class="login_table">
<?php $attr = array( 'onsubmit' => 'return final_val()' ); echo form_open('signup',$attr); ?>
<tr><td>Username: </td><td><?php echo form_input($data_username); ?><span name="username_error" > <?php echo form_error('username'); ?></span></td></tr>
<tr><td>Email: </td><td><?php echo form_input($data_email); ?><span name="email_error"> <?php echo form_error('email'); ?></span></td></tr>
<tr><td>Password: </td><td><?php echo form_password($data_pass); ?><span name="pass_error"> <?php echo form_error('pass'); ?></span></td></tr>
<tr><td><input type="submit" value="SIGN UP!" name="signup"/></td></tr>
<?php echo form_close(); ?>
</table>
</div>
</div>
<?php $this->load->view('includes/footer'); ?>
表单验证配置:
<?php
$config = array(
'username' => array(
array(
'field' => 'username',
'label' => 'nama',
'rules' => 'required|min_length[1]|max_length[50]|strip_tags|encode_php_tags|htmlspecialchars'
)
),
'email' => array(
array(
'field' => 'email',
'label' => 'EMAIL',
'rules' => 'required|min_length[1]|max_length[50]|valid_email|strip_tags|encode_php_tags|htmlspecialchars'
)
),
'pass' => array(
array(
'field' => 'pass',
'label' => 'PASSWORD',
'rules' => 'required|min_length[6]|max_length[256]|strip_tags|encode_php_tags|htmlspecialchars'
)
),/*
'passconf' => array(
array(
'field' => 'passconf',
'label' => 'PASS_CONF',
'rules' => 'required|min_length[6]|max_length[32]|matches[pass]|strip_tags|encode_php_tags|htmlspecialchars'
)
),*/
'title' => array(
array(
'field' => 'title',
'label' => 'Title field',
'rules' => 'required|min_length[1]|max_length[60]|strip_tags|encode_php_tags|htmlspecialchars'
)
)
);
?>
Javascript 已关闭。
问题:一旦用户名字段被提交了任何值,都返回true。如果除了用户名字段之外的所有值都被赋予,则所有值都返回 false。
代码有什么问题?谢谢。
更新:
感谢@TheSwiftExchange 指出这一点:您不会通过验证运行每个变量-您通过验证一次运行整个表单。
因此,我将配置文件更改为:
$config = array(
'signup' => array(
array(
'field' => 'username',
'label' => 'nama',
'rules' => 'required|min_length[1]|max_length[50]|strip_tags|encode_php_tags|htmlspecialchars'
),
array(
'field' => 'email',
'label' => 'EMAIL',
'rules' => 'required|min_length[1]|max_length[50]|valid_email|strip_tags|encode_php_tags|htmlspecialchars'
),
array(
'field' => 'pass',
'label' => 'PASSWORD',
'rules' => 'required|min_length[6]|max_length[256]|strip_tags|encode_php_tags|htmlspecialchars'
)
)
)
现在效果很好。;)