11

I want to get the parameter passed in the validation rule.

For certain validation rules that I have created, I'm able to get the parameter from the validation rule, but for few rules it's not getting the parameters.

In model I'm using the following code:

public static $rules_sponsor_event_check = array(
    'sponsor_id' => 'required',
    'event_id' => 'required|event_sponsor:sponsor_id'
);

In ValidatorServiceProvider I'm using the following code:

    Validator::extend('event_sponsor', function ($attribute, $value, $parameters) {
        $sponsor_id = Input::get($parameters[0]);
        $event_sponsor = EventSponsor::whereIdAndEventId($sponsor_id, $value)->count();

        if ($event_sponsor == 0) {
            return false;
        } else {
            return true;
        }
    });

But here I'm not able to get the sponsor id using the following:

$sponsor_id = Input::get($parameters[0]);
2
  • Try dd($parameters[0]) to check if the parameter is passed correctly. Commented May 18, 2015 at 6:55
  • dd($parameters[0]) is displaying - "sponsor_id" Commented May 18, 2015 at 7:08

2 Answers 2

20

As a 4th the whole validator is passed to the closure you define with extends. You can use that to get the all data which is validated:

Validator::extend('event_sponsor', function ($attribute, $value, $parameters, $validator) {
    $sponsor_id = array_get($validator->getData(), $parameters[0], null);
    // ...
});

By the way I'm using array_get here to avoid any errors if the passed input name doesn't exist.

Sign up to request clarification or add additional context in comments.

3 Comments

I used like the following but it is returning null - $sponsor_id = array_get($validator->getData(), $parameters[0], null); dd($sponsor_id); - do I need to pass the $validator to the following - $validator = Validator::make($input, EventSponsor::$rules_sponsor_event_check);
Please check what $validator->getData() actually returns
@lukasgeiter how do I set/access ":other" in the validation message?
0

http://laravel.com/docs/5.0/validation#custom-validation-rules

The custom validator Closure receives three arguments: the name of the $attribute being validated, the $value of the attribute, and an array of $parameters passed to the rule.

Why Input::get( $parameters ); then? you should check $parameters contents.

Edit. Ok I figured out what are you trying to do. You are not going to read anything from input if the value you are trying to get is not being submitted. Take a look to

dd(Input::all());

You then will find that

sponsor_id=Input::get($parameters[0]);

is working in places where sponsor_id was submited.

5 Comments

The parameters hold the name of another input parameter. The OP wants to get the value of that.
$input = Input::get( $parameters );dd($input); is throwing an Exception - ErrorException in Arr.php line 219: Illegal offset type in isset or empty
For dd(Input::all()); is displaying an empty array. I also have tried passing the parameter a dummy value like this - 'event_id' => 'required|event_sponsor:123' and it is also displaying an empty array.
Input will read stuff submited to your request. That's what Input does
Thanks for the quick replies. I have code like this in the controller, where I'm assigning values to the $input variable and passing to the validator - $input = array(); $user = $auth->toUser(); $input['event_id'] = $eventId;//event id for event validation $input['sponsor_id'] = $sponsorId; //event id for sponsor validation $input['organizer_id'] = $user->id; $validator = Validator::make($input, EventSponsor::$rules_sponsor_event_check);

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.