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

Make the validate functions public so they can be used for custom rules. #20200

Merged
merged 1 commit into from Jul 20, 2017
Merged

Make the validate functions public so they can be used for custom rules. #20200

merged 1 commit into from Jul 20, 2017

Conversation

ghost
Copy link

@ghost ghost commented Jul 20, 2017

This can be useful if a developer wants to change a value before validating it.
For example a user might post a url without a protocol. Rather than returning a validation error it might be preferable to assume a protocol if there is one so as a custom rule that could look like this:

Validator::extend('loose_url', function ($attribute, $value, $parameters, $validator) {
    $value = $this->addProtocolIfMissing($value);
    return $validator->validateUrl($attribute, $value);
});

This can be useful if a developer wants to change a value before validating it.
For example a user might post a url without a protocol. Rather than returning a validation error it might be preferable to assume a protocol if there is one so as a custom rule that could look like this:
Validator::extend('loose_url', function ($attribute, $value, $parameters, $validator) {
    $value = $this->addProtocolIfMissing($value);
    return $validator->validateUrl($attribute, $value);
});
@taylorotwell taylorotwell merged commit b61633f into laravel:master Jul 20, 2017
@lagbox
Copy link
Contributor

lagbox commented Jul 20, 2017

Hehe, I literally just ran into this today and did an inheritance hack to get around it.
Awesome.

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.

None yet

3 participants