Jump to:

7940 Posts in 1543 Topics by 946 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Displaying user_error messages in DataObjectManager

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: 1597 Views
  • el.atomo
    Avatar
    Community Member
    5 Posts

    Displaying user_error messages in DataObjectManager Link to this post

    Hi,

    I'm using a ManyManyDataObjectManager asociated to a DataObject, and I'm checking some fields within an OnBeforeWrite method.

    The checking process works fine, but the error messages look pretty awful (it displays source code and trace data). This is the code I'm using in the OnBeforeWrite() method:

    ...
    function onBeforeWrite() {
       parent::onBeforeWrite();
       // check for unique artist name (LastName + FirstName)
       $where = "FirstName = '$this->FirstName' AND LastName = '$this->LastName'";
       
       if ($this->ID) { // not first write action
          $where .= " AND ID != '$this->ID'";
       }

       $artists = DataObject::get("Artist", $where);

       if($artists) {
          user_error('The artist already exists', E_USER_ERROR);
          exit();
       }
    }
    ...

    Is there any special way for displaying/beautifying error messages related to a DataObjectManager?

    Thanks in advance.

  • UncleCheese
    Avatar
    4085 Posts

    Re: Displaying user_error messages in DataObjectManager Link to this post

    No.. you can't do that much of anywhere in Silverstripe without building a custom form. But I really like the idea. You might be able to do it with Form validation, though. I think onBeforeWrite() is definitely the wrong place for that..

  • el.atomo
    Avatar
    Community Member
    5 Posts

    Re: Displaying user_error messages in DataObjectManager Link to this post

    Thanks for your fast reply.

    I'll follow your hints.

  • UncleCheese
    Avatar
    4085 Posts

    Re: Displaying user_error messages in DataObjectManager Link to this post

    A few thoughts.. if you're open to merging the FirstName and LastName field, you could use a UniqueTextField. That will inherently validate what you're trying to emulate in your onBeforeWrite() function.

    I think what really needs to happen is that the DOM should offer a callback function on the submit, so that you could have a custom controller inspect and approve the post data before saving the record..

    Something like:

    doSave($data, $form)
    {
    if($this->callback && $this->controller->hasMethod($this->callback)) {
    $method = $this->callback;
    $this->controller->$method($data, $form)
    }
    // continue with form processing
    }

    and then in your controller you could have something like

    public function myDOMCallback(&$data, &$form)
    {
    if($data['Foo'] != "Bar")
    $form->setErrorMessage("Fail!","Foo");
    }

    Something to think about.

  • Gene
    Avatar
    Community Member
    41 Posts

    Re: Displaying user_error messages in DataObjectManager Link to this post

    I ran into this issue as well. With the ComplexTableField you can set a custom validator as long as you have a getValidator method in your DataObject (I don't know why it isn't called getCMSValidator like in the rest of the CMS). Unfortunately, the DataObjectManager_Popup class unsets any custom validator you add.

    Is there a reason $this->unsetValidator() is called in the constructor of DataObjectManager_Popup?

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