Jump to:

22972 Posts in 11596 Topics by 2824 members

General Questions

SilverStripe Forums » General Questions » DateField being set to incorrect locale with strange behaviour in validation (2.4.0 rc1)

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1163 Views
  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    DateField being set to incorrect locale with strange behaviour in validation (2.4.0 rc1) Link to this post

    I have attached a simple data object and model admin using a couple of dates. There is also validation to ensure blank values are not entered.

    1)The dates are converted in some way that gives the wrong date... I select 1st April 2010 in the calendar pop up, then I get '01/04/2010' written into the text input next to it. then when I click 'Add' that value changes to 'Jan 4, 2010'

    2)validation fails for the second DateField? as the validator has nothing in the data for that item.

    I have the following in mysite/_config.php...

    i18n::set_locale('en_GB');

    (tested in firefox)

    Code follows, any help with this would be much appreciated

    TestTable.php

    <?php

    class TestTable extends DataObject
    {
       static $db = array(
          'Name' => 'Text',
          'DateFrom' => 'Date',
          'DateTo' => 'Date',
       );

       public static $summary_fields = array (
          'Name' => 'Name',
       );

       function getCMSFields()
       {
          $fields = parent::getCMSFields();

          $fields->replaceField('Name',new TextField('Name'));

          $df1 = new DateField('DateFrom', 'DateFrom');
          $df1->setConfig('showcalendar', true);
          $fields->replaceField('DateFrom', $df1);

          $df2 = new DateField('DateTo', 'DateTo');
          $df2->setConfig('showcalendar', true);
          $fields->replaceField('DateTo', $df2);

          return $fields;
       }

       function getCMSValidator()
       {
             return new TestValidator();
       }
    }

    TestValidator.php

    <?php

    class TestValidator extends Validator
    {

       function javascript()
       {
          return false;
       }
       function php($data)
       {
          $bRet = true;

          if (empty($data['DateTo']))
          {
             $this->validationError(
                'DateTo',
                'DateTo is required',
                "required"
             );

             $bRet = false;
          }

          if (empty($data['DateFrom']))
          {
             $this->validationError(
                'DateFrom',
                'DateFrom is required',
                "required"
             );

             $bRet = false;
          }

          return $bRet;
       }
    }

    TestAdmin.php

    <?php

    class TestAdmin   extends ModelAdmin
    {
       static $managed_models = array(
          'TestTable',
       );

       static $url_segment = 'testadmin';
       static $menu_title = 'Test Admin';

       static $model_importers = array();

    }

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: DateField being set to incorrect locale with strange behaviour in validation (2.4.0 rc1) Link to this post

    Yeah I'm seeing this as well - I'm using legacydatefields module until these are sorted out, which might not be until 2.4.1 (see http://open.silverstripe.org/ticket/5397).

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: DateField being set to incorrect locale with strange behaviour in validation (2.4.0 rc1) Link to this post

    Where can I find the 'legacydatefields'? that sounds like exactly what I need

    1163 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.