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

Issue #3381350 by g089h515r806: Add support for Symfony Cidr Constraint

parent 895f2b8d
No related branches found
No related tags found
No related merge requests found
......@@ -313,4 +313,21 @@ field_validation.rule.ip_constraint_rule:
label: 'Version'
message:
type: string
label: 'Message'
\ No newline at end of file
label: 'Message'
field_validation.rule.cidr_constraint_rule:
type: mapping
label: 'Cidr constraint validation rule'
mapping:
validate_mode:
type: string
label: 'Validate mode'
version:
type: string
label: 'Version'
message:
type: string
label: 'Message'
netmaskRangeViolationMessage:
type: string
label: 'Netmask range violation 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 CidrConstraintFieldValidationRule.
*
* @FieldValidationRule(
* id = "cidr_constraint_rule",
* label = @Translation("Cidr constraint"),
* description = @Translation("Cidr constraint.")
* )
*/
class CidrConstraintFieldValidationRule extends ConstraintFieldValidationRuleBase {
/**
* {@inheritdoc}
*/
public function getConstraintName(): string{
return "Cidr";
}
/**
* {@inheritdoc}
*/
public function isPropertyConstraint(): bool{
return TRUE;
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'version' => NULL,
'message' => NULL,
'netmaskRangeViolationMessage' => 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 CIDR notation.';
$netmaskRangeViolationMessage = 'The value of the netmask should be between %min and %max.';
$form['version'] = [
'#title' => $this->t('Version'),
'#type' => 'select',
'#options' => [
'4' => $this->t('V4'),
'6' => $this->t('V6'),
'all' => $this->t('ALL'),
],
'#default_value' => $this->configuration['version'],
];
$form['message'] = [
'#type' => 'textfield',
'#title' => $this->t('Message'),
'#default_value' => $this->configuration['message'] ?? $message,
'#maxlength' => 255,
];
$form['netmaskRangeViolationMessage'] = [
'#type' => 'textfield',
'#title' => $this->t('Netmask range violation message'),
'#default_value' => $this->configuration['netmaskRangeViolationMessage'] ?? $netmaskRangeViolationMessage,
'#maxlength' => 255,
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
$this->configuration['version'] = $form_state->getValue('version');
$this->configuration['message'] = $form_state->getValue('message');
$this->configuration['netmaskRangeViolationMessage'] = $form_state->getValue('netmaskRangeViolationMessage');
}
}
<?php
namespace Drupal\field_validation\Plugin\Validation\Constraint;
use Symfony\Component\Validator\Constraints\Cidr;
use Symfony\Component\Validator\Constraints\CidrValidator;
/**
* Cidr constraint.
*
* @Constraint(
* id = "Cidr",
* label = @Translation("Cidr", context = "Validation"),
* )
*/
class CidrConstraint extends Cidr {
public $netmaskRangeViolationMessage = 'The value of the netmask should be between %min and %max.';
/**
* {@inheritdoc}
*/
public function validatedBy() {
return CidrValidator::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