CodeIgniter类库之Validation Class(续)

在之前的一篇文章《CodeIgniter类库之Validation Class 》向大家介绍过CodeIgniter的Validation类库用法,但并不是很全面,现在对其进行部分补充。

Validation可以对提交的表单中的数据进行验证,当验证失败后会返回到表单让用户重新录入。当需要用户录入很多数据的时候,我们就希望能够的验证失败后,在表单中保留那些正确的数据,用户只要重新录入不符合验证规则的数据即可。

我们可以使用以下方法来解决这个问题(注意粗体字部分)。

< form method ="POST" action ="<?phpechosite_url('VisitManage/post_booking')?>" >
< div style ="padding:008px0;" >
< h3 > 来访者信息 </ h3 >
< table >
< tr >
< td >< span class ="label" > 访客姓名 </ span ></ td >
< td >
< input type ="text" name ="vist_name" value ="<?phpecho(isset($this->validation->vist_name)?$this->validation->vist_name:'');?>" class ="inputtext" style ="width:140px" onblur ="this.className='inputtext';" onfocus ="this.className='activeinputtext';" />
< span >< font style ="color:red" > * </ font ></ span >
< input type ="button" class ="inputtext" value ="历史访客查询" style ="width:80px" />
</ td >
< td >< span class ="label" > 手机号码 </ span ></ td >
< td >< input type ="text" name ="vist_mobile" value ="<?phpecho(isset($this->validation->vist_mobile)?$this->validation->vist_mobile:'');?>" class ="inputtext" style ="width:140px" maxLength ="11" onblur ="this.className='inputtext';" onfocus ="this.className='activeinputtext';" /></ td >
< td >< span class ="label" > 来访类型 </ span ></ td >
< td >
< SELECT name ="vist_kind" class ="inputtext" style ="width:140px" >
< OPTION VALUE ="客户" <?phpecho$this- > validation->set_select('vist_kind','客户');?>>客户 </ OPTION >
< OPTION VALUE ="VIP客户" <?phpecho$this- > validation->set_select('vist_kind','VIP客户');?>>VIP客户 </ OPTION >
< OPTION VALUE ="总部人员" <?phpecho$this- > validation->set_select('vist_kind','总部人员');?>>总部人员 </ OPTION >
< OPTION VALUE ="协作单位" <?phpecho$this- > validation->set_select('vist_kind','协作单位');?>>协作单位 </ OPTION >
< OPTION VALUE ="供应商" <?phpecho$this- > validation->set_select('vist_kind','供应商');?>>供应商 </ OPTION >
< OPTION VALUE ="承包商" <?phpecho$this- > validation->set_select('vist_kind','承包商');?>>承包商 </ OPTION >
< OPTION VALUE ="政府人员" <?phpecho$this- > validation->set_select('vist_kind','政府人员');?>>政府人员 </ OPTION >
< OPTION VALUE ="其他" <?phpecho$this- > validation->set_select('vist_kind','其他');?>>其他 </ OPTION >
</ SELECT >
< span >< font style ="color:red" > * </ font ></ span >
</ td >
</ tr >
< tr >
< td >< span class ="label" > 来访日期 </ span ></ td >
< td >
< input type ="text" name ="vist_date" value ="<?phpecho(isset($this->validation->vist_date)?$this->validation->vist_date:'');?>" class ="inputtext" style ="width:140px" maxLength ="12" onblur ="this.className='inputtext';" onfocus ="this.className='activeinputtext';" />
< span >< font style ="color:red" > * </ font ></ span >
< input type ="button" class ="inputtext" value ="日期选择" style ="width:80px;" />
</ td >
< td >< span class ="label" > 来访人数 </ span ></ td >
< td >
< input type ="text" name ="vist_num" value ="<?phpecho(isset($this->validation->vist_num)?$this->validation->vist_num:'');?>" class ="inputtext" style ="width:140px" onblur ="this.className='inputtext';" onfocus ="this.className='activeinputtext';" />
< span >< font style ="color:red" > * </ font ></ span >
</ td >
< td >< span class ="label" > 进入车间 </ span ></ td >
< td >< input type ="checkbox" name ="vist_workshop" value ="1" <?phpecho$this- > validation->set_checkbox('vist_workshop','1');?>style="margin:000-3px;"/> </ td >
</ tr >
< tr >
< td >< span class ="label" > 来访公司 </ span ></ td >
< td colspan ="5" >
< input type ="text" name ="vist_corp" value ="<?phpecho(isset($this->validation->vist_corp)?$this->validation->vist_corp:'');?>" class ="inputtext" style ="width:98%" onblur ="this.className='inputtext';" onfocus ="this.className='activeinputtext';" />
< span >< font style ="color:red" > * </ font ></ span >
</ td >
</ tr >
< tr >
< td >< span class ="label" > 来访事由 </ span ></ td >
< td colspan ="5" >
< input type ="text" name ="vist_reason" value ="<?phpecho(isset($this->validation->vist_reason)?$this->validation->vist_reason:'');?>" class ="inputtext" style ="width:98%" onblur ="this.className='inputtext';" onfocus ="this.className='activeinputtext';" />
< span >< font style ="color:red" > * </ font ></ span >
</ td >
</ tr >
< tr >
< td >< span class ="label" > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </ span ></ td >
< td colspan ="5" >< input type ="text" name ="vist_memo" class ="inputtext" style ="width:98%" onblur ="this.className='inputtext';" onfocus ="this.className='activeinputtext';" /></ td >
</ tr >
</ table >
</ div >

< div >
<? phpecho$this->validation->error_string; ?>
</ div >

< div style ="align:center;margin:20px000;" >
< input type ="submit" class ="submit" value ="确认预约" > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
< input type ="reset" class ="submit" value ="清空重写" >
<!-- inputtype="button"class="submit"value="Return"onclick='javascript:history.back();'><br/ -->
</ div >
</ form >

针对Select、Radio和CheckBox字段可以使用Validation提供的set_select、set_radio和set_checkbox方法来设置已录入值。这三个方法都需要传入两个参数,第一个是页面元素的名称,第二个是选中状态的值。

版权声明:本文为博主原创文章,未经博主允许不得转载。

你可能感兴趣的:(CodeIgniter类库之Validation Class(续))