7913 Posts in 1355 Topics by 930 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, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w
|
Page:
1
|
Go to End | |
| Author | Topic: | 273 Views |
-
DataObject->validate() method hangs on record add but works fine on amend

1 February 2013 at 5:26am Last edited: 1 February 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 -
Re: DataObject->validate() method hangs on record add but works fine on amend

1 February 2013 at 11:30pm Last edited: 1 February 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
| 273 Views | ||
|
Page:
1
|
Go to Top |

