-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
feat: [Validation] add required_if
rule
#9028
base: 4.6
Are you sure you want to change the base?
Conversation
*/ | ||
public function required_if($str = null, ?string $fieldWithValue = null, array $data = []): bool | ||
{ | ||
if ($fieldWithValue === null || $data === []) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When will data be filled up? Using your original example: ['identity_number' => 'required_if[is_internal,1]'
, $fieldWithValue is is_internal,1
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The $data
is filled out automatically when the rule is called.
* @param string|null $fieldWithValue that we should check if present | ||
* @param array<string, mixed> $data Complete list of field from the form | ||
*/ | ||
public function required_if($str = null, ?string $fieldWithValue = null, array $data = []): bool |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need the nullability here for both $str and $fieldWithValue?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like yes. The $str
can be null
when the value does not come from the form. And $fieldWithValue
can be null
when the user skips the parameter and enters only: required_if
.
public function required_if($str = null, ?string $fieldWithValue = null, array $data = []): bool | ||
{ | ||
if ($fieldWithValue === null || $data === []) { | ||
throw new InvalidArgumentException('You must supply the parameters: field,expected_values, data.'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The message is not reflecting the correct arguments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about this?
throw new InvalidArgumentException('You must supply the parameters: field,value1,value2,...');
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO better.
throw new InvalidArgumentException('You must supply the parameters: field,expected_values, data.'); | ||
} | ||
|
||
// Separate fields and expected values |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please refrain from adding comments that just describes what the code is doing. They're superfluous.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, noted. i will update later.
$expectedValues = $parts; // The remainder is the expected value | ||
|
||
if (trim($field) === '' || $expectedValues === []) { | ||
throw new InvalidArgumentException('You must supply the expected values of field: E.g. field,value1,value2,...'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It may be better to have a more descriptive exception message by saying which field needs to be supplied with the expected values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how about this one?
throw new InvalidArgumentException("You must supply the field value: {$field},value? or you can add multiple value like this {$field},value1,value2 and so on.");
* This field is required when any of the other required fields have their expected values present | ||
* in the data. | ||
* | ||
* Example (The special_option field is required when the normal_option,1,2 field has the given value.): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This error message is confusing:
The special_option field is required when the normal_option,1,2 field has the given value.
The other field is normal_option
, not normal_option,1,2
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about that too, but the fields and values are combined in the {param} placeholder. How to extract fields and values from {param}? At least we can separate them and differentiate between fields and values then provide proper error messages. do you have any ideas?
Co-authored-by: kenjis <[email protected]>
👋 Hi, @warcooft! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @warcooft - if you are still interested in this PR, it needs to be rebased.
I added some comments.
* This field is required when any of the other required fields have their expected values present | ||
* in the data. | ||
* | ||
* Example (The special_option field is required when the normal_option,1,2 field has the given value.): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* Example (The special_option field is required when the normal_option,1,2 field has the given value.): | |
* Example (The special_option field is required when the normal_option field has value 1 or 2.): |
* @param string|null $fieldWithValue that we should check if present | ||
* @param array<string, mixed> $data Complete list of field from the form | ||
*/ | ||
public function required_if($str = null, ?string $fieldWithValue = null, array $data = []): bool |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like yes. The $str
can be null
when the value does not come from the form. And $fieldWithValue
can be null
when the user skips the parameter and enters only: required_if
.
*/ | ||
public function required_if($str = null, ?string $fieldWithValue = null, array $data = []): bool | ||
{ | ||
if ($fieldWithValue === null || $data === []) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The $data
is filled out automatically when the rule is called.
public function required_if($str = null, ?string $fieldWithValue = null, array $data = []): bool | ||
{ | ||
if ($fieldWithValue === null || $data === []) { | ||
throw new InvalidArgumentException('You must supply the parameters: field,expected_values, data.'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO better.
} | ||
|
||
// If the field does not exist in the data, immediately return true | ||
if (! array_key_exists($field, $data)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should also add support for fields that are nested via dot_array_search()
.
Description
Supersedes #9018
Checklist: