Results 1 to 7 of 7

Thread: Petclinic, date validation

  1. #1
    Join Date
    Aug 2004
    Posts
    25

    Default Petclinic, date validation

    Hi,

    I m learning to know how the petclinic web app and I ve a question. In the page addPet.htm, when I enter an invalid date, an error is display "invalid date". But I ve look into the validator which refers to this page (PetValidator.java) and there is no test for the date.

    How and where this invalid date error is detected?
    We are a football tribe!

  2. #2
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    khem,
    AddPetForm.java (the form to witch /addPet.htm is mapped) extends AbstractClinicForm. The later registers a property editor for application's date format
    Code:
    	protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
    		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    		dateFormat.setLenient(false);
    		binder.registerCustomEditor(Date.class, null, new CustomDateEditor(dateFormat, false));
    	}
    This editor is used for converting to Date. The message you got "invalid date" is thrown whenever the editor fails to parse a date.

    HTH
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  3. #3
    Join Date
    Aug 2004
    Posts
    25

    Default

    Hi,

    thx for your answer irbouho.
    So if I understand, when the editor fails to parse the date, it throws an exception. Is this exception resolved automatically by the ResourceBundleMessageSource? Because in this file, I see this :
    typeMismatch.date=invalid date
    typeMismatch.birthDate=invalid date
    Would that mean that when there s a type mismatch on a field called xxx, the error message that will be thrown will be the one defined in the ResourceBundleMessageSource which will looks like
    Code:
    typeMismatch.xxx=error message
    ?
    What happend if there s no ResourceBundleMessageSource defined?
    We are a football tribe!

  4. #4
    Join Date
    Aug 2004
    Location
    Tampa, FL
    Posts
    39

    Default

    If you don't have the message defined, you get a verbose error; for example, something like this (my field is called updateStamp):

    Code:
    Failed to convert property value of type [java.lang.String] to required type [java.util.Date] for property 'updateStamp'; nested exception is java.lang.IllegalArgumentException: Could not parse date: Unparseable date: "2004-092"
    I came across this post http://forum.springframework.org/showthread.php?t=9576 which mentioned that having the declare all the properties can be cumbersome. I agree with that.. however, I found you can define a catch-all typeMismatch like this in your message bundle:

    Code:
    typeMismatch=Invalid type - {$0}
    It catches all the cases where you haven't defined a message. The {$0} just gives you the property name (in my case "updateStamp") which isn't really useful, as that's often something that's not presentable to the user.

    I would prefer a way to report errors based on the type of the property (as a fall-back when the specific property name isn't defined). For example, define a message specifically for java.util.Date, etc. Any ideas?
    Last edited by Rod Johnson; Jan 18th, 2006 at 11:02 AM.

  5. #5
    Join Date
    Aug 2004
    Location
    Tampa, FL
    Posts
    39

    Default

    Looks like there is a way to define messages that are displayed for specific types. For example:

    Code:
    typeMismatch.java.util.Date=Invalid date entry
    typeMismatch.java.lang.Integer=Invalid integer entry

  6. #6
    Join Date
    Sep 2004
    Posts
    1

    Default

    Quote Originally Posted by nilesh
    Looks like there is a way to define messages that are displayed for specific types. For example:

    Code:
    typeMismatch.java.util.Date=Invalid date entry
    typeMismatch.java.lang.Integer=Invalid integer entry
    Hi!
    Exactly! There are more details in javadoc apis on: http://www.springframework.org/docs/...sResolver.html

  7. #7
    Join Date
    Feb 2005
    Posts
    1

    Default Validation is done after typeMismatch !!

    I hade the same problem, I have tried customizing the error message in case of typeMismatch and it works fine.
    Invalid date entry
    But I still get the error message of the validation atfter that :!:
    Invoice date is a required field.
    Validation is not supposed to be done for a field with type mismatch. :?:

    Note that the above is only displayed in case of binding all messages
    Code:
    <spring&#58;bind path="invoice.*">
        <c&#58;if test="$&#123;not empty status.errorMessages&#125;">
        <div class="error">	
            <c&#58;forEach var="error" items="$&#123;status.errorMessages&#125;">
                <c&#58;out value="$&#123;error&#125;" escapeXml="false"/><br />
            </c&#58;forEach>
        </div>
        </c&#58;if>
    </spring&#58;bind>
    But in case of binding only the message of invoice.date I only get the "Invalid date entry" message
    Code:
    <spring&#58;bind path="invoice.date">
    	<input type="text" name="$&#123;status.expression&#125;" value="$&#123;status.value&#125;"/>
    	<span class="fieldError">$&#123;status.errorMessage&#125;</span>
    </spring&#58;bind>
    But still, validation is done in both cases.
    Regards
    Mohamed Ragab

Similar Threads

  1. Replies: 4
    Last Post: Mar 12th, 2010, 12:28 PM
  2. Replies: 17
    Last Post: Jan 2nd, 2007, 01:43 PM
  3. Replies: 2
    Last Post: Oct 10th, 2005, 05:12 PM
  4. validation of a date field
    By Scott Tavares in forum Swing
    Replies: 2
    Last Post: Mar 31st, 2005, 07:07 AM
  5. Date Field Validation
    By sherihan in forum EJB
    Replies: 0
    Last Post: Feb 7th, 2005, 04:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •