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

Issue #3382083 by g089h515r806: Add support for Symfony PasswordStrength Constraint

parent 6a3ffce2
No related branches found
No related tags found
No related merge requests found
......@@ -700,3 +700,17 @@ field_validation.rule.isin_constraint_rule:
message:
type: string
label: 'Message'
field_validation.rule.password_strength_constraint_rule:
type: mapping
label: 'PasswordStrength constraint validation rule'
mapping:
validate_mode:
type: string
label: 'Validate mode'
minScore:
type: integer
label: 'Min score'
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 PasswordStrengthConstraintFieldValidationRule.
*
* @FieldValidationRule(
* id = "password_strength_constraint_rule",
* label = @Translation("PasswordStrength constraint"),
* description = @Translation("PasswordStrength constraint.")
* )
*/
class PasswordStrengthConstraintFieldValidationRule extends ConstraintFieldValidationRuleBase {
/**
* {@inheritdoc}
*/
public function getConstraintName(): string{
return "PasswordStrength";
}
/**
* {@inheritdoc}
*/
public function isPropertyConstraint(): bool{
return TRUE;
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'minScore' => 2,
'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 ISBN.';
$min_score_options = [
1 => $this->t('Weak'),
2 => $this->t('Medium'),
3 => $this->t('Strong'),
4 => $this->t('Very Strong'),
];
$form['minScore'] = [
'#type' => 'select',
'#title' => $this->t('Type'),
'#options' => $min_score_options,
'#default_value' => $this->configuration['minScore'],
'#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['minScore'] = $form_state->getValue('minScore');
$this->configuration['message'] = $form_state->getValue('message');
}
}
<?php
namespace Drupal\field_validation\Plugin\Validation\Constraint;
use Symfony\Component\Validator\Constraints\PasswordStrength;
use Symfony\Component\Validator\Constraints\PasswordStrengthValidator;
/**
* PasswordStrength constraint.
*
* @Constraint(
* id = "PasswordStrength",
* label = @Translation("PasswordStrength", context = "Validation"),
* )
*/
class PasswordStrengthConstraint extends PasswordStrength {
/**
* {@inheritdoc}
*/
public function validatedBy(): string {
return PasswordStrengthValidator::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