Skip to content

Document cross-field form validation#468

Open
samdark wants to merge 2 commits into
masterfrom
docs-cross-field-validation
Open

Document cross-field form validation#468
samdark wants to merge 2 commits into
masterfrom
docs-cross-field-validation

Conversation

@samdark
Copy link
Copy Markdown
Member

@samdark samdark commented May 30, 2026

Summary

  • add a forms guide section for validating several fields together
  • show a class-level Callback rule that validates a date range and attaches the error to a specific field

Verification

  • Used a fresh yiisoft/app template created with composer create-project yiisoft/app /tmp/yii-docs-upload-app --no-interaction
  • Installed the guide prerequisite with composer require yiisoft/form-model --no-interaction
  • Added the example form and a verification script to the template app
  • Ran php -l src/Web/Report/Form.php && php -l verify-cross-field-validation.php
  • Ran php verify-cross-field-validation.php; verified invalid ranges fail on dateFrom and valid ranges pass
  • Ran vendor/bin/phpstan analyse src/Web/Report/Form.php verify-cross-field-validation.php --level=max --no-progress
  • Ran vendor/bin/psalm --no-cache --output-format=console src/Web/Report/Form.php verify-cross-field-validation.php
  • Ran npm run build

npx markdown-link-check src/guide/start/forms.md still fails on the page's pre-existing absolute image links: /images/guide/start/form-error.png and /images/guide/start/form-success.png.

Copilot AI review requested due to automatic review settings May 30, 2026 10:39
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 ReportFilterForm example demonstrating a class-level Callback and how to set the value path for the error.

Comment thread src/guide/start/forms.md Outdated
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;
}
Comment thread src/guide/start/forms.md Outdated
}

if ($dateFrom > $dateTo) {
$result->addError('Start date must be earlier than or equal to end date.', [], ['dateFrom']);
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.

2 participants