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, biapar, Willr, Ingo, swaiba, simon_w

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


Reply


6 Posts   622 Views

Avatar
lozhowlett

Community Member, 146 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, 1132 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, 146 Posts

23 December 2013 at 10:06pm

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

Avatar
martimiz

Forum Moderator, 1132 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, 1808 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, 146 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;
}
}