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.

Customising the CMS

Validating DataObject in CMS


Reply

4 Posts   2657 Views

Avatar
lx

5 August 2009 at 9:37am Community Member, 83 Posts

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/AddForm

Line 715 in ........../sapphire/core/model/DataObject.php

So how do i have to catch the exception?
Thanks

Avatar
joshy

5 August 2009 at 11:58pm (Last edited: 5 August 2009 11:59pm), Community Member, 57 Posts

So how do i have to catch the exception?

Spell Title right!

$valid->error("Titel");

Avatar
Myrdhin

10 June 2010 at 10:03am Community Member, 67 Posts

Hello,

I'm very interressting by the response too :D ... 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.

Avatar
mark_s

10 June 2010 at 2:43pm Community Member, 78 Posts

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