1

I have begun tinkering with validators in Laravel and seem to have programmed myself into a corner.

Background

I have a method which I use to check if an address exists through Google Maps. Since I started having just one file and validation in that file I just let it return an object containing if longitude and latitude matched and the formated address.

return (object) ['result' => (bool) $match, 'formatedAddress' => (string) $googleMaps->formatted_address];

(Typecast for clarity, no actual typecast in code at this point.)

This worked well in the controller since I could reuse the variables within the method, but not so well after moving it to AppServiceProvider.

Question:

Is there a way to make Validator::extend() set a public parameter upon validation so I can reach the formatted address? Or do I have to go about doing this in another way?

Using PHP 5.6 and Laravel 5.4.

1 Answer 1

1

I actually found the answer while tinkering a bit more. I made an error in my conditions so it did not even reach the point where I set the variable. Basically, this is the solution:

Validator::extend('location', function (
    $attribute, $value, $parameters, $validator
) {
    $validator->location = true;
}

Hope my slight error and oversight can help someone else to not get stuck in the same way.

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

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.