Login | Forgot password | Register
What is OpenID?
OpenID is an Internet-wide identity system that allows you to sign in to many websites with a single account.
With OpenID, your ID becomes a URL (e.g. http://username.myopenid.com/). You can get a free OpenID for example from myopenid.com.
For more information visit the official OpenID site.
Data Model Questions
SilverStripe Forums » Data Model Questions » [SOLVED] Custom validation on DataObject / ModelAdmin
|
Page:
1
|
Go to End | |
| Author | Topic: [SOLVED] Custom validation on DataObject / ModelAdmin | 1308 Views |
-
[SOLVED] Custom validation on DataObject / ModelAdmin

28 March 2009 at 10:07am Last edited: 28 March 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: getCMSValidatorExample:
/**
* 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 -
Re: [SOLVED] Custom validation on DataObject / ModelAdmin

3 April 2009 at 12:46pm
Awesome, this was EXACTLY what I was looking for.
Thanks!
-
Re: [SOLVED] Custom validation on DataObject / ModelAdmin

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".
-
Re: [SOLVED] Custom validation on DataObject / ModelAdmin

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
-
Re: [SOLVED] Custom validation on DataObject / ModelAdmin

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.
| 1308 Views | ||
|
Page:
1
|
Go to Top |
Currently Online: There is nobody online.
Welcome to our latest member: GreenWork




