Announcement Announcement Module
No announcement yet.
"Local" validation rule? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • "Local" validation rule?

    Sorry for returning with a new issue, but here is another problem that I'm stuck with: I'm building up for my application a number of wizards, each one containing a certain number of fields.
    I have defined my "DefaultRulesSource" bean inside the configuration file for the application context.
    Here i've created a number of methods - each one responsible for defining validation rules for classes inside my project.
    For instance - String properties must contain strictly alphanumerical characters.
    I've seen that these set of rules are loaded at application startup.
    But at some point in the "evolution" of my program i need to add a "particular" type of validation rule, one i cannot define before: i need to check that that value the user enters inside a text field is unique among a list of values already entered at a previous moment of time. I know that there is already a Constraint for checking if a value is unique within a list of values, but my problem is that this list is updated very often and when the application loads itself this list is not even built.
    I've thought to define a List property in my DefaultRuleSource bean, that i should set right before checking the "uniqueness"(? - please execuse my poor english), by obtaining the bean instance from the application context. However this solution seems not elegant at all, i was wondering if there is a better approach to this?
    For instance, having the "permanent" validation rules for the classes, at the time needed, to be able to "append" somehow the "context specific" constraint to the set of constraints already defined for that field.

    Thank you in advance, i'm very curious if anybody has a better way to integrate this.

  • #2
    You could define a custom constraint which depends on two fields. One field contains the collection of unique values and the other field the value which is entered by the user. The constraint will then run either when the field with the unique values or the value entered by the user changes. It works like the existing property constraints which depends on more than one field.

    To make it a little bit easier for you I've checked in a PropertyInGroupConstraint into the sandbox module which can be used to determine if a value is in a group of values like InGroup constraint. But this constraint allows you to define a "dynamic" value for your "InGroup" values. Since you need the opposite (test if a value is not in the list) you can combine this constraint with the existing NegatedPropertyConstraint.


    • #3
      Thanks a lot for your help!
      I'll take a look on your class in the sandbox module and get back with an answer as soon as I can.