MyFaces Extensions Validator
  1. MyFaces Extensions Validator
  2. EXTVAL-82

Add the EmptyValueAwareValidationStrategy annotation to the Length and Pattern Annotations

    Details

    • Type: Improvement Improvement
    • Status: Closed
    • Priority: Minor Minor
    • Resolution: Won't Fix
    • Affects Version/s: None
    • Fix Version/s: 1.2.3, 2.0.3, 1.1.3
    • Component/s: Property Validation
    • Labels:
      None

      Description

      Adding the EmptyValueAwareValidationStrategy allows in JSF 2.0 that Length and Pattern validations are triggered (with the javax.faces.VALIDATE_EMPTY_FIELDS parameter set).
      They will cause a validation error with an empty string (Length annotation with minimum set or Pattern) so the Required annotation is no longer needed.

      Tested it out with ExtVal 2.0.3-SNAPSHOT and Myfaces 2.0.0-SNAPHOT (of 21/01) and it works as expected.

        Activity

        Rudy De Busscher created issue -
        Rudy De Busscher made changes -
        Field Original Value New Value
        Component/s Property Validation [ 12312543 ]
        Component/s Bean Validation [ 12312975 ]
        Hide
        Rudy De Busscher added a comment -

        I did some more testing to verify compatibility with previous releases (1.2.2) and standard JSF (without Extval)
        Here are the results.

        Myfaces 1.2.7

        ExtVal 1.2.2
        @Length(minimum=3, maximum=50) --> Validation error when field left empty
        @Pattern(value={"[a-z]

        {3,}"}) --> Validation error when field left empty

        ExtVal 1.2.3-SnapShot + javax.faces.VALIDATE_EMPTY_FIELDS = true
        @Length(minimum=3, maximum=50) --> No validation error when field left empty
        @Pattern(value={"[a-z]{3,}

        "}) --> No validation error when field left empty

        ValidationInterceptor.isValidationStrategyCompatibleWithValue return false since no EmptyValueAwareValidationStrategy annotation present

        ExtVal 1.2.3-SnapShot + javax.faces.VALIDATE_EMPTY_FIELDS = false
        @Length(minimum=3, maximum=50) --> No validation error when field left empty
        @Pattern(value={"[a-z]

        {3,}"}) --> No validation error when field left empty

        AbstractValidationInterceptor.validateValue because empty field and Web parameter is set to false.

        ExtVal 1.2.3-SnapShot + ExtVal-82 Implemented + javax.faces.VALIDATE_EMPTY_FIELDS = false
        @Length(minimum=3, maximum=50) --> No validation error when field left empty
        @Pattern(value={"[a-z]{3,}

        "}) --> No validation error when field left empty

        AbstractValidationInterceptor.validateValue because empty field and Web parameter is set to false.

        ExtVal 1.2.3-SnapShot + ExtVal-82 Implemented + javax.faces.VALIDATE_EMPTY_FIELDS = true
        @Length(minimum=3, maximum=50) --> Validation error when field left empty
        @Pattern(value={"[a-z]

        {3,}"}) --> Validation error when field left empty

        Since EmptyValueAwareValidationStrategy is now found


        Myfaces 2.0.0-SNAPSHOT

        ExtVal 2.0.3-SnapShot + javax.faces.VALIDATE_EMPTY_FIELDS = true
        @Length(minimum=3, maximum=50) --> No validation error when field left empty
        @Pattern(value={"[a-z]{3,}

        "}) --> No validation error when field left empty

        ValidationInterceptor.isValidationStrategyCompatibleWithValue return false since no EmptyValueAwareValidationStrategy annotation present

        ExtVal 2.0.3-SnapShot + javax.faces.VALIDATE_EMPTY_FIELDS = false
        @Length(minimum=3, maximum=50) --> No validation error when field left empty
        @Pattern(value={"[a-z]

        {3,}"}) --> No validation error when field left empty

        AbstractValidationInterceptor.validateValue because empty field and Web parameter is set to false.


        Without Extval
        Myfaces 2.0.0-SNAPSHOT

        javax.faces.VALIDATE_EMPTY_FIELDS = true
        <f:validateLength minimum="3" maximum="50"/> --> Validation error when field left empty
        <f:validateRegex pattern="[a-z]{3,}

        "/> --> Validation error when field left empty

        javax.faces.VALIDATE_EMPTY_FIELDS = false
        <f:validateLength minimum="3" maximum="50"/> --> No validation error when field left empty
        <f:validateRegex pattern="[a-z]

        {3,}

        "/> --> No validation error when field left empty

        UIInput.shouldValidateEmptyFields return false since web parameter is set to false.

        MyFaces 1.2.7
        <f:validateLength minimum="3" maximum="50"/> --> No validation error when field left empty

        UIInput.validateValue doesn't do anything since the field is empty and not required.

        Show
        Rudy De Busscher added a comment - I did some more testing to verify compatibility with previous releases (1.2.2) and standard JSF (without Extval) Here are the results. Myfaces 1.2.7 ExtVal 1.2.2 @Length(minimum=3, maximum=50) --> Validation error when field left empty @Pattern(value={" [a-z] {3,}"}) --> Validation error when field left empty ExtVal 1.2.3-SnapShot + javax.faces.VALIDATE_EMPTY_FIELDS = true @Length(minimum=3, maximum=50) --> No validation error when field left empty @Pattern(value={" [a-z] {3,} "}) --> No validation error when field left empty ValidationInterceptor.isValidationStrategyCompatibleWithValue return false since no EmptyValueAwareValidationStrategy annotation present ExtVal 1.2.3-SnapShot + javax.faces.VALIDATE_EMPTY_FIELDS = false @Length(minimum=3, maximum=50) --> No validation error when field left empty @Pattern(value={" [a-z] {3,}"}) --> No validation error when field left empty AbstractValidationInterceptor.validateValue because empty field and Web parameter is set to false. ExtVal 1.2.3-SnapShot + ExtVal-82 Implemented + javax.faces.VALIDATE_EMPTY_FIELDS = false @Length(minimum=3, maximum=50) --> No validation error when field left empty @Pattern(value={" [a-z] {3,} "}) --> No validation error when field left empty AbstractValidationInterceptor.validateValue because empty field and Web parameter is set to false. ExtVal 1.2.3-SnapShot + ExtVal-82 Implemented + javax.faces.VALIDATE_EMPTY_FIELDS = true @Length(minimum=3, maximum=50) --> Validation error when field left empty @Pattern(value={" [a-z] {3,}"}) --> Validation error when field left empty Since EmptyValueAwareValidationStrategy is now found Myfaces 2.0.0-SNAPSHOT ExtVal 2.0.3-SnapShot + javax.faces.VALIDATE_EMPTY_FIELDS = true @Length(minimum=3, maximum=50) --> No validation error when field left empty @Pattern(value={" [a-z] {3,} "}) --> No validation error when field left empty ValidationInterceptor.isValidationStrategyCompatibleWithValue return false since no EmptyValueAwareValidationStrategy annotation present ExtVal 2.0.3-SnapShot + javax.faces.VALIDATE_EMPTY_FIELDS = false @Length(minimum=3, maximum=50) --> No validation error when field left empty @Pattern(value={" [a-z] {3,}"}) --> No validation error when field left empty AbstractValidationInterceptor.validateValue because empty field and Web parameter is set to false. Without Extval Myfaces 2.0.0-SNAPSHOT javax.faces.VALIDATE_EMPTY_FIELDS = true <f:validateLength minimum="3" maximum="50"/> --> Validation error when field left empty <f:validateRegex pattern=" [a-z] {3,} "/> --> Validation error when field left empty javax.faces.VALIDATE_EMPTY_FIELDS = false <f:validateLength minimum="3" maximum="50"/> --> No validation error when field left empty <f:validateRegex pattern=" [a-z] {3,} "/> --> No validation error when field left empty UIInput.shouldValidateEmptyFields return false since web parameter is set to false. MyFaces 1.2.7 <f:validateLength minimum="3" maximum="50"/> --> No validation error when field left empty UIInput.validateValue doesn't do anything since the field is empty and not required.
        Hide
        Gerhard Petracek added a comment -

        the initial reason for the marker was to avoid such an implicit required validation.
        use-case: a property is not required but if users provide a value it has to fulfill e.g. a minimal length.
        the scenario described in this issue is easy to solve via an additional @Required constraint at the property or a custom validation strategy (as replacement for the default one) which hosts the mentioned marker.

        Show
        Gerhard Petracek added a comment - the initial reason for the marker was to avoid such an implicit required validation. use-case: a property is not required but if users provide a value it has to fulfill e.g. a minimal length. the scenario described in this issue is easy to solve via an additional @Required constraint at the property or a custom validation strategy (as replacement for the default one) which hosts the mentioned marker.
        Gerhard Petracek made changes -
        Status Open [ 1 ] Resolved [ 5 ]
        Resolution Won't Fix [ 2 ]
        Hide
        Gerhard Petracek added a comment -

        hi rudy,

        thx for the additional testing!
        but i think the behavior of javax.faces.VALIDATE_EMPTY_FIELDS without extval shows a major disadvantage.
        it's also the behavior of extval < r3. due to the mentioned disadvantage (see the previous comment) we moved away from this approach.
        since it's quite easy to change the default behavior of extval it isn't a big deal to align the behavior of extval with the default behavior of jsf2 in combination with javax.faces.VALIDATE_EMPTY_FIELDS.

        however, i think you did an important task.
        maybe we should create a wiki page to document the differences (+ reasons) and how to change the default behavior.

        regards,
        gerhard

        Show
        Gerhard Petracek added a comment - hi rudy, thx for the additional testing! but i think the behavior of javax.faces.VALIDATE_EMPTY_FIELDS without extval shows a major disadvantage. it's also the behavior of extval < r3. due to the mentioned disadvantage (see the previous comment) we moved away from this approach. since it's quite easy to change the default behavior of extval it isn't a big deal to align the behavior of extval with the default behavior of jsf2 in combination with javax.faces.VALIDATE_EMPTY_FIELDS. however, i think you did an important task. maybe we should create a wiki page to document the differences (+ reasons) and how to change the default behavior. regards, gerhard
        Hide
        Rudy De Busscher added a comment - - edited

        The changed behaviour of @Length and @Pattern is indeed something that needs to be documented. In the case a project upgrades to version x.x.3, the required check they used to have implicitly, is gone.

        The following is, I think, the easiest solution without major changes (no additional constraint or custom validation strategy required) in the codebase of the project that did an upgrade.

        public class CompatiblePropertyValidationInterceptor extends PropertyValidationInterceptor {

        @Override
        protected boolean isValidationStrategyCompatibleWithValue(
        ValidationStrategy validationStrategy, Object value) {
        if (validationStrategy instanceof LengthStrategy)

        { return value != null; }
        if (validationStrategy instanceof PatternStrategy) { return value != null; }

        return super.isValidationStrategyCompatibleWithValue(validationStrategy, value);
        }

        }

        and this class is registered in a custom startup listener as follows

        ExtValContext.getContext().denyRendererInterceptor(PropertyValidationInterceptor.class);
        ExtValContext.getContext().registerRendererInterceptor(new CompatiblePropertyValidationInterceptor());

        The functionality placed in the isValidationStrategyCompatibleWithValue method is the same as putting an EmptyValueAwareValidationStrategy on the 2 strategies.

        regards
        Rudy

        Show
        Rudy De Busscher added a comment - - edited The changed behaviour of @Length and @Pattern is indeed something that needs to be documented. In the case a project upgrades to version x.x.3, the required check they used to have implicitly, is gone. The following is, I think, the easiest solution without major changes (no additional constraint or custom validation strategy required) in the codebase of the project that did an upgrade. public class CompatiblePropertyValidationInterceptor extends PropertyValidationInterceptor { @Override protected boolean isValidationStrategyCompatibleWithValue( ValidationStrategy validationStrategy, Object value) { if (validationStrategy instanceof LengthStrategy) { return value != null; } if (validationStrategy instanceof PatternStrategy) { return value != null; } return super.isValidationStrategyCompatibleWithValue(validationStrategy, value); } } and this class is registered in a custom startup listener as follows ExtValContext.getContext().denyRendererInterceptor(PropertyValidationInterceptor.class); ExtValContext.getContext().registerRendererInterceptor(new CompatiblePropertyValidationInterceptor()); The functionality placed in the isValidationStrategyCompatibleWithValue method is the same as putting an EmptyValueAwareValidationStrategy on the 2 strategies. regards Rudy
        Gerhard Petracek made changes -
        Status Resolved [ 5 ] Closed [ 6 ]
        Gerhard Petracek made changes -
        Fix Version/s 1.1.3 [ 12313875 ]
        Fix Version/s 2.0.3 [ 12314098 ]
        Fix Version/s 1.2.3 [ 12313876 ]
        Gerhard Petracek made changes -
        Affects Version/s 1.1.3 [ 12313875 ]
        Affects Version/s 1.2.3 [ 12313876 ]
        Affects Version/s 2.0.3 [ 12314098 ]

          People

          • Assignee:
            Unassigned
            Reporter:
            Rudy De Busscher
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development