Jump to:

18591 Posts in 4875 Topics by 2285 members

General Questions

SilverStripe Forums » General Questions » Returning a custom error in ModelAdmin

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba

Page: 1
Go to End
Author Topic: 605 Views
  • AdamJ
    Avatar
    Community Member
    137 Posts

    Returning a custom error in ModelAdmin Link to this post

    I'm have an onBeforeWrite function that if a checkbox is checked in that class, it checks some criteria in the relationships and if that returns in the negative, blocks the save and returns an error message using the following:

    user_error('Products can not be marked for sale without active variants', E_USER_ERROR);
    exit();

    From Firebug I can see that this line works as it returns the ajax save request with the error and a code of 500 Error. What I want to do though is have that error displayed to the user in the little javascript notification in the bottom left corner of the panel that default actions use. Is there a way to hook into that?

  • swaiba
    Avatar
    Forum Moderator
    1526 Posts

    Re: Returning a custom error in ModelAdmin Link to this post

    Here is how I do custom validation within ModelAdmin... hope it helps...

    class MyDataObject extends DataObject {
    ...
       function getCMSValidator() {
          return new MyDataObject_Validator();
       }
    ...
    }

    class MyDataObject_Validator extends RequiredFields {
       function php($data) {
          $bRet = parent::php($data);

          if (empty($data['FieldName'])) {
             $obj->validationError('FieldName','FieldName is required',"required");
             $bRet = false;
          }

          return $bRet;
       }
    }

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