// 这是一个方法 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