|
需求:ci form中的validation 很好用,但如果前台js validate 也得写一套一样的规则。验证页面少还好点,有10个form页面修改起来那就太惨了。为了大家不再遭罪,我写CI validation 转 js validate 规则的函数放给大家
论坛中的代码被转义了,下载代码在附件里。
xxx.rar
(1.04 KB, 下载次数: 72)
PHP复制代码 if ( ! function_exists('rules_ci2js')){
function rules_ci2js ($rules){
if(!$rules) return "rules:'',\\n messages:''";
$CI =& get_instance ();
$CI->lang->load('validation');
$i=0;
$form_tmp=array();
foreach($rules as $field=>$r){
$_arr=explode("|",$r);
$__arr=array();
$__arr_message=array();
foreach($_arr as $v){
if(ereg("([minax]{3})_length\\[([0-9]+)\\]",$v,$tmp)){
$__arr[]="{$tmp[1]}length:{$tmp[2]}";
$__arr_message["{$tmp[1]}length"]=langx ("{$tmp[1]}_length",array(langx ($field),$tmp[2]));
}
if(ereg("([minax]{3})_number\\[([0-9]+)\\]",$v,$tmp)){
$__arr[]="{$tmp[1]}:{$tmp[2]}";
$__arr_message["{$tmp[1]}"]=langx ("{$tmp[1]}_number",array(langx ($field),$tmp[2]));
}
if(ereg("matches\\[(.+)\\]",$v,$tmp)){
$__arr[]="equalTo:'#{$tmp[1]}'";
$__arr_message["equalTo"]=langx ("matches",array(langx ($field),langx ($tmp[1])));
}
if($v=="required"){
$__arr[]="{$v}:true";
$__arr_message["required"]=langx ($v,langx ($field));
}
if($v=="numeric"){
$__arr[]="number:true";
$__arr_message["number"]=langx ($v,langx ($field));
}
if($v=="integer"){
$__arr[]="digits:true";
$__arr_message["digits"]=langx ($v,langx ($field));
}
if($v=="valid_email"){
$__arr[]="email:true";
$__arr_message["email"]=langx ($v,langx ($field));
}
if($v=="prep_url"){
$__arr[]="url:true";
$__arr_message["url"]=langx ($v,langx ($field));
}
}
if($__arr){
$form_tmp['rules'][$i][$field]=$__arr;
$form_tmp['messages'][$i][$field]=$__arr_message;
}
}
if(!empty($form_tmp)){
$js_rules=json_encode($form_tmp['rules']);
$js_message=json_encode($form_tmp['messages']);
$js_rules=strtr($js_rules,array(
'":["'=>':{',
'"],"'=>'},',
'","'=>',',
'[{"'=>'{',
'"]}]'=>'}}',
));
$js_message=strtr($js_message,array(
'['=>'',
']'=>'',
'":{"'=>':{',
'":'=>':',
',"'=>',',
'{"'=>'{'
));
$jsform="rules:{$js_rules},\\n messages:{$js_message}";
return $jsform;
}
}
}
复制代码
建议放到form_helper.php里,方便调用,里面有个langx()的函数是代码如下:
PHP复制代码 if ( ! function_exists('langx'))
{
function langx ($line, $param=false)
{
$CI =& get_instance ();
$line=$CI->lang->line($line);
if($param!==false){
if(is_array($param)){
foreach($param as $p){
$line=preg_replace('/%s/',$p,$line,1);
}
}else{
$line = strtr($line,array('%s'=>$param));
}
}
return $line;
}
} 复制代码
你把它放到lang_helper.php中就可以了
语言定义中要和表单中name相同,这样就能正确显示 XXX犯了XXX错误了
调用代码
PHP复制代码
$fields['id'] = 'ID';
$fields['tid'] = langx('tid');;
$fields['name'] = langx('name');
$fields['desc'] = langx('desc');
$fields['thumb'] = langx('thumb');
$fields['tel'] = langx('tel');
$fields['mobile'] = langx('mobile');
$fields['linkuser'] = langx('linkuser');
$fields['addr'] = langx('addr');
$this->CI->validation->set_fields($fields);
$rules['id'] = 'numeric';
$rules['tid'] = 'required|numeric';
$rules['name'] = 'trim|required|max_length[32]|spare_name|min_length[4]';
$rules['thumb'] = 'trim|min_length[32]|max_length[100]';
$rules['tel'] = 'trim|required|min_length[8]|max_length[50]';
$rules['mobile'] = 'trim|required|min_length[11]|max_length[50]';
$rules['linkuser'] = 'trim|required|min_length[2]|max_length[4]';
$rules['addr'] = 'trim|required|min_length[10]';
$rules['desc'] = 'trim|max_length[1000]';
$this->CI->validation->set_rules($rules);
//在这里进行了转换
$data['jsform']=rules_ci2js($rules);
复制代码
前台调用,不会jquery框架的就自行研究validate吧,规则都是一样的
JS复制代码 <script type="text/javascript" src="***/js/jquery.min.js"></script>
<script type="text/javascript" src="***/js/jquery.validate.js"></script>
<script type="text/javascript">
$(function(){
$("#sight_form").validate({
<?php echo $jsform;?>,
event: "blur",
submitHandler: function() {
$('ol').each(function(i){
if($(this).css('display')=='none'){$(this).remove();}
});
document.getElementById("sight_form").submit();
}
});
});
</script> 复制代码
生成代码如下:
JS复制代码 <script type="text/javascript">
$(function(){
$("#sight_form").validate({
rules:{id:{number:true},tid:{required:true,number:true},name:{required:true,maxlength:32,minlength:4},thumb:{minlength:32,maxlength:100},tel:{required:true,minlength:8,maxlength:50},mobile:{required:true,minlength:11,maxlength:50},linkuser:{required:true,minlength:2,maxlength:4},addr:{required:true,minlength:10},desc:{maxlength:1000},ex_bus_stop:{required:true},ex_bus_line:{required:true},ex_connect:{required:true,minlength:32,maxlength:5000},vcode:{required:true}},
messages:{id:{number:"%s \\u5fc5\\u987b\\u662f\\u4e00\\u4e2a\\u6570\\u5b57\\u3002"},tid:{required:"\\u9910\\u5385\\u7c7b\\u522b \\u662f\\u5fc5\\u987b\\u8981\\u586b\\u5199\\u7684\\u3002",number:"\\u9910\\u5385\\u7c7b\\u522b \\u5fc5\\u987b\\u662f\\u4e00\\u4e2a\\u6570\\u5b57\\u3002"},name:{required:"\\u9910\\u5385\\u540d\\u79f0 \\u662f\\u5fc5\\u987b\\u8981\\u586b\\u5199\\u7684\\u3002",maxlength:"\\u9910\\u5385\\u540d\\u79f0 \\u6700\\u5927\\u957f\\u5ea6\\u5e94\\u8be5\\u662f 32 \\u4e2a\\u5b57\\u7b26\\u4ee5\\u5185\\u3002",minlength:"\\u9910\\u5385\\u540d\\u79f0 \\u6700\\u5c0f\\u957f\\u5ea6\\u5e94\\u8be5\\u662f 4 \\u4e2a\\u5b57\\u7b26\\u4ee5\\u4e0a\\u3002"},thumb:{minlength:"\\u9910\\u5385\\u6807\\u5fd7 \\u6700\\u5c0f\\u957f\\u5ea6\\u5e94\\u8be5\\u662f 32 \\u4e2a\\u5b57\\u7b26\\u4ee5\\u4e0a\\u3002",maxlength:"\\u9910\\u5385\\u6807\\u5fd7 \\u6700\\u5927\\u957f\\u5ea6\\u5e94\\u8be5\\u662f 100 \\u4e2a\\u5b57\\u7b26\\u4ee5\\u5185\\u3002"},tel:{required:"\\u56fa\\u5b9a\\u7535\\u8bdd \\u662f\\u5fc5\\u987b\\u8981\\u586b\\u5199\\u7684\\u3002",minlength:"\\u56fa\\u5b9a\\u7535\\u8bdd \\u6700\\u5c0f\\u957f\\u5ea6\\u5e94\\u8be5\\u662f 8 \\u4e2a\\u5b57\\u7b26\\u4ee5\\u4e0a\\u3002",maxlength:"\\u56fa\\u5b9a\\u7535\\u8bdd \\u6700\\u5927\\u957f\\u5ea6\\u5e94\\u8be5\\u662f 50 \\u4e2a\\u5b57\\u7b26\\u4ee5\\u5185\\u3002"},mobile:{required:"\\u79fb\\u52a8\\u7535\\u8bdd \\u662f\\u5fc5\\u987b\\u8981\\u586b\\u5199\\u7684\\u3002",minlength:"\\u79fb\\u52a8\\u7535\\u8bdd \\u6700\\u5c0f\\u957f\\u5ea6\\u5e94\\u8be5\\u662f 11 \\u4e2a\\u5b57\\u7b26\\u4ee5\\u4e0a\\u3002",maxlength:"\\u79fb\\u52a8\\u7535\\u8bdd \\u6700\\u5927\\u957f\\u5ea6\\u5e94\\u8be5\\u662f 50 \\u4e2a\\u5b57\\u7b26\\u4ee5\\u5185\\u3002"},linkuser:{required:"\\u8054 \\u7cfb \\u4eba \\u662f\\u5fc5\\u987b\\u8981\\u586b\\u5199\\u7684\\u3002",minlength:"\\u8054 \\u7cfb \\u4eba \\u6700\\u5c0f\\u957f\\u5ea6\\u5e94\\u8be5\\u662f 2 \\u4e2a\\u5b57\\u7b26\\u4ee5\\u4e0a\\u3002",maxlength:"\\u8054 \\u7cfb \\u4eba \\u6700\\u5927\\u957f\\u5ea6\\u5e94\\u8be5\\u662f 4 \\u4e2a\\u5b57\\u7b26\\u4ee5\\u5185\\u3002"},addr:{required:"\\u9910\\u5385\\u5730\\u5740 \\u662f\\u5fc5\\u987b\\u8981\\u586b\\u5199\\u7684\\u3002",minlength:"\\u9910\\u5385\\u5730\\u5740 \\u6700\\u5c0f\\u957f\\u5ea6\\u5e94\\u8be5\\u662f 10 \\u4e2a\\u5b57\\u7b26\\u4ee5\\u4e0a\\u3002"},desc:{maxlength:"\\u9910\\u5385\\u63cf\\u8ff0 \\u6700\\u5927\\u957f\\u5ea6\\u5e94\\u8be5\\u662f 1000 \\u4e2a\\u5b57\\u7b26\\u4ee5\\u5185\\u3002"},ex_bus_stop:{required:"\\u516c \\u4ea4 \\u7ad9 \\u662f\\u5fc5\\u987b\\u8981\\u586b\\u5199\\u7684\\u3002"},ex_bus_line:{required:"\\u53ef\\u4e58\\u516c\\u4ea4\\u7ebf \\u662f\\u5fc5\\u987b\\u8981\\u586b\\u5199\\u7684\\u3002"},ex_connect:{required:"\\u8be6\\u7ec6\\u63cf\\u8ff0 \\u662f\\u5fc5\\u987b\\u8981\\u586b\\u5199\\u7684\\u3002",minlength:"\\u8be6\\u7ec6\\u63cf\\u8ff0 \\u6700\\u5c0f\\u957f\\u5ea6\\u5e94\\u8be5\\u662f 32 \\u4e2a\\u5b57\\u7b26\\u4ee5\\u4e0a\\u3002",maxlength:"\\u8be6\\u7ec6\\u63cf\\u8ff0 \\u6700\\u5927\\u957f\\u5ea6\\u5e94\\u8be5\\u662f 5000 \\u4e2a\\u5b57\\u7b26\\u4ee5\\u5185\\u3002"},vcode:{required:"\\u9a8c\\u8bc1\\u7801 \\u662f\\u5fc5\\u987b\\u8981\\u586b\\u5199\\u7684\\u3002"}},
event: "blur",
submitHandler: function() {
$('ol').each(function(i){
if($(this).css('display')=='none'){$(this).remove();}
});
document.getElementById("sight_form").submit();
}
});
});
</script> 复制代码 |
评分
-
查看全部评分
|