Login | Forgot password | Register

X

What is OpenID?

OpenID is an Internet-wide identity system that allows you to sign in to many websites with a single account.

With OpenID, your ID becomes a URL (e.g. http://username.myopenid.com/). You can get a free OpenID for example from myopenid.com.

For more information visit the official OpenID site.

Jump to:

1154 Posts in 324 Topics by 274 members

Data Model Questions

SilverStripe Forums » Data Model Questions » [SOLVED] Custom validation on DataObject / ModelAdmin

Page: 1
Go to End
Author Topic: [SOLVED] Custom validation on DataObject / ModelAdmin 1308 Views
  • Mad_Clog
    avatar
    Community Member
    28 posts

    [SOLVED] Custom validation on DataObject / ModelAdmin Link to this post

    It took me ages to figure out how to add custom validation to a DataObject (such as setting fields required).
    After digging through the docs pages and forums over and over again I started digging into the code deeper.
    And guess what I found: getCMSValidator

    Example:

       /**
        * Add custom validation to the form
        *
        * @access public
        * @return RequiredFields
        */
       public function getCMSValidator() {
          return new RequiredFields('Topic', 'StartDate', 'EndDate');
       }

    Hope this helps anyone!

    Adding key search words to help people find this post
    required field fields form forms validation cms backend dataobject modeladmin

  • Apophenian
    avatar
    Community Member
    44 posts

    Re: [SOLVED] Custom validation on DataObject / ModelAdmin Link to this post

    Awesome, this was EXACTLY what I was looking for.

    Thanks!

  • Capt. Morgan
    avatar
    Community Member
    12 posts

    Re: [SOLVED] Custom validation on DataObject / ModelAdmin Link to this post

    To get more custom validation than just checking for required fields you can extend the RequiredFields validator. See Member_Validator in sapphire/Security/Member.php for "inspiration".

  • mschiefmaker
    avatar
    Community Member
    187 posts

    Re: [SOLVED] Custom validation on DataObject / ModelAdmin Link to this post

    When I add this to my ModelAdmin it stops records missing the required fields not be saved but it gives no error message. It just completes as it normally would but the record is not saved. This is probably an obvious question but what am I missing

    Thanks

    MM

  • Hamish
    avatar
    Community Member
    608 posts

    Re: [SOLVED] Custom validation on DataObject / ModelAdmin Link to this post

    Here is an example from a piece of code I'm working on. Basically, this enforces a rule where an existing projects project number can't be changed:

    class Project extends DataObject {

       // fields, methods, etc

       public function getCMSValidator() {
          return new Project_Validator();
       }

    }

    class Project_Validator extends RequiredFields {

       protected $customRequired = array('Number');

       /**
        * Constructor
        */
       public function __construct() {
          $required = func_get_args();
          if(isset($required[0]) && is_array($required[0])) {
             $required = $required[0];
          }
          $required = array_merge($required, $this->customRequired);

          parent::__construct($required);
       }
       
       function php($data) {
          $valid = parent::php($data);
          $number_SQL = Convert::raw2sql($data['Number']);
          if(isset($_REQUEST['ctf']['childID'])) {
             $id = $_REQUEST['ctf']['childID'];
          } elseif(isset($_REQUEST['ID'])) {
             $id = $_REQUEST['ID'];
          } else {
             $id = null;
          }
          
          $project = DataObject::get("Project", "`Number` = '{$number_SQL}'");
          
          if($id) {
             // Existing project, check that number hasn't changed.
             $project = DataObject::get_by_id("Project", $id);
             if($project->Number != $data['Number']) {
                $this->validationError("Number", "Sorry, you cannot change the project number.");
                $valid = false;
             }
          } else {
             // New project, check it doesn't already exist.
             if(DataObject::get("Project", "`Number` = '{$number_SQL}'")) {
                $this->validationError("Number", "Sorry, this project number already exists.");
                $valid = false;
             }
          }
          
          return $valid;
       }
    }

    So, if the the project exists and they try to change the project number, it will fail and return an error message. Hope this helps.

    1308 Views
Page: 1
Go to Top

Currently Online: There is nobody online.

Welcome to our latest member: GreenWork

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.