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   7669 Views

Avatar
Mad_Clog

Community Member, 78 Posts

28 March 2009 at 10:07am

Edited: 28/03/2009 10:09am

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

Community Member, 46 Posts

3 April 2009 at 12:46pm

Awesome, this was EXACTLY what I was looking for.

Thanks!

Avatar
Capt. Morgan

Community Member, 30 Posts

7 April 2009 at 2:09am

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

Community Member, 187 Posts

6 June 2009 at 8:50pm

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

Community Member, 712 Posts

10 June 2009 at 12:50pm

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

Community Member, 114 Posts

24 June 2010 at 6:05am

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

Community Member, 2 Posts

16 June 2011 at 5:55am

Edited: 16/06/2011 6:05am

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

Community Member, 20 Posts

4 July 2014 at 7:24am

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

Go to Top