7913 Posts in 1355 Topics by 930 members
DataObjectManager Module
SilverStripe Forums » DataObjectManager Module » Check the right date
Discuss the DataObjectManager module, and the related ImageGallery module.
Moderators: martimiz, UncleCheese, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w
|
Page:
1
|
Go to End | |
| Author | Topic: | 305 Views |
-
Check the right date

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?
-
Re: Check the right date

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()) {
...
} -
Re: Check the right date

5 January 2012 at 8:17pm Last edited: 5 January 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 %> -
Re: Check the right date

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()) {
…
} -
Re: Check the right date

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
Thanks!
-
Re: Check the right date

6 January 2012 at 2:50pm
Good to know! I would've run into the same problem.
| 305 Views | ||
|
Page:
1
|
Go to Top |


