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

前端 未结 4 1166
一向
一向 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:07

    Had a similar issue where a value loaded in from the backing bean would get reset when the field was blanked out and another component failed validation. I had to make a slight addition to BalusC's code to make it work.

    protected String getCurrentValue(FacesContext context,
                                         UIComponent component) {
    
            if (component instanceof UIInput && !((UIInput) component).isValid()) {
                Object submittedValue = ((UIInput) component).getSubmittedValue();
                if (submittedValue != null) {
                    // value may not be a String...
                    return submittedValue.toString();
                } else {
                    return null;
                }
            }
    
    
            String currentValue = null;
            Object currentObj;
    
            if ( component instanceof UIInput && ((UIInput)component).isLocalValueSet() )
            {
               currentObj = ((UIInput)component).getLocalValue();
            }
            else {
                currentObj = getValue(component);
            }
    
            if (currentObj != null) {
                currentValue = getFormattedValue(context, component, currentObj);
            }
            return currentValue;
    
    
        }
    
    0 讨论(0)
  • 2020-12-05 09:09

    Your particular problem is caused by

    <context-param>
        <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
        <param-value>true</param-value>
    </context-param>
    

    and a bug (at least, an oversight) in HtmlBasicRenderer#getCurrentValue() of Mojarra:

    if (component instanceof UIInput) {
        Object submittedValue = ((UIInput) component).getSubmittedValue();
        if (submittedValue != null) {
            // value may not be a String...
            return submittedValue.toString();
        }
    }
    
    String currentValue = null;
    Object currentObj = getValue(component);
    if (currentObj != null) {
        currentValue = getFormattedValue(context, component, currentObj);
    }
    return currentValue;
    

    Normally, the submitted value is set to null when the UIInput component is successfully converted and validated. When JSF is about to redisplay the value, it first checks if the submitted value is not null before proceeding to redisplay the model value. However, with this context parameter, it is null instead of an empty string when it is invalid and thus it will always redisplay the original model value when you remove the initial value of a required field.

    To test it, set that context param value to false or remove it altogether. You'll see that it works as intended. However, it will bring back the disadvantage that your model values will be cluttered with empty strings on empty but non-required fields and you'll lose the advantage of using @NotNull annotation of JSR 303 bean validation.

    To fix this, you've to alter the first part of HtmlBasicRenderer#getCurrentValue() as follows:

    if (component instanceof UIInput && !((UIInput) component).isValid()) {
        Object submittedValue = ((UIInput) component).getSubmittedValue();
        if (submittedValue != null) {
            // value may not be a String...
            return submittedValue.toString();
        } else {
            return null;
        }
    }
    

    I've already reported it to Mojarra guys as issue 2262.

    0 讨论(0)
  • 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...

    0 讨论(0)
  • 2020-12-05 09:14

    Aaron already descripes the behaviour.

    The problem I've been described exists also by clicking the 'newContact' button. If the first submit is not valid (birthday was entered, name-field is empty) an error message is shown. ok.

    Afterwards the 'newContact' Button do not refresh (clear) the view. Although the model was resetted (contact = new Contact()).

    I found some tipps here: http://wiki.apache.org/myfaces/ClearInputComponents

    Here is my solution:

    public void newContact(ActionEvent ae) {
        contact = new Contact();
        contact.setBirthday(new Date()); //for testing only
    
        resetForm(ae.getComponent());
    }
    
    private void resetForm(UIComponent uiComponent) {
        //get form component
        UIComponent parentComponent = uiComponent.getParent();
        if (uiComponent instanceof UIForm)
            resetFields(uiComponent);
        else if (parentComponent != null)
            resetForm(parentComponent);
        else
            resetFields(uiComponent);
    
    }
    
    private void resetFields(UIComponent baseComponent) {
        for (UIComponent c : baseComponent.getChildren()) {
            if (c.getChildCount() > 0)
                resetFields(c);
    
            if (c instanceof UIInput)
                ((UIInput) c).resetValue();
        }
    }
    
    0 讨论(0)
提交回复
热议问题