0

I have basic custom validation rule. In

public function passes($attribute, $value)
{
    foreach ($parameters as $key)
    {
        if ( ! empty(Input::get($key)) )
        {
            return false;
        }
    }

    return true;
}

I have my rule defined. I, although need to retrieve parameters from the rule but the passes method does not provide it as an argument.

If I would use the style Validator:extends... that provides 4 arguments: $attribute, $value, $parameters, $validator. Then I could use the parameters easily, unfortunatelly I have to use this way.

EDIT:

To clear the question. I want to retrieve the parameters of the rule, like so in other way of coding it: 'not_empty:user_id'. The array of values behind the colon.

H H
  • 2,065
  • 1
  • 24
  • 30
Epsilon47
  • 768
  • 1
  • 13
  • 28

2 Answers2

1

I believe the only way is to retrieve it from the request when using rule objects.

For example:

public function passes($attribute, $value)
{
    foreach ($parameters as $key) {
        // Or using \Request::input($key) if you want to use the facade
        if (!empty(request()->input($key)) { 
            return false;
        }
    }

    return true;
 }
Alex
  • 1,425
  • 11
  • 25
1

Edit:---

The custom rule object is simply an object. If you want to pass it any more parameters you can in it's constructor:

$request->validate([
    'name' => ['required', new MyCustomRule('param', true, $foo)],
]);

Then save those and use them in your passes function.

public function __construct($myCustomParam){
    $this->myCustomParam = $myCustomParam;
}

Then in your passes function use:

$this->myCustomParam
Jeff
  • 24,623
  • 4
  • 69
  • 78
  • Ofcourse you can type hint them, smart. However I think you want `$this->request->input($key);` to retrieve other input data instead of rout parameters. – Alex Jul 27 '18 at 14:16
  • @AlexBouma i read it as trying to access a route parameter... now that I read it again im not sure either of us got his question exactly right. Is he referring to passing params into a validation rule? – Jeff Jul 27 '18 at 14:18
  • @Epsilon47 I think Jeff and I both misunderstood your question :) could you clarify it? – Alex Jul 27 '18 at 14:22
  • @Jeff that's it. I was considering such possibility. But I did not want to re-invent the wheel again if there is some built-in method to do so. Thank you. :) – Epsilon47 Jul 27 '18 at 14:29