Jump to:

5451 Posts in 1673 Topics by 1197 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Validating DataObject in CMS

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

Page: 1
Go to End
Author Topic: 2434 Views
  • lx
    Avatar
    Community Member
    81 Posts

    Validating DataObject in CMS Link to this post

    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

  • joshy
    Avatar
    Community Member
    57 Posts

    Re: Validating DataObject in CMS Link to this post

    So how do i have to catch the exception?

    Spell Title right!

    $valid->error("Titel");

  • Myrdhin
    Avatar
    Community Member
    67 Posts

    Re: Validating DataObject in CMS Link to this post

    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.

  • mark_s
    Avatar
    Community Member
    78 Posts

    Re: Validating DataObject in CMS Link to this post

    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

    2434 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.