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.

DataObjectManager Module

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Displaying user_error messages in DataObjectManager


Reply

5 Posts   1605 Views

Avatar
el.atomo

2 March 2010 at 2:55am (Last edited: 2 March 2010 3:01am), Community Member, 5 Posts

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.

Avatar
UncleCheese

2 March 2010 at 3:28am 4085 Posts

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

Avatar
el.atomo

3 March 2010 at 1:31am Community Member, 5 Posts

Thanks for your fast reply.

I'll follow your hints.

Avatar
UncleCheese

4 March 2010 at 10:28am 4085 Posts

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.

Avatar
Gene

6 August 2010 at 5:54am (Last edited: 6 August 2010 5:57am), Community Member, 41 Posts

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?