Primefaces - at least one field required with one message for all fields - jsf

Is it possible to perform primefaces validation to validate that one field in the form has to be filled in? When none are filled in, all fields have to be marked red (happens automatically) and only one message may be displayed in the messages tag. Right now a message is displayed for each required field and I don't want that.
I've tried:
<p:messages id="messages" redisplay="false"/>
but this doesn't filter them.
An example of one of the many inputFields:
<p:inputText id="from" value="#{containerBean.selectedContainer.from}"
maxlength="512"
requiredMessage="#{msgs['one.field.required']}"
required="#{empty param['containerForm:description']
and empty param['containerForm:to']
and empty param['containerForm:year']
and empty param['containerForm:yearTo']
and empty param['containerForm:sequence']}"
disabled="#{documentBean.addNewOrUpdateDocumentSelected or !containerBean.canEditSelectedContainer()}"/>
I know it's possible with omnifaces, but I may not use it :)..
PF 5.3
I also know that I could perform the validation in my BackingBean, but that's not a good practice..

Related

OmniFaces o:validateOneOrMore skips fields which was disabled conditionally

I am using primefaces 6.x with omnifaces for constructing forms.This particular form has around 20 fields texfields, dropdown values extra but has to have business logic like one or more fields needs to be filled in before the save happens. I am using omnifaces o:validateOneOrMore with the id's of all the required form fields. The issue is some of the input fields are disabled based on whether its non empty, for example
<p:inputText styleClass="Fleft Wid70" maxlength="1000" id="description"
title="description" value="#{manageBean.myTO.desc}"
disabled="#{manageBean.myTO.desc != null}">
</p:inputText>
The next time the form is processed/submitted the o:validate fires skipping the disabled inputText. Is there a way to include the field in the omnifaces validation even when its disabled?
<o:validateOneOrMore id="oneOrMore"
components="foo bar too zin ber der fir nam dil suf
description" message="You must enter data for atleast one of the fields" />
Requirement is to include all id's for validation irrespective of them being disabled or not.Please help.

Primefaces p:pickList and the required attribute

EDIT:
I found the explanation (played around with f:event) and saw that PF has the behaviour I was assuming, namely that the backing bean values of the PF framework themselves are not updated, because the validation phase failed (therefore skipping the update model values phase and directly go to render phase) leaving the source/target list as they were before.
I have the following code:
<p:pickList
id="parentpicklist"
value="#{manager.parent}"
var="entity"
itemValue="#{entity}"
labelDisplay="true"
required="true"
rendered="#{not empty manager.parent.source or not empty manager.parent.target}">
At the end of the page I have a p:commandButton like this:
<p:commandButton
action="#{manager.save}"
value="#{messages.saveLabel}"
title="#{messages.saveHint}"
update="edit-extract" <!-- the picklist resides inside of this component -->
rendered="#{manager.editMode}">
The scenario:
On the page is this picklist and it has its source and target list populated with values, i.e., both are not empty.
Now the problem is the following: When I remove all items from the target list and press 'save', then the validation fails and it says: validation required. So far so good.
THE PROBLEM: It does not only show that the validation failed, but it updates the picklist with its initial values when loading the page.
Is this PF standard behavior?
Because p:commandButton has as default process="#form" (that is why it is validating the picklist which is inside the form). I tried out several stuff to set the targetlist content by my own, e.g., with the onTransfer event.
BUT it does not update the target list --> It looks like the validation fails and the targetlist does not get updated (jsf validation fails and goes directly to the renderResponse phase, i.e., the model values are not updated) and therefore the old resp. initial values are loaded.
Best regards,
ProgrammingIsAwsome

rich:suggestionbox how to call bean in case user aborts selection

Old SuggestionBox component for RichFaces (version 3.3... yes, I have to maintain legacy code, weep for my fate) is pretty useful, but has one downside.
If user enter some text in input component and then click somewhere else than entry on list of suggestion, partially filled input is left as is. It is ugly and can be confusing, giving impression that something is selected when it is not.
I want to have separate call to bean in this case, allowing me to remove text, if nothing got selected.
Example code:
<h:inputText id="selectorInput" value="#{backingBean.inputText}" label="Select something:" />
<rich:suggestionbox id="suggestion" for="selectorInput" suggestionAction="#{backingBean.resolveSuggestions}" var="sug">
<a4j:support event="onselect" reRender="someForm" action="#{backingBean.select(sug)}" />
<h:column>#{sug.name}</h:column>
</rich:suggestionbox>
I tried to add a4j:support for h:inputText with event="onblur", but it is called before backingBean.select(sug) and has no way to know if something was selected or not, making it almost useless. Adding other events to suggestionbox itself appear to not work at all or even break suggestionbox.
Is there any other way?
The backing JavaScript object has a getSelectedItems() method. It returns an empty array if nothing was selected.
To get the object use
document.getElementById(clientId).component

rendered attribute on inputText

I have a search form tied to a backing bean that contains 4 input text fields. The design i am working from indicates that the user should be able to see the search results, but they should not be editable. i decided to use the rendered attribute to show the inputs if the managed bean is empty, and to show an output text tag if it's not:
<t:inputText styleClass="inputText" id="name" rendered="#{not searchCriteria.fieldsEntered}"
value="#{searchCriteria.name}" autocomplete="off"></t:inputText>
<h:outputText value="#{searchCriteria.name}" rendered="#{searchCriteria.fieldsEntered}"></h:outputText>
The display part works correctly, but I am noticing that only the first field is stored in the managed bean when more than 1 search field is entered.
I removed a rendered attribute from an inputText, and sure enough that's causing my problems. I can infer what's going on here, but I don't understand why.
I believe in this situation I will just remove the outputText tags and change rendered to disabled. I am just curious why my initial plan is incorrect.
The rendered="false" will cause the input element not being rendered and thus its value will not be submitted to the server side. If you're using a request scoped bean, the initial value will not be set. You'd like to either put the bean in session scope or to add a h:inputHidden along the h:outputText which transfers the value to the subsequent request.
Since you're already using Tomahawk's t:inputText I'd suggest to rather use its displayValueOnly attribute instead of the rendered attribute and a complementary h:outputText.
In a nut:
<t:inputText displayValueOnly="#{searchCriteria.fieldsEntered}" ... />

problem with jsf / icefaces depended form fields and validation

I have a form with 3 fields (simplyfied example).
The first one is a checkbox.
<ice:selectBooleanCheckBox value="#{backingBean.bean.visible}" ID="checkbox1" partialSubmit="true" >
The second one is a
<ice:inputText ID="text1">
The third one is also a
<ice:inputText ID="text2" required="true" validator="mycustomvalidator">
text1 should only be visible when checkbox1 is checked.
text2 is a required field and has customvalidator set.
So my first approach with just using #{backingBean.bean.visible} failed because text2 has is required and a validation message appeared (after the checkbox was clicked) when the text2 field is empty.
Because of ths valdation error the form was never completely submitted such that the visible property is set (update model phase was never reached).
I also tried to use immediate="true" for the selectBooleanCheckBox but that only had the consequence that the validation message for the required="true" disappeared but the validation of mycustomvalidator still triggered.
So my question: how can I make sure text1 is only visible when checkbox1 is clicked?
Thanks.
To give an answer here, I think the best solution is to use client side Javascript for hiding/showing the elements.

Resources