Laravel 4 Form Validation, Extending the __call() method(Laravel 4 表单验证,扩展 __call() 方法)
问题描述
我想扩展表单验证类以支持数组表单元素,如 L4 中描述的此处用于 L3.p>
首先,我在我的 app/config/app.php
中更改了 Validator 别名:
'验证器' =>'applibSupportFacadesValidator',
然后,我将这些代码保存为 app/lib/Support/Facades/Validator.php
extensions[$rule])){$success &= $this->callExtension($rule, $parameters);}throw new BadMethodCallException("方法 [$method] 不存在.");}返回$成功;} 别的 {返回父级::__call($method, $parameters);}}受保护的函数 getMessage($attribute, $rule) {if (substr($rule, -6) === '_array') {$rule = substr($rule, 0, -6);}返回父级::getMessage($attribute, $rule);}}
然后我确保我的 composer.json
包含用于自动加载的文件夹:
自动加载":{类图":[应用程序/命令",应用程序/控制器",应用程序/模型",应用程序/数据库/迁移",应用程序/数据库/种子","app/tests/TestCase.php",应用程序/库",应用程序/库/支持",应用程序/lib/支持/外墙"]},
然后,我运行 php composer.phar dump-autoload
来生成自动加载类.
问题是,这似乎不起作用.我什至尝试在我生成的文件中添加自定义验证方法,如下所示:
受保护的函数 validateTest($attribute, $value) {返回 $value=='test';}
它说:方法 [validateTest] 不存在.
.我把protected
改成了public
,还是一样.
get_class(Validator::getFacadeRoot())
给了我 IlluminateValidationFactory
,但是当我扩展我写给它的类时,我得到此错误:不应静态调用非静态方法 IlluminateValidationFactory::make()
.
注意:是的,我没有像 L4 方式那样扩展规则,因为我不想添加新规则,但我想更改方法 __call()
和 getMessage()
的行为.
我错过了什么,我怎样才能做到这一点?
好像我搜索的不够多.正如评论中所建议的,我刚刚将 in this answer 共享的代码添加到了我的 app/routes.php
无需创建新文件或更改别名,它就可以完美运行!
这是我在解决方案中给出的验证规则:
$rules = array('项目' =>'必需|分钟:1|整数或数组');
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
:
'Validator' => 'applibSupportFacadesValidator',
Then , I saved these codes as app/lib/Support/Facades/Validator.php
<?php namespace applibSupportFacades;
class Validator extends IlluminateSupportFacadesValidator {
public function __call($method, $parameters) {
if (substr($method, -6) === '_array') {
$method = substr($method, 0, -6);
$values = $parameters[1];
$success = true;
foreach ($values as $value) {
$parameters[1] = $value;
$rule = snake_case(substr($method, 8));
if (isset($this->extensions[$rule]))
{
$success &= $this->callExtension($rule, $parameters);
}
throw new BadMethodCallException("Method [$method] does not exist.");
}
return $success;
} else {
return parent::__call($method, $parameters);
}
}
protected function getMessage($attribute, $rule) {
if (substr($rule, -6) === '_array') {
$rule = substr($rule, 0, -6);
}
return parent::getMessage($attribute, $rule);
}
}
Then I made sure my composer.json
has the folder included for autoload:
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php",
"app/lib",
"app/lib/Support",
"app/lib/Support/Facades"
]
},
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:
protected function validateTest($attribute, $value) {
return $value=='test';
}
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:
$rules = array(
'items' => 'required|min:1|integerOrArray'
);
这篇关于Laravel 4 表单验证,扩展 __call() 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Laravel 4 表单验证,扩展 __call() 方法
基础教程推荐
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01