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?