沃梦达 / 编程问答 / php问题 / 正文

Laravel 4 表单验证,扩展 __call() 方法

Laravel 4 Form Validation, Extending the __call() method(Laravel 4 表单验证,扩展 __call() 方法)

本文介绍了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() 方法

基础教程推荐