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

Issue #3382108 by g089h515r806: Add support for Symfony Issn Constraint

parent 7c0143ac
Branches
Tags
No related merge requests found
......@@ -672,3 +672,20 @@ field_validation.rule.isbn_constraint_rule:
message:
type: string
label: 'Message'
field_validation.rule.issn_constraint_rule:
type: mapping
label: 'Issn constraint validation rule'
mapping:
validate_mode:
type: string
label: 'Validate mode'
caseSensitive:
type: boolean
label: 'Case Sensitive'
requireHyphen:
type: boolean
label: 'Require Hyphen'
message:
type: string
label: 'Message'
<?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 IssnConstraintFieldValidationRule.
*
* @FieldValidationRule(
* id = "issn_constraint_rule",
* label = @Translation("Issn constraint"),
* description = @Translation("Issn constraint.")
* )
*/
class IssnConstraintFieldValidationRule extends ConstraintFieldValidationRuleBase {
/**
* {@inheritdoc}
*/
public function getConstraintName(): string{
return "Issn";
}
/**
* {@inheritdoc}
*/
public function isPropertyConstraint(): bool{
return TRUE;
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'caseSensitive' => FALSE,
'requireHyphen' => FALSE,
'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 is not a valid ISSN.';
$form['caseSensitive'] = [
'#type' => 'checkbox',
'#title' => $this->t('Case Sensitive'),
'#default_value' => $this->configuration['caseSensitive'],
];
$form['requireHyphen'] = [
'#type' => 'checkbox',
'#title' => $this->t('Require Hyphen'),
'#default_value' => $this->configuration['requireHyphen'],
];
$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['caseSensitive'] = $form_state->getValue('caseSensitive');
$this->configuration['requireHyphen'] = $form_state->getValue('requireHyphen');
$this->configuration['message'] = $form_state->getValue('message');
}
}
<?php
namespace Drupal\field_validation\Plugin\Validation\Constraint;
use Symfony\Component\Validator\Constraints\Issn;
use Symfony\Component\Validator\Constraints\IssnValidator;
/**
* Issn constraint.
*
* @Constraint(
* id = "Issn",
* label = @Translation("Issn", context = "Validation"),
* )
*/
class IssnConstraint extends Issn {
/**
* {@inheritdoc}
*/
public function validatedBy(): string {
return IssnValidator::class;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment