Account created on 17 August 2007, almost 17 years ago
#

Recent comments

🇨🇳China g089h515r806

The patch looks good, I try to merge it, but failed.

🇨🇳China g089h515r806

The code is copied from Drupal core maybe Image style, Effect plugin system , which has a similar function.
I just keep it when i first create a custom plugin system.

🇨🇳China g089h515r806

You can use config field instead of base field if you need multi-value.

🇨🇳China g089h515r806

This change does not need a update path. But people need clear cache if they using this plugin.

🇨🇳China g089h515r806

1, The ID need be changed at CountryConstraintFieldValidationRule:

public function getConstraintName(): string{
    return "Country";
  }

change it to : SymfonyCountry

2,Need to provide a update path if some user use this constraint but not use "Address" module, otherwise they will get fatal error "PluginNotFoundException".

🇨🇳China g089h515r806

Symfony Isin constraint could not be used in Drupal directly:

use Symfony\Component\Validator\Constraints\Isin;

I remind this again.

🇨🇳China g089h515r806

You can enable sub module field validation legacy, which has a unique validator, the function is the same with Drupal 7.

The default unique value is using Drupal'core unique constraint, it has limit option.

🇨🇳China g089h515r806

This is an issue with Isin constraint. They can do this way in other constraint. For example, Negative, Email ...

Email is used in Drupal core, if symfony change the validate logic in Email validator:

  return 0 === $this->context->getValidator()->validate($value, new Regex(['pattern' => $email_pattern]))->count();

Other symfony project using Email constraint still works correctly, but Drupal core's Email constraint will broken.

If Drupal core does not "decouple from Symfony Validator", we should prevent them change code this way in the future.

🇨🇳China g089h515r806

I find a issue in symfony validator, for example, Isin constraint, it proxy part of its validate logic to Luhn constraint,

  return 0 === $this->context->getValidator()->validate($number, new Luhn())->count();

we have to rewrite the Isin constraint totally, otherwise we will get fatal error:

InvalidArgumentException: The passed value must be a typed data object. in Drupal\Core\TypedData\Validation\RecursiveContextualValidator->validate() (line 97 of core\lib\Drupal\Core\TypedData\Validation\RecursiveContextualValidator.php).
Drupal\Core\TypedData\Validation\RecursiveValidator->validate('30280378331005', Object) (Line: 79)
Symfony\Component\Validator\Constraints\IsinValidator->isCorrectChecksum('US0378331005') (Line: 63)
Symfony\Component\Validator\Constraints\IsinValidator->validate('US0378331005', Object) (Line: 116)

I report the issue to symfony at https://github.com/symfony/symfony/issues/51440,

They think this is a bug of Drupal Core ,

or do you mean that Drupal's validator supports validating only TypedData objects and not scalars ? In that case, I suggest fixing that in Drupal instead of doing weird hacks forcing use to add extension points in every place calling a validator recursively.

I support decouple from Symfony Validator, we could not stop them write code in symfony way.

🇨🇳China g089h515r806

1, Most constraints could used at Drupal, but not all, Isin is the only one that not works, I test them manually in field validation 3.0.0 version. We will get fatal error when using Isin constraint.

InvalidArgumentException: The passed value must be a typed data object. in Drupal\Core\TypedData\Validation\RecursiveContextualValidator->validate() (line 97 of core\lib\Drupal\Core\TypedData\Validation\RecursiveContextualValidator.php).
Drupal\Core\TypedData\Validation\RecursiveValidator->validate('30280378331005', Object) (Line: 79)
Symfony\Component\Validator\Constraints\IsinValidator->isCorrectChecksum('US0378331005') (Line: 63)
Symfony\Component\Validator\Constraints\IsinValidator->validate('US0378331005', Object) (Line: 116)

I report the issue at :https://github.com/symfony/symfony/issues/51440.

IsinValidator use following code in isCorrectChecksum function :

return 0 === $this->context->getValidator()->validate($number, new Luhn())->count();

drupal has own typed data validator base on symfony validator.Drupal use:

core\lib\Drupal\Core\TypedData\Validation\RecursiveContextualValidator

Instead of symfony's. At here $number is not a typed data. It throw a fatal exception.

This is the only exception.

Negative is inherit from LessThan, Email use Regex, If symfony validator author change the code, and use for example:

$this->context->getValidator()->validate($value, new LessThan(['value']=0))->count()

It still works for most normal symfony project, but it does not works at Drupal at all. We can not prevent them do this change.

2, If a constraint has a message, and the message has a token, symfony use a different way for text translation, here is the example:


/**
 * NotEqualTo constraint.
 *
 * @Constraint(
 *   id = "EqualTo",
 *   label = @Translation("EqualTo", context = "Validation"),
 * )
 */
class EqualToConstraint extends EqualTo {

  public $message = 'This value should be equal to %compared_value.';
  /**
   * {@inheritdoc}
   */
  public function validatedBy(): string {
    return EqualToValidator::class;
  }

}

in symfony, it is :

public $message = 'This value should be equal to {{ compared_value }}.';

We have to extends the EqualTo, and override the message string. I am not sure Drupal core 11.x support {{ compared_value }} token, if it does not support, EqualTo, LessThan, GreaterThan have this issue, we have to extends them instead register them directly.

3, Language, Locale, constraints need install symfony/intl and PHP's intl extension, we need change the composer.json to support them and change Docs to let user install PHP's intl extension.

🇨🇳China g089h515r806

allow add guide directly.

🇨🇳China g089h515r806

mark this as fixed.
A lot symfony supported. some of them useless in Drupal, some too complex to use.

we can add them later if needed.

🇨🇳China g089h515r806

manual test.

Fatal error: Class Drupal\field_validation\Plugin\Validation\Constraint\PasswordStrengthConstraint cannot extend final class Symfony\Component\Validator\Constraints\PasswordStrength in E:\xampp\htdocs\drupal10\modules\field_validation\src\Plugin\Validation\Constraint\PasswordStrengthConstraint.php on line 16

Symfony\Component\Validator\Constraints\PasswordStrength is a final class, we could not extend it.

mark this as postponed.

🇨🇳China g089h515r806

manual test case:
1) add a Isin constraint to field_test, save it.

if populate field_test "123456", get an error message.

if populate field_test "US0378331005", no error message.

🇨🇳China g089h515r806

manual test case:
1) add a Isin constraint to field_test, save it.

if populate field_test "123456", get an error message.

if populate field_test "US0378331005", get fatal error:

InvalidArgumentException: The passed value must be a typed data object. in Drupal\Core\TypedData\Validation\RecursiveContextualValidator->validate() (line 97 of core\lib\Drupal\Core\TypedData\Validation\RecursiveContextualValidator.php).
Drupal\Core\TypedData\Validation\RecursiveValidator->validate('30280378331005', Object) (Line: 79)
Symfony\Component\Validator\Constraints\IsinValidator->isCorrectChecksum('US0378331005') (Line: 63)
Symfony\Component\Validator\Constraints\IsinValidator->validate('US0378331005', Object) (Line: 116)

🇨🇳China g089h515r806

manual test case:
1) add a Issn constraint to field_test, uncheck "Case Sensitive" and "Require Hyphen", save it.

if populate field_test "123456", get an error message.

if populate field_test "1995-7459", no error message.

🇨🇳China g089h515r806

manual test case:
1) add a Isbn constraint to field_test, leave type empty, save it.

if populate field_test "123456", get an error message.

if populate field_test "0-545-01022-5", no error message.

🇨🇳China g089h515r806

manual test case:
1) add a Iban constraint to field_test, save it.

if populate field_test "123456", get an error message.

if populate field_test "AT611904300234573201", no error message.

🇨🇳China g089h515r806

manual test case:
1) add a Luhn constraint to field_test, save it.

if populate field_test "123456", get an error message.

if populate field_test "49927398716", no error message.

🇨🇳China g089h515r806

manual test case:
1) add a Currency constraint to field_test, save it.

if populate field_test "123456", get an error message.

if populate field_test "USD", no error message.

🇨🇳China g089h515r806

manual test case:
1) add a CardScheme constraint to field_test, Schemes check "AMEX", "CHINA UNIONPAY", save it.

if populate field_test "123456", get an error message.

if populate field_test "6217003810026896707" (which is CHINA UNIONPAY valid number ), no error message.

🇨🇳China g089h515r806

manual test case:
1) add a Bic constraint to field_test,save it.

if populate field_test "123456", get an error message.

if populate field_test "BKENGB2LXXX" (which is the BIC code of Bank of England ), no error message.

🇨🇳China g089h515r806

manual test case:
1) add a Country constraint to field_test, alpha3 uncheck, save it.

if populate field_test "123456", get an error message.

if populate field_test "CN", no error message.

2) if change alpha3 checked,

if populate field_test "123456", get an error message.

if populate field_test "CN", get an error message.
if populate field_test "CHN", no error message.

Note:
You need install symfony/intl and PHP's intl extension, to make this constraint works.
composer require symfony/intl

🇨🇳China g089h515r806

manual test case:
add a Locale constraint to field_test, save it.

if populate field_test "123456", get an error message.

if populate field_test "en", no error message.

if populate field_test "ar", no error message.

Note:
You need install symfony/intl and PHP's intl extension, to make this constraint works.
composer require symfony/intl

🇨🇳China g089h515r806

manual test case:
add a Language constraint to field_test, Alpha3 uncheck, save it.

if populate field_test "123456", get an error message.

if populate field_test "en", no error message.

Note:

You need install symfony/intl and PHP's intl extension, to make this constraint works.
composer require symfony/intl

🇨🇳China g089h515r806

only add constraint plugin support, no ui field validation rule support.

It has too many options, and Drupal'core has a AllowedValuesConstraint which inherit choice. I do not know how to make it work in field validation.

mark it postponed.

🇨🇳China g089h515r806

only add constraint plugin support, no ui field validation rule support.

mark it postponed

🇨🇳China g089h515r806

manual test case:
add a Time constraint to field_test, save it.

if populate field_test "123456", get an error message.

if populate field_test "12:12:12", no error message.

🇨🇳China g089h515r806

manual test case:
add a DateTime constraint to field_test, set format to "Y-m-d H:i:s" save it.

if populate field_test "123456", get an error message.

if populate field_test "2023-08-21 12:12:12", no error message.

🇨🇳China g089h515r806

manual test case:
add a Dateconstraint to field_test, save it.

if populate field_test "123456", get an error message.

if populate field_test "2023-08-21", no error message.

🇨🇳China g089h515r806

test case:
add a NotIdenticalTo constraint to field_test, set value to "abc", save it.

if populate field_test "123456", no error message.

if populate field_test "abc", get an error message

Note:
the value option support token, when using "default" validate mode.

🇨🇳China g089h515r806

manual test case:
add a DivisibleBy constraint to field_test_int, set value = 3, save it.

if populate field_test_int 8, get an error message.

if populate field_test_int 10, get an error message.

if populate field_test_int 9, no error message.

🇨🇳China g089h515r806

manual test case:
add a GreaterThanOrEqual constraint to field_test_int, set value = 10, save it.

if populate field_test_int 5, get an error message.

if populate field_test_int 10, no error message.

if populate field_test_int 20, no error message.

🇨🇳China g089h515r806

manual test case:
add a GreaterThan constraint to field_test_int, set value = 10, save it.

if populate field_test_int 5, get an error message.

if populate field_test_int 10, get an error message.

if populate field_test_int 20, no error message.

🇨🇳China g089h515r806

manual test case:
add a LessThanOrEqual constraint to field_test_int, set value = 100, save it.

if populate field_test_int 200, get an error message.

if populate field_test_int 100, no error message.

if populate field_test_int 50, no error message.

🇨🇳China g089h515r806

manual test case:
add a LessThan constraint to field_test_int, set value = 100, save it.

if populate field_test_int 200, get an error message.

if populate field_test_int 50, no error message.

🇨🇳China g089h515r806

manual test case:
add a NegativeOrZero constraint to field_test_int, save it.

if populate field_test_int 10, get an error message.

if populate field_test_int 0, no error message.
if populate field_test_int -10, no error message.

🇨🇳China g089h515r806

manual test case:
add a Negative constraint to field_test_int, save it.

if populate field_test_int 10, get an error message.

if populate field_test_int 0, get an error message.
if populate field_test_int -10, no error message.

🇨🇳China g089h515r806

manual test case:
add a Positive constraint to field_test_int, save it.

if populate field_test_int 10, no error message.

if populate field_test_int 0, no error message.
if populate field_test_int -10, get an error message.

🇨🇳China g089h515r806

manual test case:
add a Positive constraint to field_test_int, save it.

if populate field_test_int 10, no error message.

if populate field_test_int -10, get an error message

🇨🇳China g089h515r806

Only add a constraint plugin support.
Drupal use typed data system, user can choose field type.

mark it postponed.

🇨🇳China g089h515r806

Only add a constraint plugin support.
Drupal use ckeditor 5 and filter system, already can filter Suspicious Character.

mark it postponed.

🇨🇳China g089h515r806

IdenticalTo use "==="
EqualTo use "=="
A little different.

🇨🇳China g089h515r806

test case:
add a EqualTo constraint to field_test, set value to "drupal", save it.

if populate field_test "drupal", no error message.

if populate field_test "123456", get an error message

Note:
the value option support token, when using "default" validate mode.

🇨🇳China g089h515r806

test case:
add a Callback constraint to field_test, set callback to "\Drupal\mymodule\MyValidator::validate", save it.

here is the code :

namespace Drupal\mymodule;

use Symfony\Component\Validator\Context\ExecutionContextInterface;

/**
 * My validator callback.
 */
class MyValidator {

  /**
   * {@inheritdoc}
   */
  public static function validate(mixed $items, ExecutionContextInterface $context, mixed $payload): void{
    if (!$item = $items->first()) {
      return;
    }
	if(mb_strlen($item->value) < 3){
      $context->addViolation("The length is too low.");
	}	
  }
}

if populate field_test "abc", no error message.

if populate field_test "ab", get an error message.

🇨🇳China g089h515r806

test case:

add a EqualTo constraint to field_test, set value to "abc", save it.

if populate field_test "abc", no error message.

if populate field_test "123456", get an error message

Note:

the value option support token, when using "default" validate mode.

🇨🇳China g089h515r806

the value option support token, when using "default" validate mode.
you can insert a token : [node:field_other_test]

"Direct" validate mode does not support Drupal's token.

🇨🇳China g089h515r806

test case:
add a NotEqualTo constraint to field_test, set value to "abc", save it.

if populate field_test "123456", no error message.

if populate field_test "abc", get an error message

Production build 0.69.0 2024