You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@myfaces.apache.org by Yannick Goujon <yg...@yahoo.fr> on 2006/08/03 10:38:22 UTC

Validation Process problem (UIInput)

Hello,
 
I’m working on a web application using MyFaces 1.1.1 and I have a bug during validation process.
I tried MyFaces 1.1.5 snapshot and I have the same problem.
 
Here is my simple test case to reproduce the bug  :
 
Put only two fields in a form : 
-          first has a number converter
-          second is required
-          values are managed by a bean (it gets the data from a database). In my example, value of field1 = 1 and value of field2 is null in database.
 
JSP code :
…
<h:form id="form">
    <h:inputText id="field1" value="#{myBean.field1}">
            <f:convertNumber type="number"/>
    </h:inputText>
    <h:inputText id="field2" value="#{myBean.field2}" required="true"/>
    <h:commandLink id="commandLink" value="Validate" action="#{myBean.action}"/>
</h:form>
…
 
1)       First time the page is rendered : field1=1 and field2 = null. When I validate the form, I have a message because field2 is required. In the html form, fields have the same values. That’s Ok.
2)       Now entered ‘xxxxxxx’ if field1 and validate. There are 2 messages (value of field1 is not a valid number and field2 is required). In the html form, field1= ‘xxxxxxx’ and field2 is empty. That’s Ok
3)       Now remove the value in field1 and validate. There is a message (field2 is required). In the html form, field1=1 and field2 is empty. That’s not the attempted result. Why field1 is not empty ?
 
There is a problem in the process validation (validate method) of the component UIInput
 
// validate method of the UIInput
public void validate(FacesContext context)
    {
        if (context == null) throw new NullPointerException("context");
        Object submittedValue = getSubmittedValue();
        if (submittedValue == null) return;
 
        Object convertedValue = getConvertedValue(context, submittedValue);
 
        if (!isValid()) return;
 
        validateValue(context, convertedValue);
 
        if (!isValid()) return;
 
        Object previousValue = getValue();
        setValue(convertedValue);
        setSubmittedValue(null);
        if (compareValues(previousValue, convertedValue))
        {
            queueEvent(new ValueChangeEvent(this, previousValue, convertedValue));
        }
    }
 
In my test case 3), the submitted value of field1 is ‘’ (empty string). The converter returns null without conversion error (that’s OK). If you look in the validate method, the converted value (null)  is set to the value of the component and submitted value is set to null.
 
But during rendering phase, to display the relevant value in the html input, the renderer of the component get the value if the submitted value is null
 
// code of the getValue method of the component
public Object getValue()
    {
        if (_value != null) return _value;
        ValueBinding vb = getValueBinding("value");
        return vb != null ? (Object)vb.getValue(getFacesContext()) : null;
    }
 
If value is null, the value is retrieved by means of the value binding,  that’s why the field of the form is filled by the value of the database instead of an empty string.
 
Is it a known bug ? Any workaround ?
 
Thanks.
 
Yannick