Jump to:

7938 Posts in 1541 Topics by 945 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Check the right date

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1
Go to End
Author Topic: 456 Views
  • Bereusei
    Avatar
    Community Member
    94 Posts

    Check the right date Link to this post

    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?

  • Sticks
    Avatar
    Community Member
    29 Posts

    Re: Check the right date Link to this post

    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()) {
    ...
    }

  • Bereusei
    Avatar
    Community Member
    94 Posts

    Re: Check the right date Link to this post

    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 %>

  • simon_w
    Avatar
    Forum Moderator
    471 Posts

    Re: Check the right date Link to this post

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

    }

  • Bereusei
    Avatar
    Community Member
    94 Posts

    Re: Check the right date Link to this post

    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!

  • Sticks
    Avatar
    Community Member
    29 Posts

    Re: Check the right date Link to this post

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

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