Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions

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

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


Reply

3 Posts   1234 Views

Avatar
swaiba

14 April 2010 at 9:59pm Forum Moderator, 1796 Posts

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();

}

Avatar
Hamish

15 April 2010 at 8:50am Community Member, 712 Posts

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 [url]http://open.silverstripe.org/ticket/5397[/url]).

Avatar
swaiba

15 April 2010 at 9:37pm Forum Moderator, 1796 Posts

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