OmniFaces o:validateOneOrMore skips fields which was disabled conditionally - jsf

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.

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

JSF 1.2/Seam 2.2 - validator is skipped when input it empty

I am trying to call validateLength in JSF (xhtml file of seam)
<f:validateLength minimum="2" maximum="512"/>
It works for values such as text length 513 and 1 (i.e. it shows a warning, but not for 0)
The input text filed for which it is being used is set to required=false (so that it can use a4j support for empty fields, I have to show a preview based on the input)
The problem that I see is that there a validator method in a helper class but it gets ignored when the length of input is 0 (ie I put nothing , it works for non-empty values).
I also have a NullableStringConverter here but what I have noticed that as soon as that converter sets the value of null of empty string, the validator gets skipped. Here is the complete snippet of in the inputText
<h:inputText id="linkNameInput"
value="#{someHelper.name}"
validator="#{someHelper.validateMethod}"
required="false">
<f:validateLength minimum="2" maximum="512"/>
<f:converter converterId="NullableStringConverter" />
</h:inputText>
I would just like the ability to validate an empty string in a validator.
I would just like the ability to validate an empty string in a validator.
That's not possible in JSF 1.x. That's only possible since JSF 2.0 (in favour of JSR303 Bean Validation support).
I'm not sure why you'd like to validate the empty string in the custom validator. There you normally use the required="true" attribute for this. If the sole goal is to change the required message, then just use requiredMessage attribute.
<h:inputText ... required="true" requiredMessage="Please enter name" />
As mentioned already, validating an empty input String in a JSF 1.2 component is not supported. However, you can check for this null/empty model value in your backing bean on form submit and add a FacesMessage that is mapped to that specific input field. This would allow integration with server-side component-specific error message display (<h:message for="componentId" />).
FacesContext.getCurrentInstance().addMessage("componentId",
facesMessageForComponent);

Displaying read-only forms (values are shown as text instead of disabled input controls) with JSF?

I have a data entry form where user enters lots of data. When user comes to the page to view existing data, the page should be displayed in read-only mode (all values shown as text), when he clicks 'Edit' button, normal form with all input controls should be shown so that user can change and save data.
We are using JSF 2.0 with PrimeFaces library. It is easy to achieve above behavior for text box and text area but not for Checkbox, multi-select, radio,..... controls. Is there any easy way available to achieve above behavior rather than writing our own code (which may run into lot of lines thus making backing bean code ugly)
Thanks for your help...
I'm not sure why you think that you need additional backing bean code for this. You've all the needed values in the backing bean already. Your problem is more in the presentation of those values. Just display them in the desired format by writing the view code accordingly. Perhaps you were thinking it too the hard way.
Instead of a select boolean checkbox, you could display for example a "Yes" or "No" value.
<h:selectBooleanCheckbox value="#{bean.checked}" rendered="#{bean.edit}" />
<h:outputText value="#{bean.checked ? 'Yes' : 'No'}" rendered="#{not bean.edit}" />
Instead of a select one menu/radio, you could just display the value in an output text.
<h:selectOneMenu value="#{bean.selectedItem}" rendered="#{bean.edit}">
<f:selectItems value="#{data.availableItems}" />
</h:selectOneMenu>
<h:outputText value="#{bean.selectedItem}" rendered="#{not bean.edit}" />
Instead of a select many listbox/checkbox, you could just display for example all values comma separated in a loop.
<h:selectManyListbox value="#{bean.selectedItems}" rendered="#{bean.edit}">
<f:selectItems value="#{data.availableItems}" />
</h:selectManyListbox>
<h:panelGroup rendered="#{not bean.edit}">
<ui:repeat value="#{bean.selectedItems}" var="selectedItem" varStatus="loop">
#{selectedItem}#{not loop.last ? ', ' : ''}
</ui:repeat>
</h:panelGroup>
You could wrap it all in a tag file or a composite to minimize boilerplate and code repetition.
I've done this in my last project using composite components which has a "preview" attribute and in the implementation I render a text when this attribute is true and the real (editing) when the attribute is false. For checkbox in preview mode you could show the checkbox itself but disabled, for radio - show the selected item.
MyFaces Tomahawk library [1] contains an extended version of the standard components that adds displayValueOnly attribute for this purpose. This might help you (I haven't used them).
[1] - http://myfaces.apache.org/tomahawk-project/tomahawk20/index.html

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.

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