If I remove required="true" from the input field, and I try and add a category without typing in a category name, no validation occurs.
<h:inputText validator="#{CategoryAdminBean.validateCategoryName}" id="categoryName" value="#{CategoryAdminBean.currentCategory.categoryName}" />
And I've simplified the validation for testing purposes.
public void validateCategoryName(FacesContext fc, UIComponent uic, Object o)
{
if (currentCategory.getCategoryId() == null)
{
((UIInput)uic).setValid(false);
FacesMessage facesMessage = new FacesMessage( FacesMessage.SEVERITY_WARN, "Category Name Required", null);
throw new ValidatorException(facesMessage);
}
}
On 6/21/06,
Julian Ray <[EMAIL PROTECTED]> wrote:
Hi Gregg,here is some code for [3][1] example bean-based validation. Here is a function we use to validate a new user name for a customer account. As some of these methods are re-used we actually place the code in a validation class.// Check to see if the user name is unique for this customer. The user is not allowed
// to change the username this function is only called when a new user is created,
public static void validateUserName(Integer customerId, UIComponent uic, String entry, String fieldName) {
if (! validateLength(uic, entry, fieldName, 6, 10)) {
return;
}if (entry.indexOf(' ') != -1) {
String message = fieldName + " cannot contain spaces.";
invalidateInput((UIInput) uic, message);
return;
}
if (! UserDao.isUserNameUnique(customerId, entry)) {
String message = fieldName + " must be unique for this customer.";
invalidateInput((UIInput) uic, message);
return;
}
}public static void invalidateInput(UIComponent uic, String message) {
invalidateInput((UIInput) uic, message);
}private static void invalidateInput(UIInput uii, String message) {
uii.setValid(false);
//FacesContext fc = javax.faces.context.FacesContext.getCurrentInstance();
FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_WARN, message, null);
throw new ValidatorException(facesMessage);
}[2] In the backing bean use something like the following. The ValidationUtils are defined in [1] above.public void validateUserName(FacesContext fc, UIComponent uic, Object o) {
ValidationUtils.validateUserName(getSessionBean().getCustomerId(), uic, o.toString(), "*");
}
[3] In your JSP use the validation="" attribute with a method binding to the method defined in [2]. For example<h:inputText id="userName" required="true" validator="#{myBackingBean.validateUserName}" value="#{myBackingBean.bean['userName']}"/>
<f:verbatim> </f:verbatim>
<h:message for="" errorClass="errorMessage"/>
From: Gregg Bolinger [mailto:[EMAIL PROTECTED]]Sent: Wednesday, June 21, 2006 10:04 AM
To: MyFaces Discussion
Subject: Re: Typical Validation Issues[1] Not really. The user needs the Category Name field along with it's Add button to be able to add a Category.
[2] I could probably do that (worst case)
[3] This sounds feasable. Could you provide a bit more info on this solution?
Thanks.
Gregg
On 6/21/06, Julian Ray <[EMAIL PROTECTED]> wrote:Some more suggestions[1] Is it possible to not render the form until a command link is clicked or renderer a place-holder instead?[2] Can you put dummy data in the form?[3] can use use a validation method in your backing bean which only tests if a category has been loaded (my preference).
From: Gregg Bolinger [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, June 21, 2006 9:49 AM
To: MyFaces Discussion
Subject: Typical Validation IssuesI am facing a pretty typical JSF validation issue. I have a list of commandLinks which are Categories. On the right side of the page I have a form with details about the category. You can also use this form to add or update a category.
Obviously, the category name is required. But since it is blank when the page loads, and if I click on a category link to view the details, I get a validation error. No surprise. So I change the commandLink immediate="true" and, you guessed it, my model isn't updated so most of the details don't get populated in the form. My commandLink code on my JSP looks like:
<t:commandLink actionListener="#{CategoryAdminBean.editCategory}">
<t:outputText value="#{category.categoryName}" />
<t:updateActionListener
property="#{ CategoryAdminBean.currentCategory}"
value="#{category}" />
</t:commandLink>
I've tried many different suggestions that I found on various different sites including this mailing list. I am not interested in adding yet another dependency to this project as in a 3rd party validation library or the adf:subForm (from what I can tell, this doesn't solve the problem either). But I would like to know what other people did to solve this similar issue, if they solved it.
Thanks.
Gregg

