JSF 2 - Bean Validation: validation failed -> empty values are replaced with last valid values from managed bean

前端 未结 4 1173
一向
一向 2020-12-05 08:18


I do not understand the behaviour of JSF2 during valdation. Hope someone can help me.

I have a form where the fields are validated after (ajax) submit - ok

4条回答
  •  一生所求
    2020-12-05 09:12

    I'm thinking that during normal use people won't enter a valid date, submit, and then delete the date before submitting again. I realize that you found this during testing, but probably people are just trying to successfully fill out the form and not deleting stuff they already entered, in which case perhaps keeping the last valid value is the best functionality.

    If you insist... It seems like the setter method "birthday" is never called because the value is invalid, and then when the page is redisplayed the current value of "birthday" is displayed (the current value being the valid value that was previously saved). Maybe you could write a custom validator that sets the value and THEN validates the value, but this wouldn't really make much sense. You would still have to validate the value first for cases when the users enters a text string like "yesterday" instead of a valid date, and then you would have to set the date to something based on that invalid value, and then you would have to add the message to the FacesContext. So the code would have to do something like the following.

    1) validate the date value
    2) if it's invalid then set the field to some value which makes sense and add an error message to FacesContext.
    3) if it's valid then use it.

    That's do-able, but weird because you're changing the value of the field even though the passed in value is invalid...

提交回复
热议问题