Have at least one input field as required dynamically in primefaces [duplicate] - jsf

This question already has answers here:
How to validate if at least one of multiple input fields is entered?
(2 answers)
Closed 7 years ago.
i have three fields i use for my search , but only one 'em is required (at least one of'em should be submited so i tried to have a conditionned required as seen in code below , but takes no effect , as all three fields are all the time required when clicking the command button , even when one field has value . should i use jquery to resolve this on button click.
<h:form id="search_form">
<p:inputText name="field1" id="field1" type="text"
required="#{searchBean.SearchCriteria.field3==null and searchBean.searchCriteria.field2==null}" value="#{searchBean.searchCriteria.field1}"></p:inputText>
<p:inputText name="field2" id="field2" type="text"
required="#{searchBean.SearchCriteria.field1==null and searchBean.searchCriteria.field3==null}" value="#{searchBean.searchCriteria.field2}"></p:inputText>
<p:inputText name="field3" id="field3" type="text"
required="#{searchBean.SearchCriteria.field1==null and searchBean.searchCriteria.field3==null}" value="#{searchBean.searchCriteria.field3}"></p:inputText>
<p:commandButton id="search_button" ajax="false" title="#process="#form" action{#searchBean.search()}" />
</h:form>

Have your bean's search() method run the validation , return null and possibly add a facesMessage if none of those fields is filled up.

Related

Substitute to p:dataTable

I would like to display a form using a bean with the framework JSF and primefaces.
It is very simple to do with dataTable as followed but it is also not suitable at all for this form.
<p:dataTable value="#{utilisateur.questionnaire.questions}" var="question" styleClass="borderless" class="ui-g">
<p:column value="" class="ui-g-2">#{question.titreQuestion}</p:column>
<p:column value="" class="ui-g-5">#{question.question}</p:column>
<p:column value="" class="ui-g-5"><h:inputText id="firstName" value="default}" label=""></h:inputText></p:column>
</p:dataTable>
<h:commandButton value="Submit" action="../admin.xhtml"></h:commandButton>
As you can see below it does not look very professional. There is an empty head bar. Moreover, sometimes a question has no title in the left column so there is an empty cell (which would not be important if the cell had not a background color).
Thus, have you any recommendation of a tag for this type of form where I could have two string (retrieved via the bean using #{utilisateur.questionnaire.questions}) on the first line and the inputText on the second one and so on.
I have tried with h:panelGrid, unfortunately you cannot have * value="#{utilisateur.questionnaire.questions}" * inside the tag...

p:selectBooleanCheckBox does not work inside a p:dialog [duplicate]

This question already has answers here:
Can you nest html forms?
(21 answers)
How to use <h:form> in JSF page? Single form? Multiple forms? Nested forms?
(2 answers)
Closed 6 years ago.
I'm using PrimeFaces 5.3 with the following code:
<p:dialog id="dlg1" appendTo="#(body)" header="Confirm Dialog" widgetVar="dlg1" modal="true">
<h:form id="dialogform">
<h:outputText id="confirmlabel" value="Are you Sure you want to #{reportRunnerNamedBean.currentCommand} ?" />
<br/>
<center>
Check to send only missed emails:<p:selectBooleanCheckbox id="sendOnlyMissedBox" value="#{reportRunnerNamedBean.sendMissedOnly}"></p:selectBooleanCheckbox><br/>
<p:commandButton id="yesButton" value="Yes" action="#{reportRunnerNamedBean.runCurrentCommand()}" onstart="startWait();PF('waitDialog').show();" oncomplete="PF('waitDialog').hide();stopWait();" onclick="PF('dlg1').hide();" process="#form"/>
<p:commandButton id="noButton" process="#this" value="No" onclick="PF('dlg1').hide();"/>
</center>
</h:form>
</p:dialog>
No matter what, the backing bean value for the sendMissedOnly is set to false. I have confirmed this by attaching a debugger. I have even tried adding ajax to the box, it is still false every time, no matter if it is checked or not. Does p:selectBooleanCheckbox just not work in a dialog?
I figured this out. It was because the dialog was in an form. It seems that when a dialog is in a form , and you have a form within the dialog itself, the component will not be processed. It seems JSF/primefaces does not like multiple layers of forms or dialogs defined within forms. If you are having a similar issue, makes sure your dialog is defined outside any form. Moving a dialog can cause paths to components to change. the easiest way to resolve that is to use the primefaces handy p:component tag like this update=":#{p:component('compoment_name')}" > that will make it find it no matter where in the tree the component occurs.

<h:inputText> does not update [duplicate]

This question already has answers here:
commandButton/commandLink/ajax action/listener method not invoked or input value not set/updated
(12 answers)
Closed 9 months ago.
I have a page with a form and an input text, that represents the value of a bean. On the bottom of the page, I have a navigation button to go to the next entry:
<h:form id="customerData">
...
<h:outputText value="#{customeredit.customer.name}" />
<br />
<h:inputText size="60" id="name" value="#{customeredit.customer.name}" />
...
</h:form>
<p:commandButton value="Next customer" action="customers" ajax="false" id="nextCustomer">
<f:param name="linkageAreaId" value="#{customeredit.nextCustomer.id}" />
</p:commandButton>
If I click on the button, the page reloads, a new customer object is loaded in the background and all the entries are updated EXCEPT the inputText. The outputText changes, but the inputText always keeps the value it has gotten when it was intialized. I also checked with the getters, the values return from the getters change with new customers, but the value displayed in the inputText always stays the same.
That can happen if you're nesting forms (which is illegal in HTML) and thus both the input and button ultimately end up in the same form. That button would then submit the input field as well. In any case, you should not be using <p:commandButton> for page-to-page navigation. Use <p:button> instead.
<p:button value="Next customer" outcome="customers" id="nextCustomer">
<f:param name="linkageAreaId" value="#{customeredit.nextCustomer.id}" />
</p:button>
Don't forget to fix the nested form problem as well.
See also:
How to navigate in JSF? How to make URL reflect current page (and not previous one)
Difference between h:button and h:commandButton

Making AT-LEAST any one of Input Fields as Required in JSF Form [duplicate]

This question already has answers here:
JSF multiple input text (One of them is required)
How to validate if at least one of multiple input fields is entered?
(2 answers)
Closed 9 years ago.
I want to make At-least any one of the filed as Required in a form.
For Example:
<h:form id="searchForm">
<h:outputLabel value="User Name:"/>
<h:inputText value="#{searchBean.userName}" id="user_name"/>
<h:outputLabel value="Location:"/>
<h:inputText value="#{searchBean.location}" id="location"/>
<h:outputLabel value="Date of Birth"/>
<p:calendar value="#{searchBean.dob}" id="dob"/>
<p:commandButton value="Search"/>
</h:form>
In the above form has there are 3 Input Fields:
user_name
location
dob
and it searches based on the given criteria.
I want to make User to enter at-least any one of the above Fields to be REQUIRED to make the search happen.
So how do i use required validation so that the above scenario will work.
Please suggest any wok around!

JSF 2 - No way to do Required Inputs + Clear (Immediate) Button?

This question is similar to the question here but that solution doesn't work here.
Take this simple-to-the-max example:
<h:form>
<h:inputText required="true" value="#{mrBean.someValue}" />
<h:inputText required="true" value="#{mrBean.someValue2}" />
<h:commandButton value="Submit">
<f:ajax execute="#form" />
</h:commandButton>
<h:commandButton immediate="true" action="#{mrBean.clearAllValues}" value="Clear">
<f:ajax render="#form" />
</h:commandButton>
</h:form>
And the bean Code:
public void clearAllValues() {
setSomeValue(null);
setSomeValue2(null);
}
Take this scenario:
Enter 'X' value in first input
Submit it using the 'Submit' Button. (failed in validation)
Enter 'Y' into the same input.
Click the 'Clear' button.
Result: Inputs don't clear and value of first input returns to 'X'.
I would expect this scenario to clear the input values but it doesn't, instead, it restores 'X' which is the previously submitted value. It actually never really runs mrBean.getSomeValue() on the bean (Which would have returned null and clear the input field)
The problem is related to JSF not working well with required fields and immediate. I wonder if anyone managed to overcome this problem.
Thanks!
Ben.
Your code example is oversimplified. The described problem symptoms will only occur when you have multiple required inputs. Add one more required input field to the example. Fill out only one of them. A validation error will occur for the empty one. Then enter something else in both and press clear. The valid input will indeed retain the previously submitted value.
This problem is described in detail in this question and this blog article. The solution boils down to collecting all to-be-cleared input components and calling EditableValueHolder#resetValue() on each of them. This can be done with a <f:actionListener> as shown in the blog article.
Another way in your particular case since you just want to clear out the entire form is to use a <h:button> which will basically just refresh the page. If your bean is request or view scoped then it will also be recreated with all properties set to default.
<h:form>
<h:inputText required="true" value="#{mrBean.someValue}" />
<h:commandButton value="Submit">
<f:ajax execute="#form" />
</h:commandButton>
<h:button value="Clear" />
</h:form>
Are you sure clearAllValues is executed? Do you get any errors in the logs or console?
Try adding execute
<f:ajax render="#form" execute="#this">

Resources