class DimensionsConstraintValidator in Physical Fields 8
Validates the dimensions constraint.
Hierarchy
- class \Drupal\physical\Plugin\Validation\Constraint\DimensionsConstraintValidator extends \Symfony\Component\Validator\ConstraintValidator
Expanded class hierarchy of DimensionsConstraintValidator
File
- src/
Plugin/ Validation/ Constraint/ DimensionsConstraintValidator.php, line 11
Namespace
Drupal\physical\Plugin\Validation\ConstraintView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DimensionsConstraintValidator:: |
public | function | Checks if the passed value is valid. |