Adan
Adan

Reputation: 147

How to check a textbox value when the user press tab

The scenario was something like this:

I have 2 textboxes, say txtbox1 and txtbox2. When the user type something on txtbox1 and then press tab, txtbox1 loses focus and txtbox2 got focus. I want to check the value of txtbox1 when it loses focus. If txtbox1 value is invalid, I need to render a <h:outputText value="Invalid field" rendered=#{bean.errorFlag}/>

I used <p:ajax event="blur" /> on txtbox1.

My problem is it doesn't render the outputText even though the value of errorFlag is set to true. I also use update on ajax to update outputText, but it doesn't render it.

Upvotes: 0

Views: 341

Answers (2)

BalusC
BalusC

Reputation: 1108742

You need to specify the client ID of the to-be-updated element in update attribute.

<h:inputText id="input1" value="#{bean.input1}">
    <p:ajax event="blur" update="input1Message" />
</h:inputText>
<h:panelGroup id="input1Message">
    <h:outputText value="Invalid field!" rendered="#{bean.input1Error}" />
</h:panelGroup>

But... You're basically reinventing JSF validation and not taking benefit of JSF built-in validation API. I strongly recommend to just implement a Validator instead.

@FacesValidator("input1Validator")
public class Input1Validator implements Validator {

    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        if (isInvalid(value)) {
            throw new ValidatorException(new FacesMessage("Invalid field!"));
        }
    }

}

and use it as follows

<h:inputText id="input1" value="#{bean.input1}">
    <p:ajax event="blur" update="input1Message" />
</h:inputText>
<h:message id="input1Message" for="input1" />

This keeps your managed bean free from validation and boolean property clutter.

Upvotes: 1

eaktas
eaktas

Reputation: 273

I guess you use p:tabview. if it is correct, you can use tab change listener


you can look this site http://www.primefaces.org/showcase-labs/ui/tabviewChangeListener.jsf

Upvotes: 0

Related Questions