rozerro
rozerro

Reputation: 7146

Hide h:outputText element if there was no user input

<h:form id="aform">

    <p:growl id="debug-growl" showDetail="true" sticky="false" />

    <p:inputText id="expression" value="#{debug.expression}" required ="true" />

    <p:commandButton update="debug-growl" value="Process" action="#{debug.myaction}" />

    <h:outputText value="Source regular expression: #{debug.expression}" rendered="#{not empty debug.expression}" />

</h:form>

the bean

@ManagedBean
@ViewScoped
public class Debug implements Serializable {

    private String expression; //getter & setter is present

When I enter the value it then shows after submit in the h:outputText element. But if I enter empty value (which is wrong) then in the h:outputText element still present the previous value.

How can I hide the 'h:outputText' when no values was submitted?

Upvotes: 1

Views: 2683

Answers (1)

Tony Scialo
Tony Scialo

Reputation: 5957

So I see 2 issues with the above code.

  1. Your not updating the h:outputText on the commandButton click. You need to add an ID to the h:outputText and add it as an update to the command button

    <p:commandButton update="debug-growl someText" value="Process" action="#{debug.myaction}" />
    
    <h:outputText id = "someText" value="Source regular expression: #{debug.expression}" rendered="#{not empty debug.expression}" />
    
  2. The required ="true" on the inputText is not allowing the empty value to be submitted to the server. Thus the h:outputText will never be empty, therfore this value will always be rendered. To fix this, I would do my validation on the server.

    JSF

    Remove the required="true" tag

    <p:inputText id="expression" value="#{debug.expression}"/>
    

    Java

    public void myAction(){
    
        //check to make sure inputText isnt null/blank first
        if(StringUtils.isBlank(expression))
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Error", "Must provide value"));
        else{
             //business logic
        }
    }
    

Upvotes: 2

Related Questions