Note that there are some explanatory texts on larger screens.

plurals
  1. POLaravel 4 Form Validation, Extending the __call() method
    primarykey
    data
    text
    <p>I want to extend the Form validation class to support array form elements like described <a href="https://stackoverflow.com/a/14199205/570763">here for L3</a> in L4.</p> <p>First, I changed the Validator alias with this in my <code>app/config/app.php</code>:</p> <pre><code>'Validator' =&gt; 'app\lib\Support\Facades\Validator', </code></pre> <p>Then , I saved these codes as app/lib/Support/Facades/Validator.php</p> <pre><code>&lt;?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-&gt;extensions[$rule])) { $success &amp;= $this-&gt;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); } } </code></pre> <p>Then I made sure my <code>composer.json</code> has the folder included for autoload:</p> <pre><code>"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" ] }, </code></pre> <p>Then, I ran <code>php composer.phar dump-autoload</code> to generate autoload classes.</p> <p>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:</p> <pre><code>protected function validateTest($attribute, $value) { return $value=='test'; } </code></pre> <p>It says: <code>Method [validateTest] does not exist.</code>. I altered the <code>protected</code> to <code>public</code>, still same.</p> <p><code>get_class(Validator::getFacadeRoot())</code> gives me <code>\Illuminate\Validation\Factory</code>, but when I extend the class I've written to it, I get this error: <code>Non-static method Illuminate\Validation\Factory::make() should not be called statically</code>.</p> <p><strong>Note</strong>: 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 <code>__call()</code>'s and <code>getMessage()</code>'s behaviour.</p> <p>What am I missing, how can I make this work?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload