Add support for Symfony NotBlank Constraint

Created on 16 August 2023, over 1 year ago
Updated 17 August 2023, over 1 year ago

Problem/Motivation

Add support for Symfony NotBlank 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

    it does not work at all.

    namespace Drupal\field_validation\Plugin\Validation\Constraint;
    
    use Symfony\Component\Validator\Constraints\NotBlank;
    
    /**
     * NotBlank constraint.
     *
     * Overrides the symfony constraint to use Drupal-style replacement patterns.
     *
     * @Constraint(
     *   id = "NotBlank",
     *   label = @Translation("NotBlank", context = "Validation")
     * )
     */
    class NotBlankConstraint extends NotBlank {
    
      public $message = 'This value should not be blank.';
    
      /**
       * {@inheritdoc}
       */
      public function validatedBy() {
        return '\Drupal\field_validation\Plugin\Validation\Constraint\NotBlankConstraintValidator';
      }
    
    }
    
    namespace Drupal\field_validation\Plugin\Validation\Constraint;
    
    use Drupal\Core\TypedData\ComplexDataInterface;
    use Drupal\Core\TypedData\ListInterface;
    use Drupal\Core\TypedData\Validation\TypedDataAwareValidatorTrait;
    use Symfony\Component\Validator\Constraint;
    use Symfony\Component\Validator\Constraints\NotBlankValidator;
    
    /**
     * NotBlank constraint validator.
     *
     * Overrides the symfony validator to handle empty Typed Data structures.
     */
    class NotBlankConstraintValidator extends NotBlankValidator {
    
      use TypedDataAwareValidatorTrait;
    
      /**
       * {@inheritdoc}
       */
      public function validate($value, Constraint $constraint) {
    	\Drupal::messenger()->addMessage("not blank123:");  
        $typed_data = $this->getTypedData();
        if (($typed_data instanceof ListInterface || $typed_data instanceof ComplexDataInterface) && $typed_data->isEmpty()) {
          $value = NULL;
        }
    	\Drupal::messenger()->addMessage("not blank:".var_export($value, true));
        parent::validate($value, $constraint);
      }
    
    }
    

    strange thing happens.

  • 🇨🇳China g089h515r806

    after some research, find it register by drupal core:

     public function registerDefinitions() {
        $this->getDiscovery()->setDefinition('Callback', [
          'label' => new TranslatableMarkup('Callback'),
          'class' => '\Symfony\Component\Validator\Constraints\Callback',
          'type' => FALSE,
        ]);
        $this->getDiscovery()->setDefinition('Blank', [
          'label' => new TranslatableMarkup('Blank'),
          'class' => '\Symfony\Component\Validator\Constraints\Blank',
          'type' => FALSE,
        ]);
        $this->getDiscovery()->setDefinition('NotBlank', [
          'label' => new TranslatableMarkup('Not blank'),
          'class' => '\Symfony\Component\Validator\Constraints\NotBlank',
          'type' => FALSE,
        ]);
        $this->getDiscovery()->setDefinition('Email', [
          'label' => new TranslatableMarkup('Email'),
          'class' => '\Drupal\Core\Validation\Plugin\Validation\Constraint\EmailConstraint',
          'type' => ['string'],
        ]);
      }
    
    
  • 🇨🇳China g089h515r806

    If you apply not blank to a field property, if it is main property, it not works as your expect. if drupal field is empty, it skip validation, which means it skip NotBlank constraint.

    A use case for not blank, for body field, apply NotBlank to its summary property, if you set body some text, and leave summary empty,
    you will get the error.

  • Status changed to Fixed over 1 year ago
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024