Announcement Announcement Module
Collapse
No announcement yet.
Using Rules Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using Rules

    Hi,

    I am having problems implementing an or rule. What I am trying is to create a rule that checks that property1 must be equal to property2 or property3.

    so this i what I have.

    Rules rule = new Rules(MyClass.class);
    rules.addRequired("password", rules.or(rules.eqProperty("password", "oldPassword"), rules.eqProperty("password", "confirmPassword")));

    From this I get the following exception when I validate.
    org.springframework.beans.NotReadablePropertyExcep tion: Invalid property 'password' of bean class [java.lang.String]: Bean property 'password' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?

    From the error it seems the rule is applied to one of the passed in params and not on MyClass.

    Any help will be appreciated.

  • #2
    I think the problem is not with your rule but with the form where you are editing your MyClass object. Can you show us that code?

    Marios

    Comment


    • #3
      Hi,

      It actually fails in a unit test.

      Here is the unit test code.

      ...
      UserDTO userDTO = new UserDTO();

      userDTO.setPassword("pass");
      userDTO.setOldPassword("pass2");
      userDTO.setConfirmPassword("pass");

      assertTrue(rules.test(userDTO));
      ...

      Comment


      • #4
        Hmm,

        It wasn't as obvious as I expected...

        The thing is that eqProperty("password", "oldPassword") is a PropertyConstraint, and cannot be used in a required constraint.

        Instead, you must create is using the Constraints.instance() factory, and add it to the rules object directly using the add method. Here is a code that worked for me:

        Code:
        Rules rules = new Rules(MyClass.class);
        Constraints cc = Constraints.instance();
        PropertyConstraint c1 = cc.eqProperty("password", "oldPassword");
        rules.add(c1);
        The catch is that rules.add() requires a PropertyConstraint. This means that I could add c1 but I could not add an "or" constraint because it is not a PropertyConstraint. I will look into this some more tomorrow and see if I can find anything more. But I hope I have given you a direction in which to search for the solution...

        Marios

        Comment


        • #5
          thanks,

          I eventually got round the problem by doing this:

          rules.add((CompoundConstraint) or(new PropertiesConstraint("password", EqualTo.instance(), "oldPassword"), new PropertiesConstraint("password", EqualTo.instance(), "confirmPassword")));

          It seems that when you do
          Code:
          rules.add("password", eqProperty("password", "oldPassword"));
          It assumes that "password" is the object that must be checked. But if this was the case then I would have expected an extra method signature in the form of

          Code:
          public void add(Object, PropertyConstraint)
          Does this make sense or am I missing something?

          Comment

          Working...
          X