Results 1 to 6 of 6

Thread: CustomDateEditor, strange validation ?

  1. #1

    Default CustomDateEditor, strange validation ?

    Hi,

    I have registered a CustomDateEditor like this :

    Code:
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); 
    binder.registerCustomEditor(java.util.Date.class, null, new CustomDateEditor(dateFormat,false));
    When i put "25/08/04" instead of "25/08/2004" in my field form, the date is ok for the customDateEditor and the date register is in fact : "25/08/0004".

    Is there a lenient property to stop the incorrect validation or another way ?

    Thanks again,

    Fabien.

  2. #2
    Join Date
    Aug 2004
    Location
    Montreal, Canada
    Posts
    35

    Default

    To avoid the editor to interpret dates use the following before binding:
    Code:
    dateFormat.setLenient(false);
    And it should make it.

    Cheers,

    Uze

  3. #3

    Default

    Hi,

    For me it doesn't work ? Someone has the same pb ?

    Thanks,

    Fabien.

  4. #4
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,104

    Default

    Why not use:
    Code:
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yy");

  5. #5
    Join Date
    Oct 2004
    Location
    China
    Posts
    14

    Default I meet the same case!

    Yeah, I meet the same case. Is it a bug.

  6. #6
    Join Date
    Oct 2004
    Location
    China
    Posts
    14

    Default It seems that dateFormat.setLenient(false) doesn't work!

    I see carefully the source code of CustomDateEditor and find that dateFormat.setLenient(false) doesn't work. I write my own MyCustomDateEditor as such:

    import java.beans.PropertyEditorSupport;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;

    import org.springframework.util.StringUtils;

    public class MyCustomDateEditor extends PropertyEditorSupport {
    private final SimpleDateFormat dateFormat;

    private final boolean allowEmpty;

    private final boolean strict;


    public MyCustomDateEditor(SimpleDateFormat dateFormat, boolean allowEmpty ,boolean strict) {
    this.dateFormat = dateFormat;
    this.allowEmpty = allowEmpty;
    this.strict = strict;
    }

    public void setAsText(String text) throws IllegalArgumentException {
    if (this.allowEmpty && !StringUtils.hasText(text)) {
    // treat empty String as null value
    setValue(null);
    } else {
    String pattern = this.dateFormat.toPattern() ;
    if ( strict && pattern.length() != text.length() ){
    throw new IllegalArgumentException("error date Pattern: " + text + ",should be " + pattern );
    }
    try {
    setValue(this.dateFormat.parse(text));
    } catch (ParseException ex) {
    throw new IllegalArgumentException("Could not parse date: "
    + ex.getMessage());
    }
    }
    }

    public String getAsText() {
    return (getValue() == null ? "" : this.dateFormat
    .format((Date) getValue()));
    }

    }
    and regist MyCustomDateEditor as such:
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); binder.registerCustomEditor(Date.class, null ,new MyCustomDateEditor(dateFormat, true,true))
    Then when I input something as "25/08/04" ,it throws binding exption to informat me something wrong.It does work for me.I think it may be help you and suggest spring framework to include it!

Similar Threads

  1. Replies: 2
    Last Post: Oct 10th, 2005, 05:12 PM
  2. Persistent Object Life-cycle Event Validation pattern
    By tentacle in forum Announcements
    Replies: 0
    Last Post: Jun 24th, 2005, 03:24 PM
  3. Lost with Validation
    By RAPHEAD in forum Architecture
    Replies: 6
    Last Post: May 29th, 2005, 06:01 AM
  4. Domain Object Validation
    By lhilden in forum Architecture
    Replies: 4
    Last Post: Dec 14th, 2004, 07:00 AM
  5. Replies: 0
    Last Post: Nov 15th, 2004, 11:25 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
  •