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

Issue #3382095 by g089h515r806: Add support for Symfony DateTime Constraint

parent c77f11a8
No related branches found
No related tags found
No related merge requests found
......@@ -534,3 +534,17 @@ field_validation.rule.date_constraint_rule:
message:
type: string
label: 'Message'
field_validation.rule.date_time_constraint_rule:
type: mapping
label: 'DateTime constraint validation rule'
mapping:
validate_mode:
type: string
label: 'Validate mode'
format:
type: string
label: 'Format'
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 DateTimeConstraintFieldValidationRule.
*
* @FieldValidationRule(
* id = "date_time_constraint_rule",
* label = @Translation("DateTime constraint"),
* description = @Translation("DateTime constraint.")
* )
*/
class DateTimeConstraintFieldValidationRule extends ConstraintFieldValidationRuleBase {
/**
* {@inheritdoc}
*/
public function getConstraintName(): string{
return "DateTime";
}
/**
* {@inheritdoc}
*/
public function isPropertyConstraint(): bool{
return TRUE;
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'format' => 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 is not a valid datetime.';
$form['format'] = [
'#type' => 'textfield',
'#title' => $this->t('Format'),
'#default_value' => $this->configuration['format'] ?? 'Y-m-d H:i:s',
'#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['format'] = $form_state->getValue('format');
$this->configuration['message'] = $form_state->getValue('message');
}
}
<?php
namespace Drupal\field_validation\Plugin\Validation\Constraint;
use Symfony\Component\Validator\Constraints\DateTime;
use Symfony\Component\Validator\Constraints\DateTimeValidator;
/**
* DateTime constraint.
*
* @Constraint(
* id = "DateTime",
* label = @Translation("DateTime", context = "Validation"),
* )
*/
class DateTimeConstraint extends DateTime {
/**
* {@inheritdoc}
*/
public function validatedBy() {
return DateTimeValidator::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