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.

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

4 Comments

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.
@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?
@Epsilon47 I think Jeff and I both misunderstood your question :) could you clarify it?
@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. :)
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;
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.