Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: [Solved]Problem with mindate validation on form datefield

  1. #1

    Default [Solved]Problem with mindate validation on form datefield

    I am having a problem where I am defaulting the value of a date to the minimum value, and when the page is rendered it shows the date as invalid, even though the date equals the date it says it must be greater than. Additionally if you open the datepicker and select the date that is currently selected the validation passes.

    Any thoughts?

    Creation of datefield on form

    Code:
    var minDate =new Date(); var maxDate =new Date();
    maxDate
    .setDate(maxDate.getDate()+90); var posEffdt =new Date(); posEffdt.setDate(minDate.getDate());
    Code:
    xtype:'datefield', fieldLabel:'Effective Date', id:'poseffdt', name:'poseffdt', allowBlank:false, minValue: minDate, maxValue: maxDate, value: posEffdt, width:95


    Image diplays error.
    Attached Images Attached Images

  2. #2
    Sencha User mystix's Avatar
    Join Date
    Mar 2007
    Location
    Singapore
    Posts
    6,236

    Default

    new Date() creates a js Date object with time info (which is often overlooked).

    simply call Ext's clearTime() method on both minDate & maxDate:
    Code:
    var minDate = (new Date()).clearTime();
    var maxDate = (new Date()).clearTime();

    p.s. in future, pls post code snippets in [code][/code] tags.

  3. #3

    Default

    That worked! Thanks so much for the help. Added the code snippets for anyone searching in the future.

    We're just beginning to look at implementing Ext in our organization and this place has been a big help.

  4. #4
    Sencha User NightAvatar's Avatar
    Join Date
    Nov 2008
    Location
    Norway
    Posts
    206

    Default

    Does this mean that I can define a minDate in my code like this:
    Code:
    Ext.override(Ext.DatePicker, {
        minDate           : (new Date()).clearTime(),
        todayText         : 'Dagens dato',
        minText           : 'Denne dato er før minimum dato',
        maxText           : 'Denne dato er etter maximum dato',
        disabledDaysText  : '',
        disabledDatesText : '',
        monthNames        : localMonthNames,
        dayNames          : localDayNames,
        nextText          : 'Neste Måned (Ctrl+Høyrepil)',
        prevText          : 'Forrige måned (Ctrl+Venstrepil)',
        monthYearText     : 'Velg måned (Ctrl+Opp/Ned flytter år)',
        todayTip          : '{0} (spacebar)',
        okText            : ' OK ',
        cancelText        : 'Avbryt',
        format            : 'd.m.Y',
        startDay          : 1
    });
    I can't figure out how to get this to work. I'm trying to disallow all dates prior to today.

    Thanks!

  5. #5
    Sencha User Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,246

    Default

    Yes, that should work for DatePicker, but not for DateField, because it overrides the DatePicker default with minValue.

    So you would need:
    Code:
    var today = new Date()).clearTime();
    Ext.DatePicker.prototype.minDate = today;
    Ext.form.DateField.prototype.minValue = today;

  6. #6
    Sencha User NightAvatar's Avatar
    Join Date
    Nov 2008
    Location
    Norway
    Posts
    206

    Default

    That works perfect! Thanks!

  7. #7
    Sencha User NightAvatar's Avatar
    Join Date
    Nov 2008
    Location
    Norway
    Posts
    206

    Default

    Is this the correct way to set the minDate to 2 years prior to todays date?

    Code:
    // Date Picker calendar
    Ext.onReady(function(){
        new Ext.form.DateField({
            applyTo  : 'fromDate'
        });
        new Ext.form.DateField({
            applyTo  : 'toDate'
        });
    });
    //min date is set to 2 years ago
    var currentTime = new Date();
    var month = currentTime.getMonth() + 1;
    var day = currentTime.getDate();
    var year = currentTime.getFullYear();
    var twoYearsAgo = day + "." + month + "." + (year-2);
    var minDate = (twoYearsAgo).clearTime();
    Ext.DatePicker.prototype.minDate = minDate;
    Ext.form.DateField.prototype.minValue = minDate;
    If not, please explain how because I am at a loss.

    Thanks!

  8. #8
    Sencha User Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,246

    Default

    Use:
    Code:
    var minDate = currentTime.add(Date.YEAR, -2).clearTime();
    ps. And do you really want this in the prototype or only for one field at a time?

  9. #9
    Sencha User mystix's Avatar
    Join Date
    Mar 2007
    Location
    Singapore
    Posts
    6,236

    Default

    Code:
    Ext.DatePicker.prototype.minDate = (new Date()).clearTime().add(Date.YEAR, -2);
    Ext.form.DateField.prototype.minValue = Ext.DatePicker.prototype.minDate;
    
    // Date Picker calendar
    Ext.onReady(function(){
        new Ext.form.DateField({
            id: 'fromDate',
            renderTo: document.body
        });
        new Ext.form.DateField({
            id: 'toDate',
            renderTo: document.body
        });
    });
    [edit]
    awww shucks... @condor beat me to it by 2 full minutes...
    Last edited by mystix; 23 Feb 2009 at 3:00 AM. Reason: edit

  10. #10
    Sencha User NightAvatar's Avatar
    Join Date
    Nov 2008
    Location
    Norway
    Posts
    206

    Default

    Thanks to both of you!

    Quote Originally Posted by Condor View Post
    And do you really want this in the prototype or only for one field at a time?
    Only one field at a time I guess, but what exactly is the "prototype"? How does it work, or rather, what exactly am I setting when I set it to the prototype? (what's the difference?)

    Cheers!

Page 1 of 2 12 LastLast

Posting Permissions

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