You are here

class DimensionsConstraintValidator in Physical Fields 8

Validates the dimensions constraint.

Hierarchy

Expanded class hierarchy of DimensionsConstraintValidator

File

src/Plugin/Validation/Constraint/DimensionsConstraintValidator.php, line 11

Namespace

Drupal\physical\Plugin\Validation\Constraint
View source
class DimensionsConstraintValidator extends ConstraintValidator {

  /**
   * {@inheritdoc}
   */
  public function validate($value, Constraint $constraint) {
    $properties = [
      'length' => t('Length'),
      'width' => t('Width'),
      'height' => t('Height'),
    ];

    // Drupal runs the validator only if !$value->isEmpty(), which means that
    // we can count on $value->unit and at least one number not being empty.
    foreach ($properties as $property => $label) {
      if (is_null($value->{$property}) || $value->{$property} === '') {
        $this->context
          ->buildViolation($constraint->emptyMessage)
          ->atPath($property)
          ->setParameter('@name', $label)
          ->addViolation();
      }
      elseif (!is_numeric($value->{$property})) {
        $this->context
          ->buildViolation($constraint->invalidMessage)
          ->atPath($property)
          ->setParameter('@name', $label)
          ->addViolation();
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DimensionsConstraintValidator::validate public function Checks if the passed value is valid.