Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.3] Before/After validation hooks for FormRequests #16238

Merged
merged 1 commit into from
Nov 7, 2016
Merged

[5.3] Before/After validation hooks for FormRequests #16238

merged 1 commit into from
Nov 7, 2016

Conversation

jtgrimes
Copy link
Contributor

@jtgrimes jtgrimes commented Nov 2, 2016

I often find that I want to munge data before I run validation and generally override the validate() function in ValidatesWhenResolvedTrait. I thought that might be useful to others, so figured I'd PR it.

(I admit, I've never had a use for afterValidation(), but I'm unable to add a before() function without an after().)

If the PR is accepted, I'll make a PR for the docs.

@GrahamCampbell GrahamCampbell changed the title Before/After validation hooks for FormRequests [5.3] Before/After validation hooks for FormRequests Nov 2, 2016
@taylorotwell
Copy link
Member

Is there a strong reason you don't want to just keep overriding validate?

@jtgrimes
Copy link
Contributor Author

jtgrimes commented Nov 3, 2016

I'm perfectly happy to keep overriding validate - this was just one of those rare occasions when something I do might be useful to someone else.

What would you think of a line in the docs explaining that if you want to fiddle with the data before validation, this is how you do it?

@bryannielsen
Copy link
Contributor

I usually end up doing something similar to get an afterValidation hook on my FormRequests. I agree with @jtgrimes, it's not a problem to do keep doing this on an as needed basis but it could be a useful hook for other developers too.

@tomschlick
Copy link
Contributor

This is something that Laravel has been sorely missing.

I remember back in the CodeIgniter days you could add mutators to the validation for things like trim or to uppercase/lowercase a field before it hit validation / the database.

@taylorotwell taylorotwell merged commit 7991645 into laravel:5.3 Nov 7, 2016
@taylorotwell
Copy link
Member

I renamed this to prepareForValidation. Got rid of after hook since we have no use case.

@juukie
Copy link
Contributor

juukie commented Nov 8, 2016

Thanks, I like this method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants