Document cross-field form validation#468
Open
samdark wants to merge 2 commits into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Adds documentation to the “Working with forms” guide explaining how to validate multiple fields together in a Yii form model, using a class-level Callback rule to validate a date range and attach the error to a specific field.
Changes:
- Added a new “Validating several fields together” section with an anchor for cross-field validation.
- Included a
ReportFilterFormexample demonstrating a class-levelCallbackand how to set the value path for the error.
Comment on lines
+93
to
+98
| $dateFrom = DateTimeImmutable::createFromFormat('!Y-m-d', $this->dateFrom); | ||
| $dateTo = DateTimeImmutable::createFromFormat('!Y-m-d', $this->dateTo); | ||
|
|
||
| if ($dateFrom === false || $dateTo === false) { | ||
| return $result; | ||
| } |
| } | ||
|
|
||
| if ($dateFrom > $dateTo) { | ||
| $result->addError('Start date must be earlier than or equal to end date.', [], ['dateFrom']); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Verification
yiisoft/apptemplate created withcomposer create-project yiisoft/app /tmp/yii-docs-upload-app --no-interactioncomposer require yiisoft/form-model --no-interactionphp -l src/Web/Report/Form.php && php -l verify-cross-field-validation.phpphp verify-cross-field-validation.php; verified invalid ranges fail ondateFromand valid ranges passvendor/bin/phpstan analyse src/Web/Report/Form.php verify-cross-field-validation.php --level=max --no-progressvendor/bin/psalm --no-cache --output-format=console src/Web/Report/Form.php verify-cross-field-validation.phpnpm run buildnpx markdown-link-check src/guide/start/forms.mdstill fails on the page's pre-existing absolute image links:/images/guide/start/form-error.pngand/images/guide/start/form-success.png.