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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

DataObject->validate() method hangs on record add but works fine on amend


Go to End


2 Posts   2052 Views

Avatar
MJA

Community Member, 21 Posts

1 February 2013 at 5:26am

Edited: 01/02/2013 5:36am

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

Avatar
MJA

Community Member, 21 Posts

1 February 2013 at 11:30pm

Edited: 01/02/2013 11:33pm

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