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

查看:109
本文介绍了Laravel 4表单验证,扩展了__call()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想扩展Form验证类以支持数组形式元素,如L4中L3 的此处所述.

I want to extend the Form validation class to support array form elements like described here for L3 in L4.

首先,我在app/config/app.php中使用它更改了Validator别名:

First, I changed the Validator alias with this in my app/config/app.php:

'Validator'       => 'app\lib\Support\Facades\Validator',

然后,我将这些代码保存为app/lib/Support/Facades/Validator.php

Then , I saved these codes as app/lib/Support/Facades/Validator.php

<?php namespace app\lib\Support\Facades;


class Validator extends \Illuminate\Support\Facades\Validator {

    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);
    }

}

然后确保我的composer.json包含用于自动加载的文件夹:

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"
    ]
},

然后,我运行php composer.phar dump-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:

protected function validateTest($attribute, $value) {
    return $value=='test';
}

它说:Method [validateTest] does not exist..我将protected更改为public,仍然一样.

It says: Method [validateTest] does not exist.. I altered the protected to public, still same.

get_class(Validator::getFacadeRoot())给了我\Illuminate\Validation\Factory,但是当我扩展编写的类时,出现此错误:Non-static method Illuminate\Validation\Factory::make() should not be called statically.

get_class(Validator::getFacadeRoot()) gives me \Illuminate\Validation\Factory, but when I extend the class I've written to it, I get this error: Non-static method Illuminate\Validation\Factory::make() should not be called statically.

注意:是的,我没有像L4那样扩展规则,因为我不想添加新规则,但是我想更改方法__call()的和getMessage()的行为.

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?

推荐答案

似乎搜索不够.如评论中所建议,我只是在此答案中将在此答案中添加的共享代码添加到了app/routes.php中,而没有创建新文件或更改别名,它可以正常工作!

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()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆