Jump to:

7940 Posts in 1543 Topics by 946 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » DataObject->validate() method hangs on record add but works fine on amend

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1
Go to End
Author Topic: 720 Views
  • MJA
    Avatar
    Community Member
    16 Posts

    DataObject->validate() method hangs on record add but works fine on amend Link to this post

    Hello people

    I am new to SilverStripe and loving it so far, but I have run into a little problem

    Title pretty much says it all but to clarify, I am trying to add validation to a custom ModelAdmin form for DataObject.

    Code snippit below:

    /* start */
    if (!class_exists('stVehicle')) {
       class stVehicle extends DataObject {

          public function validate() {
             $result = parent::validate();
             
             $this->RegNum = str_replace (' ', '', strtoupper($this->RegNum) );
             if ( strlen($this->RegNum) != 7 )
                $result->error ('bad reg num');
             
             return $result;
          }

       }   // end class stVehicle
    }
    /* end */

    This works fine when amending existing records but the system hangs when attempting to add a new record.
    Stranger still, an add operation hangs regardless of whether or not the entered data passes the test.

    I do not understand why this is happening or, more to the point, how to fix it.
    Any suggestions would be much appropriated.

    In case it is relevant, the system is running under SilverStripe 2.4.7
    And before anybody suggests it, I cannot upgrade as this is a legacy site so I need a solution for 2.4.7

  • MJA
    Avatar
    Community Member
    16 Posts

    Re: DataObject->validate() method hangs on record add but works fine on amend Link to this post

    Just in case anybody else is having this problem, found the solution

    /* start */

    if (!class_exists('stVehicle')) {
       class stVehicle extends DataObject {
       
          public function getCMSValidator() {
             return new stVehicleValidator('RegNum');
          }
       
       }   // end class stVehicle
    }

    if (!class_exists('stVehicleValidator')) {
       class stVehicleValidator extends RequiredFields {
       
          protected $customRequired = array();
          
          // *** 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);
          }
          
          // *** custom validation ***
          public function php($data) {
             $valid = parent::php($data);
             
             if (isset($data['RegNum'])) {
                $data['RegNum'] = str_replace (' ', '', strtoupper($data['RegNum']) );
                if ( strlen($data['RegNum']) != 7 )
                   $this->validationError ('RegNum', 'bad reg num');
             }
             
             return empty($this->errors) && $valid;
          }
       
       }   // end class stVehicleValidator
    }

    /* end */

    Works perfectly

    720 Views
Page: 1
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.