Home > Backend Development > PHP Tutorial > yii自定义rules,参数传递

yii自定义rules,参数传递

WBOY
Release: 2016-06-06 20:14:37
Original
1066 people have browsed it

<code>    return [
        [['cat_name','unit','pid','sort','is_show','price_area'], 'required',],
        [['pid', 'sort', 'is_show', 'price_area'], 'integer'],
        [['cat_name'], 'string', 'max' => 30],
        [['unit'], 'string', 'max' => 10],
        [['des'], 'string', 'max' => 100],
         [['cat_name'], 'checkCategoryName' ,  'params' => [ 'pid' => '1',  'catgoryname'  => 'cat_name'  ]  ], //验证分类名是否合法 (同一级别下 分类名不能重复)
    ];</code>
Copy after login
Copy after login

最后一条规则,我想把pid和 cat_name作为参数传递到 自定义的checkCategoryName($attribute, $params)中去,
但是好像只可以传递常量例如pid=1,但是为什么我传cat_name变量不行?

回复内容:

<code>    return [
        [['cat_name','unit','pid','sort','is_show','price_area'], 'required',],
        [['pid', 'sort', 'is_show', 'price_area'], 'integer'],
        [['cat_name'], 'string', 'max' => 30],
        [['unit'], 'string', 'max' => 10],
        [['des'], 'string', 'max' => 100],
         [['cat_name'], 'checkCategoryName' ,  'params' => [ 'pid' => '1',  'catgoryname'  => 'cat_name'  ]  ], //验证分类名是否合法 (同一级别下 分类名不能重复)
    ];</code>
Copy after login
Copy after login

最后一条规则,我想把pid和 cat_name作为参数传递到 自定义的checkCategoryName($attribute, $params)中去,
但是好像只可以传递常量例如pid=1,但是为什么我传cat_name变量不行?

inline validator的话可以在方法中直接访问即可:

<code>function checkCategoryName($attribute, $params) {
    $pid = $this->pid;
}
</code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template