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

Issue #3381353 by g089h515r806: Add support for Symfony Ulid Constraint

parent ccd05e11
No related branches found
No related tags found
No related merge requests found
......@@ -330,4 +330,15 @@ field_validation.rule.cidr_constraint_rule:
label: 'Message'
netmaskRangeViolationMessage:
type: string
label: 'Netmask range violation message'
\ No newline at end of file
label: 'Netmask range violation message'
field_validation.rule.ulid_constraint_rule:
type: mapping
label: 'Ulid constraint validation rule'
mapping:
validate_mode:
type: string
label: 'Validate mode'
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 UlidConstraintFieldValidationRule.
*
* @FieldValidationRule(
* id = "ulid_constraint_rule",
* label = @Translation("Ulid constraint"),
* description = @Translation("Ulid constraint.")
* )
*/
class UlidConstraintFieldValidationRule extends ConstraintFieldValidationRuleBase {
/**
* {@inheritdoc}
*/
public function getConstraintName(): string{
return "Ulid";
}
/**
* {@inheritdoc}
*/
public function isPropertyConstraint(): bool{
return TRUE;
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'message' => NULL,
] + parent::defaultConfiguration();
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
//copied from core.
$message = 'This is not a valid ULID.';
$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['message'] = $form_state->getValue('message');
}
}
<?php
namespace Drupal\field_validation\Plugin\Validation\Constraint;
use Symfony\Component\Validator\Constraints\Ulid;
use Symfony\Component\Validator\Constraints\UlidValidator;
/**
* Ulid constraint.
*
* @Constraint(
* id = "Ulid",
* label = @Translation("Ulid", context = "Validation"),
* )
*/
class UlidConstraint extends Ulid {
/**
* {@inheritdoc}
*/
public function validatedBy() {
return UlidValidator::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