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.

Data Model Questions

[SOLVED] Custom validation on DataObject / ModelAdmin


Go to End
Reply

9 Posts   7387 Views

Avatar
Mad_Clog

28 March 2009 at 10:07am (Last edited: 28 March 2009 10:09am), Community Member, 78 Posts

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

Avatar
Apophenian

3 April 2009 at 12:46pm Community Member, 46 Posts

Awesome, this was EXACTLY what I was looking for.

Thanks!

Avatar
Capt. Morgan

7 April 2009 at 2:09am Community Member, 30 Posts

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

Avatar
mschiefmaker

6 June 2009 at 8:50pm Community Member, 187 Posts

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

Avatar
Hamish

10 June 2009 at 12:50pm Community Member, 712 Posts

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.

Avatar
ttyl

24 June 2010 at 6:05am Community Member, 114 Posts

what about requiring a 'has_one' or 'has_many'?

is there any other documentation on this? I'm confused by why form validation is supported but not validation in the CMS - this can't be the case, right?

Avatar
cbq

16 June 2011 at 5:55am (Last edited: 16 June 2011 6:05am), Community Member, 2 Posts

about the relations, i did it this way (supposing that Area is for example in has_one) and used for the has_one relation the dataobjectmanager module for displaying the data.

if(!array_key_exists('Area', $_REQUEST) || !array_key_exists('selected', $_REQUEST['Area']) || !$_REQUEST['Area']['selected']){
$this->validationError('Area', 'Sorry, please select a keyword');
$valid=false;
}

Avatar
Hattori

4 July 2014 at 7:24am Community Member, 20 Posts

It does not work on SS 3.1. Can someone give another solution?

Go to Top