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   1645 Views

Avatar
el.atomo

Community Member, 5 Posts

2 March 2010 at 2:55am

Edited: 02/03/2010 3:01am

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

Forum Moderator, 4096 Posts

2 March 2010 at 3:28am

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

Community Member, 5 Posts

3 March 2010 at 1:31am

Thanks for your fast reply.

I'll follow your hints.

Avatar
UncleCheese

Forum Moderator, 4096 Posts

4 March 2010 at 10:28am

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

Community Member, 41 Posts

6 August 2010 at 5:54am

Edited: 06/08/2010 5:57am

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?