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

前端 未结 4 1169
一向
一向 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;
    
    
        }
    

提交回复
热议问题