Jump to:

3430 Posts in 1057 Topics by 734 members

Data Model Questions

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

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1 2
Go to End
Author Topic: 7099 Views
  • Mad_Clog
    Avatar
    Community Member
    78 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
    46 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
    30 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
    712 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.

  • ttyl
    Avatar
    Community Member
    114 Posts

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

    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?

  • cbq
    Avatar
    Community Member
    2 Posts

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

    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;
    }

  • Hattori
    Avatar
    Community Member
    20 Posts

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

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

    7099 Views
Page: 1 2
Go to Top

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

Comments on this website? Please give feedback.