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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

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


Go to End


3 Posts   1705 Views

Avatar
swaiba

Forum Moderator, 1899 Posts

14 April 2010 at 9:59pm

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

Community Member, 712 Posts

15 April 2010 at 8:50am

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).

Avatar
swaiba

Forum Moderator, 1899 Posts

15 April 2010 at 9:37pm

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