views:

2012

answers:

1

Hi all,

My form validation works great, but the messages use the JSF component name and not the field label. For instance, I have a field subject and the error message I get when it is empty is something along the lines of:

UIComponent:contactBean.firstName cannot be empty.

My Form:

<div class="field">
<s:decorate id="firstNameDecorate" template="edit.xhtml">
 <ui:define name="label">#{messages['user.firstName']}</ui:define>
 <h:inputText id="firstName" required="true" value="#{contactBean.firstName}">
  <a:support event="onblur" reRender="firstNameDecorate"/>
 </h:inputText>
</s:decorate>

My edit template:

<div class="entry">
 <s:label styleClass="label #{invalid?'errors':''}">
  <ui:insert name="label"/>
  <s:span styleClass="required" rendered="#{required}">*</s:span>
 </s:label>
 <span class="input #{invalid?'errors':''}">
  <s:validateAll>
   <ui:insert/>
  </s:validateAll>
 </span>
 <s:message styleClass="error errors"/>
</div>

The validation works just like I want, the error messages are configurable (at least the value to the right of the label it uses), and the validators are so easy to configure as they simple are annotations on the ContactBean class' fields.

I would just like to polish it off and get the right values displayed there for instance:

First Name cannot be empty.

The last part is the ajax, onblur, doesn't actually do anything. That is a minor issue that I would like to address at some point as well.

ContactBean.java

package com.walterjwhite.seamCore.model;

import java.io.Serializable;

import org.hibernate.validator.Email;
import org.hibernate.validator.NotEmpty;

import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Name;

@AutoCreate
@Name("com.walterjwhite.seamCore.model.contactBean")
public class ContactBean
    implements Serializable
{
    protected String firstName;
    protected String lastName;
    protected String emailAddress;
    protected String subject;
    protected String message;

    public String getFirstName()
    {
     return firstName;
    }

    public void setFirstName(String firstName)
    {
     this.firstName = firstName;
    }

    @NotEmpty
    public String getLastName()
    {
     return lastName;
    }

    public void setLastName(String lastName)
    {
     this.lastName = lastName;
    }

    @NotEmpty
    @Email
    public String getEmailAddress()
    {
     return emailAddress;
    }

    public void setEmailAddress(String emailAddress)
    {
     this.emailAddress = emailAddress;
    }

    @NotEmpty
    public String getSubject()
    {
     return subject;
    }

    public void setSubject(String subject)
    {
     this.subject = subject;
    }

    @NotEmpty
    public String getMessage()
    {
     return message;
    }

    public void setMessage(String message)
    {
     this.message = message;
    }
}

Thanks,

Walter

A: 

It's actually quite straightforward. All you have to do is to set the label attribute on the inputText component, like so:

<h:inputText id="firstName" label="#{messages['user.firstName']}" required="true" value="#{contactBean.firstName}">
    <a:support event="onblur" reRender="firstNameDecorate"/>
</h:inputText>

This is what the JSF error message will use when displaying the message. The s:label tag just renders a label on the screen and does not set the label of the input component.

I hope this helps.

Kirderf
Thanks, I will give that a try later.
Thanks again, that was it. It looks so pretty now.Walter