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

Issue #3382096 by g089h515r806: Add support for Symfony Time Constraint

parent 8130a0a9
Branches
Tags
No related merge requests found
...@@ -548,3 +548,14 @@ field_validation.rule.date_time_constraint_rule: ...@@ -548,3 +548,14 @@ field_validation.rule.date_time_constraint_rule:
message: message:
type: string type: string
label: 'Message' label: 'Message'
field_validation.rule.time_constraint_rule:
type: mapping
label: 'Time constraint validation rule'
mapping:
validate_mode:
type: string
label: 'Validate mode'
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 TimeConstraintFieldValidationRule.
*
* @FieldValidationRule(
* id = "time_constraint_rule",
* label = @Translation("Time constraint"),
* description = @Translation("Time constraint.")
* )
*/
class TimeConstraintFieldValidationRule extends ConstraintFieldValidationRuleBase {
/**
* {@inheritdoc}
*/
public function getConstraintName(): string{
return "Time";
}
/**
* {@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 value is not a valid time.';
$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\Time;
use Symfony\Component\Validator\Constraints\TimeValidator;
/**
* Time constraint.
*
* @Constraint(
* id = "Time",
* label = @Translation("Time", context = "Validation"),
* )
*/
class TimeConstraint extends Time {
/**
* {@inheritdoc}
*/
public function validatedBy() {
return TimeValidator::class;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment