-
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?
Changes from 18 commits
8f86617
195bacf
82954bf
20f3b4b
62737f1
db6d923
1407006
de666cc
95bf9b5
8cde4a0
5865971
6e7f7cf
e127ddd
c5c9578
6980b32
1215f36
97eb7fe
4ef7b2a
08dd975
afed8fb
c3df1b0
a3056c5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -436,6 +436,47 @@ public function required_without( | |
return true; | ||
} | ||
|
||
/** | ||
* 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.): | ||
* | ||
* required_if[normal_option,1,2] | ||
* | ||
* @param string|null $str | ||
* @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 commentThe reason will be displayed to describe this comment to others. Learn more. Do we need the nullability here for both $str and $fieldWithValue? |
||
{ | ||
if ($fieldWithValue === null || $data === []) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When will data be filled up? Using your original example: |
||
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 commentThe 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 commentThe 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,...'); |
||
} | ||
|
||
// Separate fields and expected values | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. OK, noted. i will update later. |
||
$parts = explode(',', $fieldWithValue); | ||
$field = array_shift($parts); // Get field | ||
$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 commentThe 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 commentThe 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."); |
||
} | ||
|
||
// If the field does not exist in the data, immediately return true | ||
if (! array_key_exists($field, $data)) { | ||
return true; | ||
} | ||
|
||
// If the value of a field matches one of the expected values, then this field is required | ||
if (in_array($data[$field], $expectedValues, true)) { | ||
// The field to be checked must exist and cannot be empty | ||
return trim($str ?? '') !== ''; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* The field exists in $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.
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
, notnormal_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?