#1845546: Implement validation for the TypedData API introduces validation based upon the symfony validator for Drupal 8. For translation violation messages we need to extract the messages defined at the validation constraint classes, e.g.

class RangeConstraint extends Range {

  public $minMessage = 'This value should be %limit or more.';
  public $maxMessage = 'This value should be %limit or less.';

}

Those messages are defined on classes with the "Constraint" suffix and are always public properties that by convention end with the "Message" suffix or are it's a single public $message;

In addition to that there is format_plural() support which is special as it combines singular and plural forms in a single message, e.g.:

class LengthConstraint extends Length {

  public $maxMessage = 'This value is too long. It should have %limit character or less.|This value is too long. It should have %limit characters or less.';
  public $minMessage = 'This value is too short. It should have %limit character or more.|This value is too short. It should have %limit characters or more.';
  public $exactMessage = 'This value should have exactly %limit character.|This value should have exactly %limit characters.';
}

Thus it uses the pattern $message = "singular|plural"; with "|" being a special delimiter here (introduced by symfony).

Files: 
CommentFileSizeAuthor
#19 interdiff.txt1.18 KBGábor Hojtsy
#15 potx-constraint-1903362-15.patch4.91 KBherom
FAILED: [[SimpleTest]]: [MySQL] 159 pass(es), 1 fail(s), and 0 exception(s). View
#15 interdiff-1903362-10-15.txt411 bytesherom
#18 potx-constraint-1903362-18.patch4.95 KBherom
PASSED: [[SimpleTest]]: [MySQL] 160 pass(es). View
#18 interdiff-1903362-15-18.txt747 bytesherom
#13 potx-constraint-1903362-13.patch4.92 KBherom
FAILED: [[SimpleTest]]: [MySQL] Invalid PHP syntax in sites/default/modules/potx/tests/TestConstraint.php. View
#13 interdiff-1903362-10-13.txt3.34 KBherom
#10 potx-constraint-1903362-10.patch3.23 KBherom
PASSED: [[SimpleTest]]: [MySQL] 154 pass(es). View
#8 potx-constraint-1903362-8.patch3.07 KBherom
FAILED: [[SimpleTest]]: [MySQL] 154 pass(es), 0 fail(s), and 129 exception(s). View

Comments

Gábor Hojtsy’s picture

Issue tags: +D8MI

Thanks!

Gábor Hojtsy’s picture

Title: Support extracting validation constraint messages » Support fro Drupal 8 validation constraint messages

Retitling.

Gábor Hojtsy’s picture

Title: Support fro Drupal 8 validation constraint messages » Support for Drupal 8 validation constraint messages
Gábor Hojtsy’s picture

Issue tags: +Drupal 8 compatibility

Also tag with this unique tag for findability.

Gábor Hojtsy’s picture

Version: 7.x-2.x-dev » 6.x-3.x-dev
herom’s picture

I'll work on this.

herom’s picture

Assigned: Unassigned » herom
herom’s picture

Status: Active » Needs review
FileSize
3.07 KB
FAILED: [[SimpleTest]]: [MySQL] 154 pass(es), 0 fail(s), and 129 exception(s). View

patch added.

Status: Needs review » Needs work

The last submitted patch, potx-constraint-1903362-8.patch, failed testing.

herom’s picture

Status: Needs work » Needs review
FileSize
3.23 KB
PASSED: [[SimpleTest]]: [MySQL] 154 pass(es). View

fixing.

herom’s picture

Assigned: herom » Unassigned
Gábor Hojtsy’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Thanks a lot for working on this, looks like good job! First of all this needs test coverage. Then some other minor things:

  1. +++ b/potx.inc
    @@ -212,6 +212,11 @@ function _potx_process_file($file_path, $strip_prefix = 0, $save_callback = '_po
    +  $constraint_extract = false;
    +  if (substr($name_parts['filename'], -10) == 'Constraint' && $api_version > POTX_API_7) {
    +    $constraint_extract = true;
    +  }
    

    FALSE, TRUE :)

  2. +++ b/potx.inc
    @@ -226,11 +231,14 @@ function _potx_process_file($file_path, $strip_prefix = 0, $save_callback = '_po
    +           $key = ($token[0] == T_DOC_COMMENT) ? 'T_DOC_COMMENT' : ($constraint_match ? 'T_CONSTRAINT' : $token[1]);
    
    @@ -1752,6 +1764,34 @@ function _potx_find_routing_yml_file_strings($code, $file_name, $save_callback,
    +  foreach ($_potx_lookup['T_CONSTRAINT'] as $key => $ti) {
    

    Seems like T_CONSTRAINT is something we made up :) I think we should name this something very specific to our situation, so we don't mix it up with the PHP built-in tokens.

  3. +++ b/potx.inc
    @@ -1752,6 +1764,34 @@ function _potx_find_routing_yml_file_strings($code, $file_name, $save_callback,
    +function _potx_find_constraint_messages($file_name, $save_callback) {
    +  global $_potx_tokens, $_potx_lookup;
    

    Would be important to have a doc block for this explaining what this does, the structure it is looking for, etc. like other functions.

herom’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
FileSize
3.34 KB
4.92 KB
FAILED: [[SimpleTest]]: [MySQL] Invalid PHP syntax in sites/default/modules/potx/tests/TestConstraint.php. View

patch update.

Status: Needs review » Needs work

The last submitted patch, potx-constraint-1903362-13.patch, failed testing.

herom’s picture

Status: Needs review » Needs work
FileSize
411 bytes
4.91 KB
FAILED: [[SimpleTest]]: [MySQL] 159 pass(es), 1 fail(s), and 0 exception(s). View

how did that get there?

EDIT: I just realized the 10-15 interdiff is wrong. Instead, read the 10-13 interdiff, just without the strange "extends Range" that got in there.

herom’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, potx-constraint-1903362-15.patch, failed testing.

herom’s picture

Status: Needs work » Needs review
FileSize
747 bytes
4.95 KB
PASSED: [[SimpleTest]]: [MySQL] 160 pass(es). View

uhh. I failed at my own tests!

Gábor Hojtsy’s picture

Version: 6.x-3.x-dev » 7.x-2.x-dev
Status: Needs work » Patch (to be ported)
FileSize
1.18 KB

Yay, thanks again! I committed this patch with the attached minor changes :) Now to be ported to 7.x-2.x. (including the changes in the interdiff).

Gábor Hojtsy’s picture

This change is now rolled out to localize.drupal.org! Reparssed 8.0-alpha3 with this. It did find constraint messages :) https://localize.drupal.org/translate/languages/hu/translate?sid=1864953

fago’s picture

Awesome!

fago’s picture

Issue summary: View changes

Updated issue summary.

  • Gábor Hojtsy committed f95a846 on 7.x-2.x
    Issue #1903362 by herom | fago: Support for Drupal 8 validation...

  • Gábor Hojtsy committed f95a846 on 7.x-3.x
    Issue #1903362 by herom | fago: Support for Drupal 8 validation...
SebCorbin’s picture

Version: 7.x-2.x-dev » 7.x-3.x-dev
Issue summary: View changes
Status: Patch (to be ported) » Closed (fixed)

Ported to 7.x-3.x as it has been branched directly from 6.x-3.x