php 验证类(张艳冲的博客)

//  这是一个方法    http://localhost/feelappdemo/index.php?c=site&a=adduser 路径下 
    public function actionAddUser()

    {
        $model = new publicModel();
           
        $error=array();  //错误信息  
        if(isset($_POST['register'])){  //[user] => [passwd] => [repassword] => [address] => [postcode] => [url] =>                
        //    print_r($_POST['register']);
          $post=$_POST['register'];
         $kong =array(
              'user'=>array($_POST['register']['user'],'用户名不能为空'),
              'passwd'=> array($_POST['register']['passwd'],'密码不能为空'),
              'repassword'=>array($_POST['register']['repassword'],'确定密码不能为空'),  
               'jine'=> array($_POST['register']['passwd'],'金额不能为空'),
              'url'=>array($_POST['register']['url'],'网址不能为空'),    
              'qq'=>array($_POST['register']['qq'],'QQ不能为空'),      
                );  
        $model->kongValidator($kong);
        $model->lengthValidator('user',$_POST['register']['user'],3,0,'最少三个字符');
        $model->compareValidator('tocompare',$_POST['register']['passwd'],$_POST['register']['repassword'],'俩次密码不一样');
        $model->moneyValidator('jine',$_POST['register']['jine'],'请这个正确填写金额');
        $model->urlValidator('url',$_POST['register']['url'],'请这个正确填写网址');    
        $model->qqValidator('qq',$_POST['register']['qq'],'请这个正确填写QQ');    
         // print_r($error);    
          if(empty($model->errors)){
                echo "成功!";
            }
        }
    $this->render('addUser',array('error'=>$error,'model'=>$model,'post'=>$post));
    }



// php验证类

<?php

class publicModel extends ParentModel
{
  public $post=array();   
  public $errors=array();   //所有的错误   
//构造方法自动给错误的定义成model中$erroe变量   
    public  function __set($name,$value){
        if(is_array($this->errors)){   //把错误添加到错误数组中
            $this->errors[$name]=$value;
        }               
    }
//得到每个错误
 public  function  getError($key){
         return isset($this->errors[$key]) ? $this->errors[$key] : '';    
 }
//判断某个属性是否有错误信息
    public function hasError($key)
    {
        return isset($this->errors[$key]);
   }

//向类属性中错误信息
public function addError($key,$value)
{
        $this->errors[$key] = $value;
}   

// 多个判断空的方法
  public function  kongValidator($array){
              if(is_array($array)){
                   foreach ($array  as $k=>$val){
                        if(trim($val[0])==''){
                            $this->$k=$val['1'];
                        }                      
                  }        
    

转载于:https://www.cnblogs.com/zhangyanchong/archive/2012/03/30/5134067.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值