Laravel 4 表单验证,扩展 __call() 方法
问题描述
我想扩展表单验证类以支持数组表单元素,如 L4 中描述的此处用于 L3.p>
首先,我在我的 app/config/app.php
中更改了 Validator 别名:
然后,我将这些代码保存为 app/lib/Support/Facades/Validator.php
然后我确保我的 composer.json
包含用于自动加载的文件夹:
然后,我运行 php composer.phar dump-autoload
来生成自动加载类.
问题是,这似乎不起作用.我什至尝试在我生成的文件中添加自定义验证方法,如下所示:
它说:方法 [validateTest] 不存在.
.我把protected
改成了public
,还是一样.
get_class(Validator::getFacadeRoot())
给了我 IlluminateValidationFactory
,但是当我扩展我写给它的类时,我得到此错误:不应静态调用非静态方法 IlluminateValidationFactory::make()
.
注意:是的,我没有像 L4 方式那样扩展规则,因为我不想添加新规则,但我想更改方法 __call()
和 getMessage()
的行为.
我错过了什么,我怎样才能做到这一点?
好像我搜索的不够多.正如评论中所建议的,我刚刚将 in this answer 共享的代码添加到了我的 app/routes.php
无需创建新文件或更改别名,它就可以完美运行!
这是我在解决方案中给出的验证规则:
I want to extend the Form validation class to support array form elements like described here for L3 in L4.
First, I changed the Validator alias with this in my app/config/app.php
:
Then , I saved these codes as app/lib/Support/Facades/Validator.php
Then I made sure my composer.json
has the folder included for autoload:
Then, I ran php composer.phar dump-autoload
to generate autoload classes.
The thing is that, it seems like this isn't working. I even tried to add a custom validation method into the file I've generated, something like this:
It says: Method [validateTest] does not exist.
. I altered the protected
to public
, still same.
get_class(Validator::getFacadeRoot())
gives me IlluminateValidationFactory
, but when I extend the class I've written to it, I get this error: Non-static method IlluminateValidationFactory::make() should not be called statically
.
Note: Yes, I didn't extend the rules like L4 way, because I don't want to add a new rule, but I want to change the method __call()
's and getMessage()
's behaviour.
What am I missing, how can I make this work?
Seems that I didn't search enough. As suggested in the comments, I just added the codes shared in this answer to my app/routes.php
without creating a new file or changing the alias, and it worked flawlessly!
This is the validation rule that I've given with the solution:
这篇关于Laravel 4 表单验证,扩展 __call() 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!