0

I have a validator and it fills in both password containers with the has-error tag correctly. But how to get rid of one of the two error messages produced by the errors.rejectValue()?

password blank: true, nullable: true, validator: { password, obj, errors ->
    if (obj.password2 != password){
        errors.rejectValue('password', 'invalid.matchingpasswords')
        errors.rejectValue('password2', 'invalid.matchingpasswords')
        return false
    }
}

An other possibility would be, to tell spring to put an has-error tag into the second password container.

sch0rschi
  • 35
  • 1
  • 6
  • sounds like you want it but you don't want it. I mean it is showing the has-error tag why ? is that because it had error on that instance hence shows has-error so if you don't wish for 2nd message don't return it but then you won't get your has error, you can parse your errors and choose to ignore that is the alternative http://stackoverflow.com/questions/31732452/grails-add-validation-error-to-haserrors. try setting that error to no output for a start to see if that does what you want – V H Feb 10 '17 at 21:42
  • The problem arises when the passwords don't match, i want the Containers to have this red boarder around them, but i don't want the 2 alerts popping up, stating that the passwords do not match. – sch0rschi Feb 13 '17 at 09:25
  • http://stackoverflow.com/questions/19323652/changing-twitter-bootstrap-validation-states-of-a-control-group-in-grails as mentioned previously look at altering the error in gsp in this case. above may give a clue – V H Feb 13 '17 at 10:55
  • sorry : https://github.com/grails/grails-core/blob/master/grails-web-gsp-taglib/src/main/groovy/org/grails/plugins/web/taglib/RenderTagLib.groovy#L376-L408 basically you are looking to intercept how it normally works and for that specific message on that specific page have a different behaviour - make your own errors page that ignores that specific field error. alternatively you could make both failures only fail 1 field. it seems like a lot of work just to get highlighting around another box. may be easier just to use some js event triggering change to both when 1 changes.. and fail 1 – V H Feb 13 '17 at 12:13

2 Answers2

0

so a few ideas there I think on of the easiest options would be java script and here is how I would go about it:

<script>
<g:if test="${instance.password.hasErrors()}">
 var msg="${g.message(code:'password.error')}";
 $('#password2')[0].setCustomValidity(msg);
</g:if>
</script>

So as the page loads if password has an error then it creates/ that js msg and password custom validity. You then create password.error in messages.properties and set that to be the message.

As the user tries to submit form again it will push with that same warning. so you may need some form of other addition such as:

$('#password2').on('change', function() {
  $('#password2')[0].setCustomValidity('');
})

This now tells it to reset the custom validity to nothing when it has a new value. so user can now submit form again.

Rather easy and painless vs having to match error codes / ignoring. Now only validate and return one error for the first password javascript will do the next bit for you

V H
  • 8,382
  • 2
  • 28
  • 48
0

This is quite ugly but adding a check in the UserController.groovy:

if (action.equals("edit")) {
    model.fieldList += [property      : 'password2',
                        message       : 'user.password.repeat.label',
                        type          : InputType.PASSWORD,
                        containerClass: userInstance.base.errors?.allErrors?.field.contains("password")?'has-error col-md-6': 'col-md-6',
                        labelClass    : 'col-md-5',
                        inputClass    : 'col-md-7']
}

did the job.

sch0rschi
  • 35
  • 1
  • 6