users@woodstock.java.net

Re: Setting textField valid=false in table.

From: Dan Labrecque <Dan.Labrecque_at_Sun.COM>
Date: Tue, 08 Jan 2008 12:44:23 -0500

To set unique component values in a table, you must use a JSF value
expression. (Only one component is used, but value expressions allow you
to change each value per row.) Although "valid" is not a tag attribute,
you can still set a value expression for it providing you have a
component "binding". Once you have the component instance, you can use
the following code to create a value expression dynamically via Java
(i.e., your backing bean).

    import javax.el.ValueExpression;
    import javax.faces.context.FacesContext;
    import javax.faces.component.UIComponent;
    ...

    /**
     * Helper method to set value expression property.
     *
     * @param component The UIComponent to set a value expression for.
     * @param name The name of the value expression property.
     * @param expression The expresion for the value expression.
     */
    public static void setValueExpression(UIComponent component, String
name,
            String expression) {
        if (expression == null) {
            return;
        }
        FacesContext context = FacesContext.getCurrentInstance();
        component.setValueExpression(name, createValueExpression(
                context, expression, Object.class));
    }

    public static ValueExpression createValueExpression(
            FacesContext context, String expr, Class value) {
        return context.getApplication().getExpressionFactory().
            createValueExpression(context.getELContext(), expr, value);
    }

That said, you might want to file a bug/RFE against textField to help
make this easier. Highlighting invalid textField values was a feature
recently added to Woodstock -- tables may have been overlooked?

Dan

Steven Bell wrote:
> Hi,
>
> I have a table used to enter line detail information. It's basically
> a table with several columns, each one has a textField. When the form
> is submitted I need to display and validation errors. At one point in
> a backing bean I know that I have an error for a specific field on a
> specific row. I add a FacesMessage to the context and I can show the
> message in a messageGroup or message with the for attribute set
> easily, but I cannot figure out how to set the textFields valid
> property to false, so that it renders as in error (yellow background).
>
> About the only think I could think to try was to get the component out
> of the FacesContext and set valid to false, but there is only one
> textField for the column at that point, and setting it's valid
> property seems to have no effect.
>
> Any ideas?
>
> --
> Regards,
>
> Steven Bell