Skip to content
Snippets Groups Projects
Commit decd642f authored by howard ge's avatar howard ge Committed by howard ge
Browse files

Issue #3382086 by g089h515r806: Add support for Symfony LessThanOrEqual Constraint

parent 0e254fe6
No related branches found
No related tags found
No related merge requests found
......@@ -413,6 +413,20 @@ field_validation.rule.not_identical_to_constraint_rule:
field_validation.rule.less_than_constraint_rule:
type: mapping
label: 'LessThan constraint validation rule'
mapping:
validate_mode:
type: string
label: 'Validate mode'
value:
type: string
label: 'Value'
message:
type: string
label: 'Message'
field_validation.rule.less_than_or_equal_constraint_rule:
type: mapping
label: 'LessThanOrEqual constraint validation rule'
mapping:
validate_mode:
type: string
......
<?php
namespace Drupal\field_validation\Plugin\FieldValidationRule;
use Drupal\Core\Form\FormStateInterface;
use Drupal\field_validation\ConstraintFieldValidationRuleBase;
use Drupal\field_validation\FieldValidationRuleSetInterface;
/**
* Provides funcationality for LessThanOrEqualConstraintFieldValidationRule.
*
* @FieldValidationRule(
* id = "less_than_or_equal_constraint_rule",
* label = @Translation("LessThanOrEqual constraint"),
* description = @Translation("LessThanOrEqual constraint.")
* )
*/
class LessThanOrEqualConstraintFieldValidationRule extends ConstraintFieldValidationRuleBase {
/**
* {@inheritdoc}
*/
public function getConstraintName(): string{
return "LessThanOrEqual";
}
/**
* {@inheritdoc}
*/
public function isPropertyConstraint(): bool{
return TRUE;
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'value' => NULL,
'message' => NULL,
] + parent::defaultConfiguration();
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
//copied from core.
$message = 'This value should be less than or equal to %compared_value.';
$form['value'] = [
'#type' => 'textfield',
'#title' => $this->t('Value'),
'#default_value' => $this->configuration['value'],
'#required' => TRUE,
];
$form['message'] = [
'#type' => 'textfield',
'#title' => $this->t('Message'),
'#default_value' => $this->configuration['message'] ?? $message,
'#maxlength' => 255,
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
$this->configuration['value'] = $form_state->getValue('value');
$this->configuration['message'] = $form_state->getValue('message');
}
/**
* {@inheritdoc}
*/
public function getReplacedConstraintOptions(array $params): array {
$constraintOptions = $this->getConstraintOptions();
$data = $this->getTokenData($params);
if (empty($data)) {
return $constraintOptions;
}
$constraintOptions['value'] = $this->tokenService->replace($constraintOptions['value'], $data);
return $constraintOptions;
}
}
<?php
namespace Drupal\field_validation\Plugin\Validation\Constraint;
use Symfony\Component\Validator\Constraints\LessThanOrEqual;
use Symfony\Component\Validator\Constraints\LessThanOrEqualValidator;
/**
* LessThanOrEqual constraint.
*
* @Constraint(
* id = "LessThanOrEqual",
* label = @Translation("LessThanOrEqual", context = "Validation"),
* )
*/
class LessThanOrEqualConstraint extends LessThanOrEqual {
public $message = 'This value should be less than or equal to %compared_value.';
/**
* {@inheritdoc}
*/
public function validatedBy(): string {
return LessThanOrEqualValidator::class;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment