Add support for Symfony Callback Constraint

Created on 16 August 2023, 11 months ago
Updated 19 August 2023, 10 months ago

Problem/Motivation

Add support for Symfony Callback Constraint

πŸ“Œ Task
Status

Fixed

Version

3.0

Component

Code

Created by

πŸ‡¨πŸ‡³China g089h515r806

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @g089h515r806
  • πŸ‡¨πŸ‡³China g089h515r806

    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.

  • Status changed to Fixed 10 months ago
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.69.0 2024