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.6] Adds new comparison validation rules to compare two fields against each other. #23807

Closed
wants to merge 3 commits into from

Conversation

Alymosul
Copy link
Contributor

@Alymosul Alymosul commented Apr 4, 2018

This PR adds two new validation rules greater_than & less_than, those two new rules will be used to compare two fields against each other, the two rules are different than max/min rules because the two fields under validation must be of the same type also they passed parameter cannot be a hard coded value..

An example:
Supposedly we have a form that takes two input min_salary & max_salary...

$rules = [
'min_salary' => 'less_than:max_salary',
'max_salary' => 'greater_than:min_salary'
];

The above rules will enforce the min_salary to respect the max_salary value and vice versa.

@laurencei
Copy link
Contributor

Nice. If this PR gets accepted @Alymosul - can you please do a PR on the Laravel Docs to document this new validation option.

@Alymosul
Copy link
Contributor Author

Alymosul commented Apr 5, 2018

Sure! Just waiting for Taylor’s decision.

@jmarcher
Copy link
Contributor

jmarcher commented Apr 5, 2018

@Alymosul also the translations for the laravel/laravel repository.

* @param $first
* @param $second
*/
protected function requireSameTypeValues($first, $second)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing @return and @throws

@devcircus
Copy link
Contributor

Compared to all existing validation rules, the naming suggests you could indeed do the following:

$rules = [
'salary' => 'greater_than:36000',
'fees' => 'less_than:3500'
];

I understand there's 'min' and 'max', but this will be a major source of confusion in the future. IMO we need to back up and try again. I like the idea, but not the implementation or naming.

Personally, I think a custom rule in your project or a small package with similar functionality would be the best place for this.

@Alymosul
Copy link
Contributor Author

what's wrong with the implementation?

@devcircus
Copy link
Contributor

Forgive me. After re-reading, the actual implementation seems fine.

@taylorotwell
Copy link
Member

I agree it may be beneficial to allow specifying an integer number the field must be greater than. Also, what about the eventual "can we have greater than or equal to?"

@Alymosul
Copy link
Contributor Author

Alymosul commented Apr 17, 2018

I think we can have greater than or equal as a separate validation rule, but i think if we will allow specifying an integer number then it overlaps with the 'max' rule, the concept behind this rule is the comparison against another field... so what do you think about that?

@taylorotwell
Copy link
Member

Feel free to submit when it can take integers and handles "equal to"... personally I would just shorten the rules to something like gt:field and gte:field etc.

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.

6 participants