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

Issue #3382090 by g089h515r806: Add support for Symfony Positive Constraint

parent 34f692e2
Branches
Tags
No related merge requests found
......@@ -479,3 +479,17 @@ field_validation.rule.divisible_by_constraint_rule:
message:
type: string
label: 'Message'
field_validation.rule.positive_constraint_rule:
type: mapping
label: 'Positive constraint validation rule'
mapping:
validate_mode:
type: string
label: 'Validate mode'
value:
type: string
label: 'Value'
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 PositiveConstraintFieldValidationRule.
*
* @FieldValidationRule(
* id = "positive_constraint_rule",
* label = @Translation("Positive constraint"),
* description = @Translation("Positive constraint.")
* )
*/
class PositiveConstraintFieldValidationRule extends ConstraintFieldValidationRuleBase {
/**
* {@inheritdoc}
*/
public function getConstraintName(): string{
return "Positive";
}
/**
* {@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 should be positive.';
$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\Positive;
/**
* Positive constraint.
*
* @Constraint(
* id = "Positive",
* label = @Translation("Positive", context = "Validation"),
* )
*/
class PositiveConstraint extends Positive {
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment