-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Add requiredIf()
and requiredUnless()
validation methods
#6666
Conversation
PR Summary
|
Hey! Thank you very much for this very thorough PR, the effort is much appreciated. Do you think it's worth making the |
@danharrin thanks! Good question, I think that case is covered by the |
Alright, thanks! |
Looks like this doesn't work exactly how I assumed it would. The validation works but using just Adding something like this inside $this->required(static function (Field $component, Closure $get) use ($statePath, $stateValues) {
$statePath = $component->evaluate($statePath);
$stateValues = $component->evaluate($stateValues);
return in_array($get($statePath), (array) $stateValues);
}); @danharrin thoughts? The current behaviour does work, is consistent with TextInput::make('status_detail')
->required(fn (Closure $get) => $get('status') === 'other')
->requiredIf('status', 'other'); |
Maybe, we should use |
This PR adds
requiredIf()
andrequiredUnless()
validation methods. I know that performing just the actual underlying validation here is technically already possible using$get()
, but that approach results in incorrect error messages.I've seen in other issues/PRs and in Discord that the suggested way to accomplish this is manually with
->required()
and aClosure
and$get()
, but the error messages produced that way don't accurately describe the actual validation rules. It may be possible to manually override the messages, but that's still extra work and code that this PR makes unnecessary.Before
This validates correctly (validation fails if
status
is set toother
andstatus_detail
is left empty), but the error message for thestatus_detail
field is just "The status detail is required." That may or may not be true depending on the other input in the form, and it could be misleading because it implies that the field is always required, which it isn't.It's possible to get a better error message by specifying the rule manually with a string, but that's kind of annoying because you have to prefix it with
data.
(and I saw this discouraged in Discord because it requires knowledge of Filament's internals and could be confusing):Finally, if you want the required state of the field to react to other fields and you want an accurate error message, you have to define your rules twice:
After
Adding dedicated methods for both these rules cleans this up nicely:
This is a lot easier to read and write (in my opinion), and more importantly it provides a much more accurate and helpful error message out of the box: "The status detail field is required when status is other."
The new
multiFieldValueComparisonRule()
method I added here is based onmultiFieldComparisonRule()
but may be able to be simplified further.See #3682 and #4373.