Archive for the ‘ PHP ’ Category

漫谈YII 表单的内置验证规则

Yii 有内置数据验证,我将它列出来:

required: 必填字段验证, 来自 CRequiredValidator类的别名

array(‘字段名列表用逗号隔开’, ‘required’),    就这样的一个小小的写法,可以让字段前面加个 * 星号显示出来,表示必填字段
filter: 过滤验证, 来自 CFilterValidator 类的别名
match: 使用正则表达式,来自CRegularExpressionValidator的别名, 用于 验证属性是否匹配一个正则表达式
email: 验证邮箱,来自CEmailValidator类的别名
url: 验证网址, 来自CUrlValidator的别名
unique: 验证唯一性, 来自CUniqueValidator的别名
compare: 多字段对比验证,来自CCompareValidator的别名
length: 验证字符串的长度验证,来自CStringValidator的别名
in: 验证属性值是否在一个预订的值列表里面,来自CRangeValidator的别名
numerical: 验证字符串是否为整数,来自CNumberValidator的别名
captcha: 验证码啦,来自CCaptchaValidator的别名
type: 验证字符串类型,来自 CTypeValidator的别名
file: 文件验证,来自CFileValidator的别名
default: 默认值验证, 来自CDefaultValueValidator的别名
exist: 验证数据是否存在, 来自CExistValidator的别名
boolean: 验证真假值, 来自CBooleanValidator的别名
date: 验证日期时间, 来自CDateValidator的别名
safe:验证数据是否安全,来自 CSafeValidator的别名
unsafe: 不验证,来自CUnsafeValidator的别名

漫谈yii预定义验证

在每一个Model中,总会一些自动生成的验证规则。它是入库前验证是否报错的先决条件。模型中,model的示例如下:
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array(‘realname, gender, mobile, province, city, district, address, zipcode’, ‘required’),
array(‘realname, mobile, province, city, district’, ‘length’, ‘max’=>30),
array(‘address’, ‘length’, ‘max’=>300),
array(‘zipcode’, ‘length’, ‘max’=>10),
array(‘updated’,'default’, ‘value’=>date(‘Y-m-d H:i:s’), ‘setOnEmpty’=>false, ‘on’=>’update’),
array(‘created, updated’, ‘default’,'value’=>date(‘Y-m-d H:i:s’), ‘setOnEmpty’=>false, ‘on’=>’insert’),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array(‘id, user_id, realname, gender, mobile, province, city, district, address, zipcode, remark’, ‘safe’, ‘on’=>’search’),
);
}
上面即是一个典型的模型预定定验证规则。下面,我将能接触到的验证,细细的列出来。

boolean : CBooleanValidator 的别名, 确保属性的值是CBooleanValidator::trueValue 或 CBooleanValidator::falseValue 。[...]

memcache在Yii中如何应用

Memcache是一个高性能的分布式的内存对象缓存系统,通过在内存里维护一个统一的巨大的hash表,它能够用来存储各种格式的数据,包括图像、视频、文件以及数据库检索的结果等。简单的说就是将数据调用到内存中,然后从内存中读取,从而大大提高读取速度。
  Memcache是danga的一个项目,最早是LiveJournal 服务的,最初为了加速 LiveJournal 访问速度而开发的,后来被很多大型的网站采用。
  Memcached是以守护程序方式运行于一个或多个服务器中,随时会接收客户端的连接和操作。
正确使用 memcache 的前提是先在 system/protected/config/main.php 中配置好组件 memcache。[...]

Yii表格输入研究

看完Yii表单表格输入后,发现,适合做批量提交。让我们一起来看看下面的代码。参考网址为:http://www.yiiframework.com/doc/guide/1.1/zh_cn/form.table。

在上面是这么介绍的:
有时我们想通过批量模式收集用户输入。也就是说, 用户可以为多个模型实例输入信息并将它们一次性提交。[...]