problem with jsf / icefaces depended form fields and validation - jsf

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.

Related

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

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..

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

p:outputLabel would not update

I have a dialog box containing p:inputText with required="true" and a corresponding p:outputLabel.
<p:outputLabel for="name" value="First Name" />
<p:inputText id="name" value="#{userManagedBean.name}" required="true"/>
I submit the dialog box without any value causing a validation error indicated by the p:outputLabel in red required mark. But on reopening the dialog without submitting, the outputLabel is still in the invalidate state(red font). It doesnt get updated until I submit or navigate back from other page. I tried to update the whole dialog box but it wouldnt affect the label.
Thanks in advance
This is expected behaviour as input components (and related components) will maintain their state after validation fails. The state is only changed after the full request is re-executed and the complete JSF lifecycle is executed again.
You're not stating exactly how you're "updating" the panel, but I can recommend <p:resetInput/> to explicitly reset the state of related input components, without having to resubmit the entire <h:form/>. If you'd posted more meaningful code, I might have been able to give a working snippet
It will not be updated since opening the dialog box is happening in javascript (client side).
You need to re-render your dialog box when you open it.

JSF/Richfaces/A4j ==> component/field conversion and reRendering problem

I have an input field in a JSF Page like the following (maps to BigDecimal on backing bean)
<h:inputText disabled="#{volumeBean.grossVolumeDisabled}" id="grossVolume" size="10" validateOnExit="true" value="#{volumeBean.enteredGrossVolume}" >
<a4j:support ajaxSingle="true" event="onblur" ignoreDupResponses="true" oncomplete="checkFieldValidation(this)" onsubmit="updateDirty()"/>
</h:inputText>
And an a4j:commandButton to "refresh" all the data from the database on the page:
<a4j:commandButton accesskey="T" action="#{volumeBean.revert}" button-type="ajax" disabled="#{volumeBean.revertDisabled}" id="volumeBean_reset" immediate="true" reRender="volumesTable" value="#{msg.button_RESET}"/>
Here are the steps to reproduce my problem:
And please note that the error occurs regardless of whether there is a reRender attribute set on the a4j:support
Here are the steps to reproduce - just to clarify further:
navigate to the screen where the BigDecimal input field exists
type aa into the field (should be a number but put non-numeric characters purposely)
tab off the field
notice that an error is reported 'aa' is not a valid netVolume
click on the RESET button
all of the changed fields have their original values EXCEPT those that have non-numeric data entered
unless the user manually deletes the non-numeric data in the fields or refreshes the entire screen, the "bad data" sticks
When you do a reset, you fire an Ajax request, the entire form is submitted and you get validation error again. So the field still has the old (incorrect) value.
Try adding the parameter ajaxSingle="true" to the button. I've found that immediate="true" is not adequate for bypassing validation on ajax components.

Skip form validation on command button

I have a JSF page that includes a tree form tag which is rendered depending on some bean property. There are two buttons for next and previous page. I want to skip form validation on the button which goes to the previous page.
I tried the following ways to disable the validation:
Set h:commandButton immediate="true"
Change button by a4j:commandButton ajaxSingle="true" rerender="someparts"
It does not work. Why does the navigation fail when I want to skip validation?
immediate="true" does skip the validation. Make sure you have redeployed successfully, and the there aren't any errors.
I solve problem using a4j:commandButton ajaxSingle="true" reRender=":outhercomponent:formconteningcomponent:component"
reRender needs absolute path to component even if component id unique

Resources