Skip to content

Commit

Permalink
Merge pull request #11564 from creative-commoners/pulls/5/depr-form-v…
Browse files Browse the repository at this point in the history
…alid

API Deprecate passing null paramters for Form and ValidationResult methods
  • Loading branch information
GuySartorelli authored Jan 29, 2025
2 parents 6e3e655 + 70ed656 commit e7cccf6
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 2 deletions.
24 changes: 24 additions & 0 deletions src/Forms/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -1183,6 +1183,14 @@ public function FieldMap()
*/
public function sessionMessage($message, $type = ValidationResult::TYPE_ERROR, $cast = ValidationResult::CAST_TEXT)
{
if ($cast === null) {
Deprecation::notice(
'5.4.0',
'Passing $cast as null is deprecated. Pass a ValidationResult::CAST_* constant instead.',
Deprecation::SCOPE_GLOBAL
);
$cast = ValidationResult::CAST_TEXT;
}
$this->setMessage($message, $type, $cast);
$result = $this->getSessionValidationResult() ?: ValidationResult::create();
$result->addMessage($message, $type, null, $cast);
Expand All @@ -1199,6 +1207,14 @@ public function sessionMessage($message, $type = ValidationResult::TYPE_ERROR, $
*/
public function sessionError($message, $type = ValidationResult::TYPE_ERROR, $cast = ValidationResult::CAST_TEXT)
{
if ($cast === null) {
Deprecation::notice(
'5.4.0',
'Passing $cast as null is deprecated. Pass a ValidationResult::CAST_* constant instead.',
Deprecation::SCOPE_GLOBAL
);
$cast = ValidationResult::CAST_TEXT;
}
$this->setMessage($message, $type, $cast);
$result = $this->getSessionValidationResult() ?: ValidationResult::create();
$result->addError($message, $type, null, $cast);
Expand All @@ -1216,6 +1232,14 @@ public function sessionError($message, $type = ValidationResult::TYPE_ERROR, $ca
*/
public function sessionFieldError($message, $fieldName, $type = ValidationResult::TYPE_ERROR, $cast = ValidationResult::CAST_TEXT)
{
if ($cast === null) {
Deprecation::notice(
'5.4.0',
'Passing $cast as null is deprecated. Pass a ValidationResult::CAST_* constant instead.',
Deprecation::SCOPE_GLOBAL
);
$cast = ValidationResult::CAST_TEXT;
}
$this->setMessage($message, $type, $cast);
$result = $this->getSessionValidationResult() ?: ValidationResult::create();
$result->addFieldMessage($fieldName, $message, $type, null, $cast);
Expand Down
66 changes: 65 additions & 1 deletion src/ORM/ValidationResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,23 @@ public function __construct()
*/
public function addError($message, $messageType = ValidationResult::TYPE_ERROR, $code = null, $cast = ValidationResult::CAST_TEXT)
{
return $this->addFieldError(null, $message, $messageType, $code, $cast);
if ($code === null) {
Deprecation::notice(
'5.4.0',
'Passing $code as null is deprecated. Pass a blank string instead.',
Deprecation::SCOPE_GLOBAL
);
$code = '';
}
if ($cast === null) {
Deprecation::notice(
'5.4.0',
'Passing $cast as null is deprecated. Pass a ValidationResult::CAST_* constant instead.',
Deprecation::SCOPE_GLOBAL
);
$cast = ValidationResult::CAST_TEXT;
}
return $this->addFieldError('', $message, $messageType, $code, $cast);
}

/**
Expand All @@ -108,6 +124,22 @@ public function addFieldError(
$code = null,
$cast = ValidationResult::CAST_TEXT
) {
if ($code === null) {
Deprecation::notice(
'5.4.0',
'Passing $code as null is deprecated. Pass a blank string instead.',
Deprecation::SCOPE_GLOBAL
);
$code = '';
}
if ($cast === null) {
Deprecation::notice(
'5.4.0',
'Passing $cast as null is deprecated. Pass a ValidationResult::CAST_* constant instead.',
Deprecation::SCOPE_GLOBAL
);
$cast = ValidationResult::CAST_TEXT;
}
$this->isValid = false;
return $this->addFieldMessage($fieldName, $message, $messageType, $code, $cast);
}
Expand All @@ -126,6 +158,22 @@ public function addFieldError(
*/
public function addMessage($message, $messageType = ValidationResult::TYPE_ERROR, $code = null, $cast = ValidationResult::CAST_TEXT)
{
if ($code === null) {
Deprecation::notice(
'5.4.0',
'Passing $code as null is deprecated. Pass a blank string instead.',
Deprecation::SCOPE_GLOBAL
);
$code = '';
}
if ($cast === null) {
Deprecation::notice(
'5.4.0',
'Passing $cast as null is deprecated. Pass a ValidationResult::CAST_* constant instead.',
Deprecation::SCOPE_GLOBAL
);
$cast = ValidationResult::CAST_TEXT;
}
return $this->addFieldMessage(null, $message, $messageType, $code, $cast);
}

Expand All @@ -149,6 +197,22 @@ public function addFieldMessage(
$code = null,
$cast = ValidationResult::CAST_TEXT
) {
if ($code === null) {
Deprecation::notice(
'5.4.0',
'Passing $code as null is deprecated. Pass a blank string instead.',
Deprecation::SCOPE_GLOBAL
);
$code = '';
}
if ($cast === null) {
Deprecation::notice(
'5.4.0',
'Passing $cast as null is deprecated. Pass a ValidationResult::CAST_* constant instead.',
Deprecation::SCOPE_GLOBAL
);
$cast = ValidationResult::CAST_TEXT;
}
if ($code && is_numeric($code)) {
throw new InvalidArgumentException("Don't use a numeric code '$code'. Use a string.");
}
Expand Down
2 changes: 1 addition & 1 deletion tests/php/Core/Validation/ConstraintValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ public function testValidateResults(mixed $value, Constraint|array $constraints,

$this->assertCount($countViolations, $violations);
foreach ($violations as $violation) {
$this->assertSame($fieldName ?: null, $violation['fieldName']);
$this->assertSame($fieldName ?: '', $violation['fieldName']);
}
}

Expand Down

0 comments on commit e7cccf6

Please sign in to comment.