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.

General Questions /

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

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

[SOLVED] Showing the user a friendly validation error in OnBeforeWrite


Go to End
Reply


15 Posts   1656 Views

Avatar
lozhowlett

Community Member, 151 Posts

19 December 2013 at 11:31pm

Hi Everyone

I want to show my user a friendly error...

public function onBeforeWrite() {
        
        //check a branch doesnt already exist for this Member, as they are only allowed one branch per login
        if(!$this->ID) {
          if(CompanyBranch::get()->where("MemberID='".$this->MemberID."'")) {
            user_error('Only one branch is allowed per franchisee', E_USER_ERROR);
            exit();
          }
        }

        //get the lat long details if not present
        if(!$this->MapLat||!$this->MapLong){
            $url = "http://maps.googleapis.com/maps/api/geocode/json?address=".urlencode($this->getFullAddressMAPAPI()).",GB&sensor=true";
            $geocode = json_decode(file_get_contents($url));
            $geo = $geocode->results[0]->geometry->location;
            $this->MapLat = $geo->lat;
            $this->MapLong = $geo->lng;
        }
        parent::onBeforeWrite();
        
    }

However this just throws the black box in the top right with a php error, rather than a nice warning to the user that they can do what they are trying to do.

Any ideas on how to show a friendly error and stop the process in this instance?

Thanks

Avatar
martimiz

Forum Moderator, 1307 Posts

22 December 2013 at 12:26am

I think that generating a user error as a form validation method isn't the best way to tackle this, as you basically just stop execution... Maybe you could use a validator instead? Or disable the option beforehand (including some description) if a branch already exists for that user?

Martine

Avatar
lozhowlett

Community Member, 151 Posts

23 December 2013 at 10:06pm

Thanks Martine.... could you point me towards any documentation on that?

Avatar
martimiz

Forum Moderator, 1307 Posts

24 December 2013 at 1:31am

This is about Form validation (in the CMS):

http://doc.silverstripe.com/framework/en/topics/form-validation#validation-in-the-cms

But on second thought the best option is probably the second. Maybe just set canCreate for the branch to false if a branch already exists for that member. That should remove the add button if all goes well, and still allow the user to edit the existing (i don't know how you've implemented things, but that should be the general gist of it...

Avatar
swaiba

Forum Moderator, 1890 Posts

24 December 2013 at 2:01am

Gives me a chance to link to this (yet again!)....

http://www.silverstripe.org/general-questions/show/17007

Avatar
lozhowlett

Community Member, 151 Posts

24 December 2013 at 2:47am

Thanks that sorted it

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


      if(!$this->ID) {
          if(CompanyBranch::get()->where("MemberID='".$this->MemberID."'")) {
            $this->validationError('CompanyBranchID','A branch for this Franchisee already exists',"required"); 
            $bRet = false; 
          }
      }

      

      return $bRet; 
   } 
}

Avatar
Spambanjo

Community Member, 24 Posts

20 February 2016 at 12:49am

Edited: 20/02/2016 12:50am

Please, could whoever is in charge of the websites at least do some 301 redirects if you're going to change the URLs of sooooooooooo many pages?

There are 2 different links to the solution I need in this thread and NEITHER of them work any more. Fortunately I know how to find the answers I need, but this looks unbelievably poor to new users considering Silverstripe as their CMS solution. I find links to Silverstripe documents on the web week-in, week out which link to 404 pages. This is especially bad when most of the broken links are posted on the Silverstripe forum and/or are links to solutions/tutorials.

Avatar
martimiz

Forum Moderator, 1307 Posts

20 February 2016 at 1:46am

Edited: 20/02/2016 1:49am

Here at least is swaiba's original link: http://www.silverstripe.org/community/forums/general-questions/show/17007

And here's the modern equivalent of the other one: https://docs.silverstripe.org/en/3.2/developer_guides/forms/validation/#validation-in-the-cms

The user docs have had such a big overhaul that doing redirects for all those old pages really didn't seem to be doable. As for redirecting to the old forum posts, that bugs me too - basically just a general url change... maybe something to report :)

Go to Top