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.

Customising the CMS /

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

Validating DataObject in CMS


Go to End


4 Posts   3796 Views

Avatar
lx

Community Member, 83 Posts

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

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

So how do i have to catch the exception?
Thanks

Avatar
joshy

Community Member, 57 Posts

5 August 2009 at 11:58pm

Edited: 05/08/2009 11:59pm

So how do i have to catch the exception?

Spell Title right!

$valid->error("Titel");

Avatar
Myrdhin

Community Member, 70 Posts

10 June 2010 at 10:03am

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

Community Member, 78 Posts

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