Laravel 4 Form Validation, Extending the __call() method

假如想象 提交于 2019-12-18 09:53:50

问题


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'       => 'app\lib\Support\Facades\Validator',

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

}

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 \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.

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?


回答1:


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


来源:https://stackoverflow.com/questions/18894362/laravel-4-form-validation-extending-the-call-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!