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.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Check the right date


Go to End


6 Posts   978 Views

Avatar
Bereusei

Community Member, 96 Posts

5 January 2012 at 8:08am

Hi everyone,

I´ve a little problem. I have an form with an datefield.
Before I send the datas to the database, I wanna check, if it is a expired date.
I can´t figured it out, how to check the date. I´ve tried: if($this->Datum < NOW() and if($this['Datum'] < NOW()) in the onBeforeWrite-function:

function onBeforeWrite(){
...

if($this['Datum'] < NOW()){
$form->setErrorMessage("Fail!","Date");
}

parent::onBeforeWrite();
}

Does anyone know, how to check the date?

Avatar
Sticks

Community Member, 31 Posts

5 January 2012 at 11:02am

Date has an InPast function that looks like it should work for you.

http://api.silverstripe.org/2.4/sapphire/model/Date.html#methodInPast

if ($this->Datum->InPast()) {
...
}

Avatar
Bereusei

Community Member, 96 Posts

5 January 2012 at 8:17pm

Edited: 05/01/2012 8:18pm

No, that doesn´t work. I get this error: Fatal error: Call to a member function InPast() on a non-object
Apparently the InPast-Function could only used in the frontend like this:
<% if $Date.InPast %>
//some code
<% end_if %>

Avatar
(deleted)

Community Member, 473 Posts

5 January 2012 at 8:58pm

You need to get the object that represents the date. The normal getter only returns its value.

To do so, use $this->dbObject('Datum'). Then you can call the Date methods on it. I.e.:

if($this->dbObject('Datum')->InPast()) {
…
}

Avatar
Bereusei

Community Member, 96 Posts

5 January 2012 at 9:15pm

Lol thanks Simon, you were a little bit faster than me.
I found this solution:

if ($this->obj("Datum")->InPast()) {

}

This looks pretty much the same :D

Thanks!

Avatar
Sticks

Community Member, 31 Posts

6 January 2012 at 2:50pm

Good to know! I would've run into the same problem.