Problem/Motivation

Add support for Symfony Callback Constraint

Comments

g089h515r806 created an issue. See original summary.

  • g089h515r806 committed f8721a50 on 3.0.x
    Issue #3381360 by g089h515r806: Add support for Symfony Callback...
g089h515r806’s picture

test case:
add a Callback constraint to field_test, set callback to "\Drupal\mymodule\MyValidator::validate", save it.

here is the code :

namespace Drupal\mymodule;

use Symfony\Component\Validator\Context\ExecutionContextInterface;

/**
 * My validator callback.
 */
class MyValidator {

  /**
   * {@inheritdoc}
   */
  public static function validate(mixed $items, ExecutionContextInterface $context, mixed $payload): void{
    if (!$item = $items->first()) {
      return;
    }
	if(mb_strlen($item->value) < 3){
      $context->addViolation("The length is too low.");
	}	
  }
}

if populate field_test "abc", no error message.

if populate field_test "ab", get an error message.

g089h515r806’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.