I am having a problem with my grails project right now, I wanted to write a matches, that would best fit the allowable characters for my input fields. I have written a matches, that throws an error message if the input characters contain a single space character., but no longer works if the input contains a series of spaces. This is my code:
newPassword nullable: false, minSize: 8, matches: /[0-9a-zA-Z_\[\]\\\^\$\.\|\?\*\+\(\)~!@#%&-=]*/, blank: false, notEqualToAnyProperty:['username', 'emailAddress'],validator: { value, obj ->
(obj.currentPassword != value && value != '')
}
These are the sample inputs:
1) ‘rain drops’ – my matches works, it returns an error message that the input contains an invalid character.
2) ‘ ‘ – series of spaces; my program returns an error message that should be displayed for the blank constraint instead of displaying the error message for my matches constraint which is the, “input contains an invalid character”, since the input doesn’t match the allowable input characters.
Any help from you guys? Thanks!
You shouldn’t need to add any begin (^) or end ($) tags to your regular expression, as the
matchesconstraint attempts to match the entire String input against the Pattern, thus your first test correctly fails against the constraint.For your second test where the input is only a series of spaces
' ', yourmatchesconstraint will never run. Bothblankandnullableare constraints which can block the running of other constraints if they fail. Thematchesconstraint will not run in your case because theblankconstraint returns a failure on an all-whitespace input.