I'm new to JSF and I'm sure I'm doing something stupid, but I've been trying different things for days and cant make any progress. I'm trying to do validation when a user types in a date instead of using the rich calendar but for some reason I cant seem to get the validator to fire. The page code is as follows:
<a4j:outputPanel id="responseReleaseDate" rendered="#{appealSearchManager.isVendor}">
<p><h:outputText value="#{messages.ResponseReleaseDate}"/></p>
<rich:calendar id="responseReleaseDateBegin" datePattern="MM/dd/yyyy"
enableManualInput="true"
buttonIcon="/images/calendar_icon.jpg" buttonClass="calendar"
validator="#{appealSearchManager.validateResponseReleaseDateBegin}"
value="#{appealSearchManager.responseReleaseDateBegin}">
</rich:calendar>
<rich:calendar id="responseReleaseDateEnd" datePattern="MM/dd/yyyy"
enableManualInput="true"
buttonIcon="/images/calendar_icon.jpg" buttonClass="calendar"
validator="#{appealSearchManager.validateResponseReleaseDateBegin}"
value="#{appealSearchManager.responseReleaseDateEnd}">
</rich:calendar>
</a4j:outputPanel>
The bean code I'm trying to invoke is as follows:
public void validateResponseReleaseDateBegin(FacesContext facesContext, UIComponent uiComponent, Object value) throws ValidatorException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy");
if (value != null && !value.equals("")) {
try {
simpleDateFormat.parse(value.toString());
} catch (ParseException e) {
throw new ValidatorException(new FacesMessage(
FacesMessage.SEVERITY_ERROR,
MessageFormat.format((RootUtils.getCommonBundle().getString(BundleConstants.INVALID_ITEM)), "Response Release Date"),
MessageFormat.format(RootUtils.getCommonBundle().getString(BundleConstants.INVALID_DATE_FORMAT), "Date", "MM/DD/YYYY")));
}
}
}
The wierd thing is that I can reach the validateResponseReleaseDateBegin(...) method using the code below (I know, it doesnt make sense for a text field, it was just for testing purposes). But I never hit the method when I enter input for the rich calendar.
<div class="div30">
<p><h:outputText value="#{messages.ProgramInvoiceId}"/></p>
<h:inputText id="programInvoiceId"
validator="#{appealSearchManager.validateResponseReleaseDateBegin}"
value="#{appealSearchManager.programInvoiceId}"/>
</div>
Any ideas why this validator works in one place and not another?
Thanks!
This makes no sense. The <rich:calendar> will already implicitly convert the String submitted value to Date before setting it as model value. If you enter a date in invalid format, the <rich:calendar> will already throw a ConverterException for that. Its message should already be visible in any <h:messages> or <h:message> component associated with the component.
As conversion runs before validation, your validator is never fired when conversion fails. Even when your validator was fired, the Object value argument in the validator is already of the type java.util.Date. So if your validator was fired, it would always have thrown an exception because Date#toString() does definitely not match MM/dd/yyyy.
I'm not sure why you need this validator. Perhaps you just wanted to supply a custom conversion error message? In that case, you should be using its converterMessage attribute instead.
<rich:calendar ... converterMessage="Invalid date" />
Or, if you really need to "validate" the date format by yourself, implement a custom Converter instead and register it by converter attribute or <f:converter> tag instead.
Related
I have to upload CSV file and I have to validate some values of file, for example does not allow negative value. Then, I need validate it and that file does not apear like "done" or "uploaded". I need handle an error on display it.
<rich:fileUpload
fileUploadListener="#{configClientBean.listener}"
ontyperejected="alert('some error');"
maxFilesQuantity="1"
onuploadcomplete="#{rich:component('waitPanelInterno')}.hide();"
onfilesubmit="#{rich:component('waitPanelInterno')}.show();"
render="pnlMensajes, idTableClients, scrollRegistros, outputPanelDetalle"
autoclear="true">
<rich:message for="uploadConfigClient" />
<a4j:ajax event="uploadcomplete" execute="popupFileLoad"
render="panelCarga, pnlMensajes" />
</rich:fileUpload>
In the Backing bean I can validate some things, but I cant show errors or change the behavior of "rich:fileUpload" compoment for example doneLable can not be displayed.
public void listener(FileUploadEvent event) throws Exception {
try {
UploadedFile file = event.getUploadedFile();
ByteArrayInputStream bais = new ByteArrayInputStream(file.getData());
InputStreamReader is = new InputStreamReader(bais, getMessage("iso"));
BufferedReader bufRead = new BufferedReader(is);
while ((registro = bufRead.readLine()) != null) {
if(cvsLine[1].isEmpty()){
// stop process
// Throw error
}
}
}
Thanks for your time.
To add extra behavior to the rich:fileUpload component that is not default is by creating your own file upload component (ex. myown:fileUpload) based on the rich:fileUpload source code and the use of the Component Development Kit.
A second solution could be by adding one extra message field that is described in this post: RichFaces fileupload and h:message problem
How can I pass the actual value in the text boxt of emailEdit (after changed) to a function or how can I bind this value to another property of a bean when changed ?
Currently I got this :
<h:inputText id="emailEdit"
value="#{portalUserHome.instance.customer.email}">
</h:inputText>
<s:link value="Send mail"
action="#{customerEdit.sendEmail}" rerender="myCustomerEditData,myCustomerData"></s:link>
Thanks in advance
[Assuming you're talking about an EL function, not a JS function here]There are a few ways, but they'll mostly start with you having to bind the text to the page scope:
<h:inputText id="emailEdit" binding="#{emailEdit}" value="#{portalUserHome.instance.customer.email}"/>
From that point, you can pretty much do whatever you want, referring to the textbox using the binding of emailEdit
You could pass the value directly to the method you're interested in (available only starting from EL 2.2)
<s:link value="Send mail" action="#{customerEdit.sendEmail(emailEdit.value)}" rerender="myCustomerEditData,myCustomerData"/>
You could pass the value using the f:attribute tag, and then retrieve the passed attribute in your function
<s:link id="emailLink" value="Send mail"action="#{customerEdit.sendEmail}" rerender="myCustomerEditData,myCustomerData">
<f:attribute name="emailValue" value="#{emailEdit.value}"/>
</s:link>
Retrieving in your function:
public void sendEmail(){
FacesContext ctxt = FacesContext.getCurrentInstance();
UIComponent comp = ctxt.getViewRoot().findComponent("emailLink"); //assuming you already have a FacesContext
String theValue = comp.getAttributes().get("emailValue").toString(); //obligatory null check omitted for brevity
}
I have a complex form where the user fills a few fields, and has two options: generate a license file or save the changes. If the user clicks on the generate license file button without saving the changes, I render a small component with an error message asking him to save before generating the license.
To display the component with a warning message, I want to use ajax to avoid rendering the whole page just to render the warning component. Of course, if the changes were saved, then the warning message is not required and I redirect the user to another page.
I have a change listener on the changeable fields to detect when a change has been made. What I don't know is the conditional execution. The "render with ajax if unsaved OR redirect if saved" part. Here's the logic
if(saved){
redirect();
}else{
ajax.renderWarning()
}
--EDIT--
I'm going to add more info because I realized I'm leaving things too open ended.
Here's one example of an updateable field.
<h:inputText name="computername3" value="#{agreement.licenseServerBeans[2].computerId}" valueChangeListener="#{agreement.fieldChange}">
<rich:placeholder value="Add Computer ID"/>
</h:inputText>
The fieldChange() bean method
public void fieldChange(ValueChangeEvent event) {
change = true; //change is a boolean, obviously :P
}
Here's the generate license button jsf
<h:commandLink action="#{agreement.generateLicenseFile}">
<span class="pnx-btn-txt">
<h:outputText value="Generate License File" escape="false" />
</span>
</h:commandLink>
Here's the generateLicenseFile() method
public String generateLicenseFile(){
....//lots of logic stuff
return "/licenseGenerated.xhtml?faces-redirect=true";
}
Use PartialViewContext#getRenderIds() to get a mutable collection of client IDs which should be updated on the current ajax request (it's exactly the same as you'd specify in <f:ajax render>, but then in form of absolute client IDs without the : prefix):
if (saved) {
return "/licenseGenerated.xhtml?faces-redirect=true";
}
else {
FacesContext.getCurrentInstance().getPartialViewContext().getRenderIds().add("formId:messageId");
return null;
}
Returning null causes it to redisplay the same view. You can even add it as a global faces message and let the ajax command reference the <h:messages> in the render.
if (saved) {
return "/licenseGenerated.xhtml?faces-redirect=true";
}
else {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(...));
return null;
}
with
<h:messages id="messages" globalOnly="true" />
...
<f:ajax render="messages" />
I am using Prime faces 3.4.1 as component framework and JSF 2.0 as the server side framework
Following is my requirement
1) I have a field with label as "Meeting Required". Then I have SelectOneRadio with two options "No" and "Yes" with default value as "No". I am using JSF/HTML component h:SelectOneRadio.
2) I have another field which is Calendar component and this is a primefaces calendar component. When the user selects "Yes" which indicates the "Meeting is required" and the user should select a date from the calendar control.
3) If the user selects "Yes" and does not select a date, then a Validation message should be displayed indicating that the date should be selected.
I created a Custom Validation component and attached to the SelectOneRadio and I am able to see the selected value in the Custom validator. Now, I try to get the value from the Calendar component to check if the value is empty, through UIComponent.getParent().findCompoent("rvmDate"), I get the component but I do not know how to check if the date component is empty or contain any values.
Please help me out to get the date value selected by the user.
Please help me out to resolve the issue. Or is there any other way? Please find the source code.
XHTML Page
<h:selectOneRadio id="rvmMeetingOption"
readonly="#{wipMB.rvmMeetingOptionReadOnly}"
value="#{wipMB.requirementsMeeting}"
disabled="#{wipMB.rvmMeetingOptionDisabled}"
validator="#{wipMB.validateRVMDate}"
validatorMessage="Please enter RVM Date>
<f:selectItem itemLabel="No" itemValue="0"></f:selectItem>
<f:selectItem itemLabel="Yes" itemValue="1" ></f:selectItem>
<f:attribute value="#{rvmDateComp}" name="rvmDateComp"></f:attribute>
</h:selectOneRadio>
<p:calendar id="rvmDate"
readonly="#{wipMB.rvmMeetingDateReadOnly}"
disabled="#{wipMB.rvmMeetingDateDisabled}"
readonlyInput="true"
navigator="true" mode="popup"
pattern="dd/MM/yyyy"
value="#{wipMB.rvmDate}"
effect="explode"
yearRange="1900:2500"
style="margin-left:5px"
binding="#{rvmDateComp}"
</p:calendar>
<p:message id="rvmDateMsg" for="rvmDate" display="both" ></p:message>
Custom Validator
public void validateRVMDate(FacesContext context, UIComponent component, Object value)
throws ValidatorException
{
String invalidDate;
String rvmOption;
Date rvmDate;
String rvmDt = "";
try
{
FacesContext fc = FacesContext.getCurrentInstance();
rvmOption = value.toString();
DateFormat formatter = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
UIInput rvmCompDt = (UIInput)component.getAttributes().get("rvmDateComp");
rvmDateId = rvmCompDt.getId();
rvmDt = rvmCompDt.getSubmittedValue() == null ? "" : rvmCompDt.getSubmittedValue().toString();
if (rvmOption.equals("1") && rvmDt.isEmpty())
{
FacesMessage msg = new FacesMessage("RVM date is required");
msg.setSeverity(FacesMessage.SEVERITY_ERROR);
fc.addMessage("rvmDateMsg", msg);
throw new ValidatorException(msg);
}
}
catch (Exception ex)
{
String msg = ex.getMessage();
}
}
In JSF each component is a little MVC stack of its own; there is the Model (stored as value), the Controller (the component object) and View (renderer). Validators and controllers are part of the architecture and are needed to move the values between the model and the view.
While JSF Validators play an important role, it is important only INSIDE this little MVC stack. They were not designed to "validate forms", they are made strictly to "validate component value". Lamentably, the name "validator" makes everyone who comes to JSF think, that each time any validating needs to be done, validator is the solution. Strangely, converters are not so abused.
In your case, building a custom validator created a strange situation, where:
validator and view have a cyclic dependency on each other,
there is a need to use hacks (such as "immediate") and low-level APIs,
hard-coding view logic in an unlikely place,
requires much more knowledge and is flaky. For example the logic of acquiring value from calendar might be different depending on whether radio button is before or after calendar in document order.
The problems above could be solved, but since they all arise from abusing JSF architecture, I think it would be better to rethink the problem. Since your validation concerns flow of application, it is a perfect fit for the action method, where all the complications will dissolve into a single, simple "if" statement with a conditional "addMessage".
You first need to remove immediate="true" from the <p:calendar>, otherwise it's not processed at all when the radio button is processed.
Then, to check if a string is null or empty, just do
String dt = (String) uiCalendar.getSubmittedValue();
if (dt == null || dt.isEmpty()) {
// dt is null or empty. Throw validator exception depending on the
// current radio button value. Note: you should not catch it yourself!
}
Note that this has nothing to do with JSF. It's just basic Java. Your initial attempt as if (dt == "") is indeed completely invalid. The String is an object, not a primitive. The == compares objects by reference, not by their internal value. Technically, you should have used if (dt.equals("")) instead, but the isEmpty() is nicer.
Unrelated to the concrete problem, a much easier way is to just check the radio button value in the required attribute of the calendar component. First bind the radio button component via binding to a variable in the view, then reference its UIInput#getValue() method in the required attribute.
<h:selectOneRadio id="rvmMeetingOption" binding="#{rvmMeetingOption}"
readonly="#{wipMB.rvmMeetingOptionReadOnly}"
value="#{wipMB.requirementsMeeting}"
disabled="#{wipMB.rvmMeetingOptionDisabled}">
<f:selectItem itemLabel="No" itemValue="0"></f:selectItem>
<f:selectItem itemLabel="Yes" itemValue="1" ></f:selectItem>
</h:selectOneRadio>
<p:calendar id="rvmDate"
readonly="#{wipMB.rvmMeetingDateReadOnly}"
disabled="#{wipMB.rvmMeetingDateDisabled}"
readonlyInput="true"
navigator="true" mode="popup"
pattern="dd/MM/yyyy"
value="#{wipMB.rvmDate}"
effect="explode"
yearRange="1900:2500"
style="margin-left:5px"
required="#{rvmMeetingOption.value == 1}">
</p:calendar>
use
UIInput uiCalendar = (UIInput) component.getParent().findComponent("rvmDate");
Date test = uiCalendar.getValue();
if(test==null){
throw ValidatorException
}
test will then have the date filled in or will be null when nothing is chosen in teh date field
I am trying to set an exception message in the <h:message>.
Here is the relevant view code:
<h:inputText id="titleId" value="#{bookController.book.title}"/>
<h:message for="titleId"/>
<h:commandButton value="Create a book" actionListener="#{bookController.doCreateBook}" action="listBooks"/>
I need a message to be displayed when the titleId is empty. My #Stateless EJB method throws an exception when the title is empty:
public Book createBook(Book book) throws CustomException {
if(book.getTitle().isEmpty()) {
throw new CustomException("Please, type a Title !");
}
else {
em.persist(book);
return book;
}
}
My backing bean catches it and sets a message:
public void doCreateBook() {
FacesContext ctx = FacesContext.getCurrentInstance();
try {
book = bookEJB.createBook(book);
bookList = bookEJB.findBooks();
} catch (CustomException e) {
ctx.addMessage("titleId", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", e.getMessage()));
}
}
What I except is, when the exception occurs, an error message must be displayed near the input text tag, but it isn't the case, the execution displays the page with list of books and the "Error" message displayed under the list, as shown below:
How can I get the full exception message to show up next to the input field?
Apart from the erroneous message handling which Thinksteep has already answered, your other mistake is that you're doing validation in an action method. This is not right. You should be using JSF builtin validation facilities instead. Whenever the JSF builtin validation fails, then the action method will not be invoked and the page will also not navigate. The enduser sticks to the current form and the message will appear in the therefor specified <h:message> tag.
In your particular case, you just need to set the required attribute.
<h:inputText id="titleId" value="#{bookController.book.title}" required="true" />
<h:message for="titleId" />
If you want to customize the default required message, use requiredMessage attribute.
<h:inputText id="titleId" value="#{bookController.book.title}"
required="true" requiredMessage="Please, type a Title !" />
<h:message for="titleId" />
Remove that input validation from the EJB method. It doesn't belong there. The EJB isn't responsible for that, the caller (which is in your case thus your JSF code) is responsible for that.
ctx.addMessage("titleId", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", e.getMessage()));
Your message text is Error and you are getting same. Change "Error" here to what ever you want.
PUT <h:messages showDetail="true" />