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

Issue #3382084 by g089h515r806: Add support for Symfony NotIdenticalTo Constraint

parent ab788edf
No related branches found
No related tags found
No related merge requests found
......@@ -395,3 +395,17 @@ field_validation.rule.identical_to_constraint_rule:
message:
type: string
label: 'Message'
field_validation.rule.not_identical_to_constraint_rule:
type: mapping
label: 'NotIdenticalTo constraint validation rule'
mapping:
validate_mode:
type: string
label: 'Validate mode'
value:
type: string
label: 'Value'
message:
type: string
label: 'Message'
\ No newline at end of file
<?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 NotIdenticalToConstraintFieldValidationRule.
*
* @FieldValidationRule(
* id = "not_identical_to_constraint_rule",
* label = @Translation("NotIdenticalTo constraint"),
* description = @Translation("NotIdenticalTo constraint.")
* )
*/
class NotIdenticalToConstraintFieldValidationRule extends ConstraintFieldValidationRuleBase {
/**
* {@inheritdoc}
*/
public function getConstraintName(): string{
return "NotIdenticalTo";
}
/**
* {@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 not be identical to %compared_value_type %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\NotIdenticalTo;
use Symfony\Component\Validator\Constraints\NotIdenticalToValidator;
/**
* NotEqualTo constraint.
*
* @Constraint(
* id = "NotIdenticalTo",
* label = @Translation("NotIdenticalTo", context = "Validation"),
* )
*/
class NotIdenticalToConstraint extends NotIdenticalTo {
public $message = 'This value should not be identical to %compared_value_type %compared_value.';
/**
* {@inheritdoc}
*/
public function validatedBy(): string {
return NotIdenticalToValidator::class;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment