Yii模型rules()方法不起作用(Yii model rules() method doesn't work)

系统教程 行业动态 更新时间:2024-06-14 17:02:18
Yii模型rules()方法不起作用(Yii model rules() method doesn't work)

您有一个我的应用程序模型的rules()方法有问题。

这是规则():

public function rules() { $newRules = array( array('password_verification', 'required'), array('password_verification', 'length', 'min'=>6, 'max'=>32), array('password', 'compare', 'compareAttribute'=>'password_verification'), array('username, email', 'length', 'min'=>3,'max'=>255), array('password', 'length','min'=>6, 'max'=>32), array('username, email', 'unique'), array('email', 'email'), array('valid_until_formated','compare', 'compareAttribute'=>'valid_from_formated','operator'=>'>'), array('id, type,username, password, password_verification, email, valid_from, valid_until, valid_until_formated, valid_from_formated, added_on, created_by','safe'), ); }

(对不起代码的格式)。

这是问题所在:

我想测试“valid_until_formated”是否大于“valid_from_formated”。

当我提交错误值的表单(valid_from大于vali_直到)时,我收到一条错误消息,我可以在firebug上看到它,但是值会插入到数据库中。

但是,例如,如果我尝试使用已经使用的用户名或字符数少于6的密码,那么我有一个错误,并没有在数据库中创建任何东西?

有没有人知道为什么这条规则不起作用(即使我收到错误信息)? (这是valid_until_formated的格式,例如:“20121118”)

感谢您阅读我并抱歉我的大概英语。

祝你有美好的一天 :)

迈克尔

Hi have a problem with the rules() method of one of my application model.

Here is rules():

public function rules() { $newRules = array( array('password_verification', 'required'), array('password_verification', 'length', 'min'=>6, 'max'=>32), array('password', 'compare', 'compareAttribute'=>'password_verification'), array('username, email', 'length', 'min'=>3,'max'=>255), array('password', 'length','min'=>6, 'max'=>32), array('username, email', 'unique'), array('email', 'email'), array('valid_until_formated','compare', 'compareAttribute'=>'valid_from_formated','operator'=>'>'), array('id, type,username, password, password_verification, email, valid_from, valid_until, valid_until_formated, valid_from_formated, added_on, created_by','safe'), ); }

(sorry for the format of the code).

And here is the problem:

I want to test if "valid_until_formated" is greater than "valid_from_formated".

When I submit my form with wrong values (valid_from greater than vali_ until) I get an error message, I can see it on firebug but the values are inserted in data base.

But for instance if I try with a username already used or a password with fewer character than 6 then I have an error and nothing is created in database?

Does anybody have an idea why this rule doesn't work (even if I get an error message) ? (this is the format of valid_until_formated, for example: "20121118" )

Thanks for reading me and sorry for my approximate English.

Have a good day :)

Michaël

最满意答案

谢谢大家的回答。

实际上这是我做的转储错误。

我不知道validate()方法是由save()调用的。 实际上我试图保存一个用户,我不明白为什么我的CustomUser模型中的规则没有运行 - _ - 。

测试是在我的用户:rules()和我的CustomUser :: rules()继承我的User :: rules()...无论如何我修复了这个问题。

如果(myCustomUser->验证()){

myUser =新用户;

......

myUser->保存(假);

}

就像我可以用CustomUser的验证规则创建一个用户。

谢谢,祝你有愉快的一天:)

Thanks you all for your answer.

Actually it was a dump mistake I did.

I didn't know the validate() method was call by the save() one. Actually I was trying to save a User and I didn't understand why the rules in my CustomUser model were not run -_-.

The test was on my User:rules() and my CustomUser::rules() inherits my User::rules()...Anyway I fixed this issue like that.

if(myCustomUser->validate()){

myUser = new User;

... ...

myUser->save(false);

}

Like that I can create a user with the validation rules of my CustomUser.

Thanks, Have a nice day :)

更多推荐

本文发布于:2023-04-21 18:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/fe3b049078426c0762d109fb3f3f6b76.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   模型   方法   rules   Yii

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!