|
发表于 2012-10-22 16:21:01
|
显示全部楼层
表单验证可以分两种情况,一种直接在前台进行js表单验证,可以用jquery ui validate表单验证插件,另一种可以通过ci中form表单验证类进行所谓的后台验证
表单验证要么全在前端js验证,要么进行表单验证类验证,不要两种混合验证。
举例:form表单验证类
$this->load->library('form_validation');
$this->form_validation->set_rules('p_name', "Product Name", "trim|required|valid_string");
$this->form_validation->set_rules('o_price', "Original Price", "trim|required|valid_string");
$this->form_validation->set_rules('s_price', "Sale Price", "trim|required|valid_string");
if ($this->form_validation->run() == false) {
$this->form_validation->set_error_delimiters();
$data["msg"] = validation_errors();
$this->load->view("admin/products/productItem", $data,true);
}
渲染到视图层的部分代码:
.....
<?php
//print_error($error_url);
//if(validation_errors()) echo validation_errors();
if ($msg)
print_msg($msg);
print_msg( $this->session->flashdata('item'));
?>
<p>
<input type="button" class="input-submit-02" value="Back to Product List" name="back" onclick="window.location.href='<?php echo site_url('admin/products'); ?>'" />
</p>
<form method="post" action="" enctype="multipart/form-data" onsubmit="check_selected();" >
<table width="100%">
<tr>
<td class="right">Category</td>
<td>
<select name="p_cid" id="p_cid">
<?php echo $this->categories_model->get_cateSelect(0, 1, $item['p_cid']); ?>
</select>
</td>
</tr>
<tr>
<td class="right">Product Name</td>
<td >
<?php echo form_input('p_name', $item['p_name'], 'class="input-text"'); ?><span style="color:red"><?php echo form_error('p_name'); ?></span>
<?php echo $newid; ?>
</td>
</tr>
...... |
|