PDA

View Full Version : Friendly date validation error message


sinoea
Apr 25th, 2007, 07:47 AM
Hi, I am using a CustomDateEditor for my PostProjectController.

The format is dd/MM/yyyy.

Now, when I enter a date in this format: dd-MM-yyyy I get an error message
saying:
Failed to convert property value of type [java.lang.String] to required type [java.util.Date] for property deliveryDate; nested exception is java.lang.IllegalArgumentException: Could not parse date: Unparseable date: "10-12-1978"

Obviously an error message should be displayed, but how can I make it a friendly error message.

Here is the controller

package com.translationbank.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.propertyeditors.CustomDa teEditor;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestDataBin der;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormCont roller;

import com.translationbank.businessservice.ProjectBusines sService;
import com.translationbank.model.Project;
import com.translationbank.model.User;
import com.translationbank.util.Constants;

public class PostProjectController extends SimpleFormController {

/** Business service for the project. */
private ProjectBusinessService projectBusinessService;

/**
* Constructor.
*/
public PostProjectController() {
setCommandClass(Project.class);
}

/**
* @return the projectBusinessService
*/
public ProjectBusinessService getProjectBusinessService() {
return this.projectBusinessService;
}

/**
* @param projectBusinessService the projectBusinessService to set
*/
public void setProjectBusinessService(final ProjectBusinessService projectBusinessService) {
this.projectBusinessService = projectBusinessService;
}

/**
*
* @param req
* @param binder
* @throws Exception
*/
protected void initBinder(HttpServletRequest req,ServletRequestDataBinder binder)
throws Exception {
binder.registerCustomEditor(Date.class, new CustomDateEditor(
new SimpleDateFormat("dd/MM/yyyy"), true));
}

/**
* Post a project.
*/
public ModelAndView onSubmit(final HttpServletRequest request, final HttpServletResponse response,final Object command, final BindException errors)
throws Exception {

final Project project = (Project)command;
project.setDatePosted(new Date());
project.setStatus(Constants.PROJECT_STATUS_ACTIVE) ;
final User user = (User)request.getSession().getAttribute(Constants. LOGGED_IN_USER);
this.projectBusinessService.insertProject(project, user);
return showForm(request, errors, getSuccessView());
}
}



Here is part of the jsp

<form:form action="post_project.html" method="post" cssClass="standardform" name="project" commandName="project">
<div>
<p>
<form:errors cssClass="error" path="*" />
</p>
</div>
<div>
<form:label path="sourceLanguage" for="sourceLanguage">Source Language</form:label>
</div>
<div>
<form:select path="sourceLanguage" cssClass="long">
<form:option value="English" label="English" />
<form:option value="Polish" label="Polish" />
</form:select>
</div>
<div>
<form:label path="targetLanguage" for="targetLanguage">Target Language</form:label>
</div>
<div>
<form:select path="targetLanguage" cssClass="long">
<form:option value="English" label="English" />
<form:option value="Polish" label="Polish" />
</form:select>
</div>
<div>
<form:label path="wordCount" for="wordCount">Approx. Word Count (Enter only numbers)</form:label>
</div>
<div>
<form:input path="wordCount" id="wordCount" />
</div>
<div>
<form:label path="deliveryDate" for="deliveryDate">Delivery Date (dd/MM/yyyy)</form:label>
</div>
<div>
<form:input path="deliveryDate" id="deliveryDate"/>
</div>
<div>
<form:label path="description" for="description">Description</form:label>
</div>
<div>
<form:textarea path="description" cols="50" rows="10" />
</div>
<div>
<input type="submit" value="post project" class="btn" />
</div>
</form:form>

noon
Apr 25th, 2007, 02:17 PM
One solution is to add following kind of message to your message bundle. I hope you're using message bundles in your project. If not, see the Spring documentation :)

typeMismatch.java.util.Date=Invalid date entry

This should do the trick. Every time you enter an invalid value to a date field, the error message is shown.

In addition you can use similar type mismatch catches with Integers and Doubles:
typeMismatch.java.lang.Integer=Invalid integer entry

sinoea
Apr 25th, 2007, 04:40 PM
Thanks! It worked.

-------------------------
A workstation is where work stops