5095 Posts in 1518 Topics by 1114 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 1980 Views |
-
Validating DataObject in CMS

5 August 2009 at 9:37am
Hi,
i am still trying to find out how i can validate fields of a DataObject in CMS.
My first steps are using the function validate()class News extends DataObject {
...
public function getCMSFields() {
...
}
...
public function validate() {
$valid = parent::validate();if ((!$this->Title) || ($this->Title=="")) {
$valid->error("Titel");
}
return $valid;
}
...
}But with this I get the following:
[User Error] Uncaught ValidationException: Validation error writing a News object: Titel. Object not written.
POST ............/admin/EditForm/field/HomepageNews/AddFormLine 715 in ........../sapphire/core/model/DataObject.php
So how do i have to catch the exception?
Thanks -
Re: Validating DataObject in CMS

5 August 2009 at 11:58pm Last edited: 5 August 2009 11:59pm
So how do i have to catch the exception?
Spell Title right!
$valid->error("Titel");
-
Re: Validating DataObject in CMS

10 June 2010 at 10:03am
Hello,
I'm very interressting by the response too
... I have the same problem !
I don't think it's the misspelling title because the first param of this error() method should be a message, a string (cf http://api.silverstripe.org/2.4/sapphire/core/ValidationResult.html#methoderror)...
Thanks for your help.
-
Re: Validating DataObject in CMS

10 June 2010 at 2:43pm
Hi.
If the data object has a method called getCMSValidator(), the CMS will call that to get a validator instance.
So for instance, you could do this:
function getCMSValidator() {
return new RequiredFields(array("Field1", "Field2"));
}RequiredFields is a built-in validator. You could equally well write a custom validator by extending Validator and putting in the right methods, and returning an instance of that.
Hope this helps
Mark
| 1980 Views | ||
|
Page:
1
|
Go to Top |




