Change login properties within ui: repeat

I would like to change the "required" property in the InputText, which is inside the ui: repeat, but I cannot access the component from the ManagedBean:

<h:selectManyCheckbox id="required" value="#{test.required}" layout="lineDirection" converter="javax.faces.Integer"> <f:ajax event="change" listener="#{test.update}" /> <f:selectItems value="#{test.selectable}"></f:selectItems> </h:selectManyCheckbox> <ui:repeat value="#{test.names}" var="name" id="repeat"> <h:panelGrid columns="3"> <h:outputLabel id="nameLabel">name:</h:outputLabel> <h:inputText id="name" value="#{name}" validator="#{test.validateName}" /> <h:message for="name"></h:message> </h:panelGrid> </ui:repeat> 

I am trying to use the findComponent method, but it does not work:

 public void update(AjaxBehaviorEvent event) { for(Integer i: selectable) { UIViewRoot vr = FacesContext.getCurrentInstance().getViewRoot(); HtmlInputText input = (HtmlInputText)vr.findComponent("form:repeat:"+i+":name"); input.setRequired(required.contains(i)); } } 
0
source share
1 answer

ui:repeat does not repeat the components in the root of the view; it repeats the output of the component in the HTML output.

There are several ways to achieve this. One of them is to use the value object instead and set the required value. For instance. a List<Item> , where Item has the String name and boolean required properties.

 <ui:repeat value="#{test.items}" var="item" id="repeat"> <h:panelGrid columns="3"> <h:outputLabel id="nameLabel">name:</h:outputLabel> <h:inputText id="name" value="#{item.name}" required="#{item.required}" validator="#{test.validateName}" /> <h:message for="name"></h:message> </h:panelGrid> </ui:repeat> 

There are more ways, but since the version of JSF you are using and the functional requirements are unclear, it only guesses which path is most applicable in your case.

+1
source

Source: https://habr.com/ru/post/982638/


All Articles