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

Issue #3377757 by g089h515r806: make "default" validate mode works for constraint rule

parent e2ce7bf7
No related branches found
No related tags found
No related merge requests found
......@@ -4,6 +4,8 @@ namespace Drupal\field_validation\Plugin\Validation\Constraint;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Drupal\field_validation\ConstraintFieldValidationRuleBase;
use Drupal\Core\Validation\ConstraintValidatorFactory;
/**
* Validates the FieldValidation constraint.
......@@ -53,38 +55,106 @@ class FieldValidationConstraintValidator extends ConstraintValidator {
return;
}
//Divide them into 2 array, one for field, the other for property
$rules_field = [];
$rules_property = [];
foreach ($rules_available as $rule) {
$is_constraint_rule = ($rule instanceof ConstraintFieldValidationRuleBase);
$validate_mode = $rule->getConfiguration()['data']['validate_mode'] ?? "default";
// Remove "direct" mode rule.
if ($validate_mode == "direct"){
continue;
}
if ($is_constraint_rule && $validate_mode =="default" && (!$rule->isPropertyConstraint())) {
$rules_field[] = $rule;
}else{
$rules_property[] = $rule;
}
}
$validator_manager = \Drupal::service('plugin.manager.field_validation.field_validation_rule');
$constraint_manager = \Drupal::service('validation.constraint');
$class_resolver = \Drupal::service('class_resolver');
$constraint_validator_factory = new ConstraintValidatorFactory($class_resolver);
// Field level validation,
foreach ($rules_field as $rule) {
$constraint_name = $rule->getConstraintName();
$constraint_options = $rule->getConstraintOptions();
$real_constraint = $constraint_manager->createInstance($constraint_name, $constraint_options);
$validator = $constraint_validator_factory->getInstance($real_constraint);
$validator->initialize($this->context);
$validator->validate($items, $real_constraint);
}
// Property level validation
$params = [];
$params['items'] = $items;
$params['context'] = $this->context;
if ($items->count() !== 0) {
foreach ($items as $delta => $item) {
$validator_manager = \Drupal::service('plugin.manager.field_validation.field_validation_rule');
// You can hard code configuration or you load from settings.
foreach ($rules_available as $rule) {
foreach ($rules_property as $rule) {
$column = $rule->getColumn();
$value = $item->{$column};
$params['value'] = $value;
$params['delta'] = $delta;
$config = [];
$params['rule'] = $rule;
$params['ruleset'] = $ruleset;
$plugin_validator = $validator_manager->createInstance($rule->getPluginId(), $config);
$plugin_validator->validate($params);
// Add support property constraint
$is_constraint_rule = ($rule instanceof ConstraintFieldValidationRuleBase);
$validate_mode = $rule->getConfiguration()['data']['validate_mode'] ?? "default";
// \Drupal::logger('field_validation')->notice("validate_mode:" . var_export($validate_mode,true));
if ($is_constraint_rule && $validate_mode == "default") {
$constraint_name = $rule->getConstraintName();
$constraint_options = $rule->getConstraintOptions();
if ($rule->isPropertyConstraint()) {
$real_constraint = $constraint_manager->createInstance($constraint_name, $constraint_options);
$constraint_validator_factory = new ConstraintValidatorFactory($class_resolver);
$validator = $constraint_validator_factory->getInstance($real_constraint);
$validator->initialize($this->context);
$validator->validate($value, $real_constraint);
}
}else{
$params['value'] = $value;
$params['delta'] = $delta;
$config = [];
$params['rule'] = $rule;
$params['ruleset'] = $ruleset;
$plugin_validator = $validator_manager->createInstance($rule->getPluginId(), $config);
$plugin_validator->validate($params);
}
}
}
}
else {
$validator_manager = \Drupal::service('plugin.manager.field_validation.field_validation_rule');
// You can hard code configuration or you load from settings.
foreach ($rules_available as $rule) {
$params['value'] = NULL;
$params['delta'] = NULL;
$config = [];
$params['rule'] = $rule;
$params['ruleset'] = $ruleset;
$plugin_validator = $validator_manager->createInstance($rule->getPluginId(), $config);
$plugin_validator->validate($params);
foreach ($rules_property as $rule) {
$value = NULL;
// Add support property constraint
$is_constraint_rule = ($rule instanceof ConstraintFieldValidationRuleBase);
$validate_mode = $rule->getConfiguration()['data']['validate_mode'] ?? "default";
// \Drupal::logger('field_validation')->notice("is_constraint_rule:" . var_export($is_constraint_rule,true));
// \Drupal::logger('field_validation')->notice("validate_mode:" . var_export($validate_mode,true));
if ($is_constraint_rule && $validate_mode == "default") {
$constraint_name = $rule->getConstraintName();
$constraint_options = $rule->getConstraintOptions();
if ($rule->isPropertyConstraint()) {
$real_constraint = $constraint_manager->createInstance($constraint_name, $constraint_options);
$constraint_validator_factory = new ConstraintValidatorFactory($class_resolver);
$validator = $constraint_validator_factory->getInstance($real_constraint);
$validator->initialize($this->context);
$validator->validate($value, $real_constraint);
}
}else{
$params['value'] = NULL;
$params['delta'] = NULL;
$config = [];
$params['rule'] = $rule;
$params['ruleset'] = $ruleset;
$plugin_validator = $validator_manager->createInstance($rule->getPluginId(), $config);
$plugin_validator->validate($params);
}
}
}
}
......
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