Validating a wrong date entry in rich faces - jsf

In one of our functionality we have some date fields for inline edit where we have enabled manual input. If in rich:calendar component, we manually enter improper date or junk data it will not even call the action method. As per our requirement we need to display an error message for date validation for these kind of fields. Is there any way to track that the date entered in the calendar field is improper with this component.
Code for the reference:
<rich:calendar id="actualOpeningDtCal" rendered="#{!empty aProgram.id}"
value="#{aProgram.actualOpeningDate}" placeholder="dd-mmm-yyyy"
inputSize="20" enableManualInput="true" datePattern="dd-MMM-yyyy"
buttonIcon="../content/images/calendar.png" showWeeksBar="false"
showFooter="false">
<a4j:support event="oninputblur" reRender="aList" ajaxSingle="false"
action="#{aController.inlineEdit}">
<f:setPropertyActionListener value="#{aProgram}"
target="#{aController.inLineEditaBean}" />
</a4j:support>
<a4j:support event="oncollapse" reRender="aList" ajaxSingle="false"
action="#{aController.inlineEdit}">
<f:setPropertyActionListener value="#{aProgram}"
target="#{aController.inLineEditaBean}" />
</a4j:support>
</rich:calendar>

If validation fails, then there are error messages. Perhaps you aren't showing them. Use <rich:messages> instead of <h:messages> to have them rendered on ajax response.
A bad practice, but might work, would be to use immediate="true" (bypasses validation) and then do the validation in the action method.

Related

add general message when validation failed

I have a form with all kind of fields and for each field I have a validator and p:message attached when validation failed.
I need to add a message at top of the page\form with custom text(something like: "please fix all problem in form") only if I have a validation failure(I don't want to use growl).
how do I set the custom message and how do I trigger it to show(it is not attached to a specific field).
I'm using JSF 2 and primeface 3.4.
You can place
<h:panelGroup id="my_custom_error_msg">
<h:outputText value="Please fix all problem in form"
rendered="#{facesContext.validationFailed}"/>
</h:panelGroup>
And render it upon form submit, for example:
<h:commandButton value="Submit">
<f:ajax render="#form my_custom_error_msg" execute="#form"/>
</h:commandButton>
If you also have some homemade validation and messages sent from your bean to your page you can extend the rendered condition into
rendered="#{facesContext.validationFailed or not empty facesContext.messageList}"

<p:commandButton CONDITIONAL onclick event

i have a jsf-form with an input field and a save-button as seen in the code below. What i want to achieve is, when the save-button clicked, the input should be validated with the regex-pattern. If the validation failed, no save-confirmation-dialog should be shown. Otherwise a save-confirmation-dialog shown, and let the user to choose if to save or not.
In the code below, the dialog has always been shown, despite the conditional onclick="if(#{conditionOK}). I want no confirmation-dialog got shown, when conditionOK returns false!!! After many tries, i think the facescontext.isValidateFailed() will not be re-evalutated.
Please help :(
All what i want, is only to check, if the regex-Validator returns true. For this case, the confirmation-dialog should be shown.
My approach could be wrong. Many thank if you guys have also other solutions.
<h:form id="save_all_form">
<p:inputTextarea rows="1" style="width:100%;resize:none"
value="#{cusBean.saveAll}" autoResize="false"
validatorMessage="Wrong format">
<f:validateRegex pattern="#{msgs.pattern}" />
</p:inputTextarea>
<ui:param name="conditionOK"
value="#{facesContext.postback and !facesContext.validationFailed}" />
<p:commandButton value="#{msgs.button_overwrite_all}"
onclick="if(#{conditionOK}){confirmation.show()}"/>
</h:form>
I do not think that the JSF-validation is the way to go for you. It is intended to prevent the change of model data in the case, that the validation fails.
And if you would like to make a check in JavaScript you have to update the section in HTML. JavaScript does not reevaluate the Expression, so the value when the view was rendered the first time will be used everytime.
Try the following in the xhtml:
<h:form id="save_all_form">
<p:inputTextarea id="input" rows="1" style="width:100%;resize:none"
value="#{cusBean.saveAll}" autoResize="false">
<p:ajax global="false" update="input submit" partialSubmit="true"/>
</p:inputTextarea>
<p:commandButton id="submit" value="#{msgs.button_overwrite_all}"
onclick="if(#{cusBean.validate(msgs.pattern)}){confirmation.show()}"/>
</h:form>
And add this method in CusBean:
public boolean validate(String pattern) {
return getSaveAll().matches(pattern);
}
The result will be, that there is not JSF validation which takes place and the value of the textArea is submitted everytime you change it. Plus the commandButton-section is updated so the condition will be updated.
Like the other answer explained onclick event is too early to check the validation status of a JSF request(using !facesContext.validationFailed) because the request has not been submitted yet; Validation has not been run so the validation status will always be false (well, sort of) during onclick.
So what you'll want to do is carry out an ajax validation of the field (like shown in the earlier answer) and then use the primefaces args variable to check the status of the request:
<p:commandButton value="#{msgs.button_overwrite_all}" id="createReport" onclick="if(!args.validationFailed){confirmation.show();}"/>

Triggering validation of <rich:calendar> client side?

I have a rich:calendar on a jsf page like this:
<rich:calendar id="someDate"
name="aName" validatorMessage="wrong format!"
enableManualInput="true" datePattern="dd/MM/yyyy"
value="#{MyBean.someDate}" >
</rich:calendar>
<rich:message id="validationSomeDate" for="someDate"
tooltip="false" showDetail="false">
<f:facet name="errorMarker">
<h:graphicImage id="imgError" value"/someImage.gif" />
</f:facet>
</rich:message>
<rich:tooltip for="validationSomeDate" value="wrong format!"/>
This succesfully validates that the user didn't write some nonsense into the date field like "12456" or something. The problem is, this validation is only triggered when the form is submitted. I would like to have this checked as soon as the date field loses focus, all client-side. Is there a way to do this?
I tried using a
<rich:validator>
tag but I'm using richfaces 3.x and this tag isn't available.
<rich:ajaxValidator event="onblur">
doesn't help either...

Trying to understand why h:commandLink submits through validation and a4j:commandLink doesn't

First of all, i'm using Jsf 1.2...
I have a problem with submitting some values in a form to validation.
Specifically this code segement:
<h:panelGrid columns="4" id="StatusPanel">
<h:outputText value="#{msg.Phone_number_to_send_SMS_to}" />
<h:inputText id="phoneNumber" value="#{general.smsPhoneNumber}" required="true"
requiredMessage="Please enter a valid phone number." />
<a4j:commandLink value="#{msg.Submit_Button}"
reRender="pinCodeDeliveryMsgText, pinCodeDeliveryMsg, pinCodeDeliveryFailedMsg, pinCodeDeliveryMainPanel, LastPinCodeMsg, SendingSMSMSG"
action="#{general.submit}" />
<h:message for="phoneNumber" fatalClass="mandatoryFieldMissing" errorClass="mandatoryFieldMissing" tooltip="true" />
</h:panelGrid>
Which looks like this in the html page:
Whenever I press the submit link, the page doesn't really go through validation, it seems to go with the last successull values instead. The result being that, if the phone number field is left empty, it does nothing and doesn't even render the <h:message> tag.
Actually, I have a workaround fix that looks like this:
<h:commandLink value="#{msg.Submit_Button}">
<a4j:support event="onclick" reRender="pinCodeDeliveryMsgText, pinCodeDeliveryMsg, pinCodeDeliveryFailedMsg, pinCodeDeliveryMainPanel, LastPinCodeMsg, SendingSMSMSG"
action="#{general.submit}"/>
</h:commandLink>
But i'm really curious to know what's the difference between a4j:commandLink and h:commandLink that makes one woirk and the other not.
TnX
Have you tried to set the process attribute of a4j:commandLink to the id of the inputText? Looks like you are just triggering rerendering of some components, so no model update is performed at all.

Form value not passed to Seam bean after a4j reRender

I'm making a webapp in Seam but ran into a problem I can't seem to fix.
I have a JSF form where the customer can select a reservation type through a combobox. Based on the selected value, other form components gets rendered.
For example: the customer selects Hours as reservation type, a panelGroup gets rendered where the customer can select a start- and an end hour. But if the customer would select 'part of the day' as reservation type, a selectOneMenu gets rendered where the customer can select a part of the day (morning, afternoon, evening)
The rerendering well but the values of the components with a rendered conditional won't get passed to the bean. They stay null values.
This is the code i'm talking about:
<s:div id="spot"
rendered="#{selectedProduct.productType.name eq 'Flex Spot'}">
<h:panelGrid columns="2">
<h:outputText value="Reservation Type" />
<h:selectOneMenu value="#{selectedPeriodPart}">
<s:selectItems
value="#{productManager.getAvailableDayPartsSpot()}"
var="daypart"
label="#{daypart.label}"></s:selectItems>
<s:convertEnum />
<a4j:support ajaxSingle="true"
event="onchange"
action="#"
reRender="spot">
</a4j:support>
</h:selectOneMenu>
<h:outputText id="date_spot" value="Date" />
<a4j:outputPanel id="calendar_spot" layout="block">
<rich:calendar value="#{reservation.reservationPeriod.startDate}"
locale="en" cellWidth="24px"
cellHeight="22px"
style="width:200px" />
</a4j:outputPanel>
<h:outputText rendered="#{selectedPeriodPart eq 'DAY_PART'}"
value="Daypart" />
<h:selectOneMenu value="#{selectedDaypart}"
rendered="#{selectedPeriodPart eq 'DAY_PART'}">
<f:selectItem id="si_morning" itemLabel="Morning (6:00 - 12:00)"
itemValue="morning" />
<f:selectItem id="si_afternoon"
itemLabel="Afternoon (12:00 - 18:00)" itemValue="afternoon" />
<f:selectItem id="si_evening" itemLabel="Evening (18:00 - 00:00)"
itemValue="evening" />
</h:selectOneMenu>
<h:outputText rendered="#{selectedPeriodPart eq 'HOURS'}"
value="Hours" />
<h:panelGroup id="hours_spot"
rendered="#{selectedPeriodPart eq 'HOURS'}">
<ui:include src="/includes/reservation/select_hours.xhtml" />
</h:panelGroup>
</h:panelGrid>
</s:div>
Note: The calendar value do get passed back to the bean but the value of this piece of code doesn't (it does if you remove the rendered conditional):
selectOneMenu value="#{selectedDaypart}" rendered="#{selectedPeriodPart eq 'DAY_PART'}"
You need to ensure that the conditionals responsible for the outcome of the rendered attribute are also the same in the subsequent request of the form submit. Because when a component isn't rendered, JSF won't apply the request values them.
In a nutshell, the property behind #{selectedPeriodPart} needs to be the same in the subsequent request. There are several solutions:
Put bean in session scope. Easiest solution, but bad for server memory and client experience (updates would be reflected in multiple tabs/windows in same session).
Pass it through using <h:inputHidden>. Not sure though how this fits in the Ajax/Richfaces picture. Better use <a4j:keepAlive> here.
Use a conversation scope. Seam offers facilities for this.
I fixed it -.- Nothing was wrong with the code I posted.
Because I wasn't able to solve this issue I continued on an other page in the same conversation. I noticed some more strange behaviour: outjection of a variable didn't work etc.
I figured the mistake was in some other part of the code which, after corrected, fixed the whole problem.
Thx for answering guys!

Resources