The Man
The Man

Reputation: 435

SilverStripe GridField and ModeAdmin create / read / update / delete return values

On create / read / update / delete using SilverStripe GridField or ModelAdmin, how do you check the return values (such as success, error, etc.) of the create / read / update / delete operations?

For example GridField and ModelAdmin commit changes even before the save button is pressed.

Upvotes: 1

Views: 74

Answers (1)

Barry
Barry

Reputation: 3318

This is an example validator for a data object... when you have this then it is called on each save this "checks" the values in the form before saving and can raise validation errors / messages next to specific form fields by name...

class ObjectA extends DataObject {

    static $db = array(
        "Name"          => "Varchar",
    );

    function getCMSValidator() {
        return new ObjectA_Validator();
    }
    function getValidator() {
        return new ObjectA_Validator();
    }
}

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

        if (ObjectB::get()->filter('OtherName',$data['Name']))
            $this->validationError('Name','ObjectB exists with that name',"required");

        return count($this->getErrors());
    }
}

Upvotes: 1

Related Questions