Jump to:

23486 Posts in 18995 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Required Fields in CMS - has_one, has_many

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1 2 3
Go to End
Author Topic: 4474 Views
  • ttyl
    Avatar
    Community Member
    114 Posts

    Required Fields in CMS - has_one, has_many Link to this post

    Is there a way to have a has_one or has_many be required in the CMS? For example, I want to force a user to upload an image for a page. Additionally, is there any documentation on validating data in the CMS?

  • TotalNet
    Avatar
    Community Member
    181 Posts

    Re: Required Fields in CMS - has_one, has_many Link to this post

    Have you tried making it a required field when you create the CMS form field for the image in getCMSFields()?
    I've never tried that with any CMS fields before so not sure how it'd work!

    Rich

  • ttyl
    Avatar
    Community Member
    114 Posts

    Re: Required Fields in CMS - has_one, has_many Link to this post

    when I do this

       public function getCMSValidator() {
          return new RequiredFields('Date', 'Time', 'Location', 'Image');
       }

    I always get a validation error on image - even if there is one. if I remove image from the required fields everything is groovy. I also have some related dataobjects I need to check for.

    also, there is documentation of public facing form validation - but what about CMS validation? that's what I'm most concerned about - even the best trained people make mistakes - I'd like to catch them.

  • ttyl
    Avatar
    Community Member
    114 Posts

    Re: Required Fields in CMS - has_one, has_many Link to this post

    nothing? this is a pretty important thing for insuring content editors don't make mistakes...

  • ttyl
    Avatar
    Community Member
    114 Posts

    Re: Required Fields in CMS - has_one, has_many Link to this post

    bumping this up - nobody has any ideas? is there any documentation at all on setting up data validation in the CMS?

  • swaiba
    Avatar
    Forum Moderator
    1792 Posts

    Re: Required Fields in CMS - has_one, has_many Link to this post

    How about making a validator....

    inside MyDataObject.php

       function getCMSValidator()
       {
             return new MyValidator();
       }

    calls MyValidator.php

    class MyValidator extends Validator
    {
       function javascript()
       {
          return false;
       }

       function php($data)
       {
          $bRet = true;

          //do some validation on the data
          if ($data['HasOneID'] SOME VALIDATION HERE)
          {
             $this->validationError(
                'HasOneID',
                'Some message here',
                "required"
             );

             $bRet = false;
          }

          return $bRet;
       }
    }

    Hope this helps...

    Barry

  • ttyl
    Avatar
    Community Member
    114 Posts

    Re: Required Fields in CMS - has_one, has_many Link to this post

    Thanks for the help, but I have a few more questions ;-)

    First, when I put the class in a new file it wasn't finding it - is there an include statement I need to put in someplace? Second, when I tried it with a normal text value it worked, but when I tried with my Image it doesn't work. I tried both 'Image' and 'ImageID' - any suggestions?

    Also, is there anyway to do a javascript alert?

    class MyValidator extends Validator{
       function javascript(){
          return false;
       }

       function php($data){
          $bRet = true;

          //do some validation on the data
          if (!$data['Image']){
             $this->validationError(
                'Image',
                'We need an image',
                "required"
             );

             $bRet = false;
          }

          return $bRet;
       }
    }

  • swaiba
    Avatar
    Forum Moderator
    1792 Posts

    Re: Required Fields in CMS - has_one, has_many Link to this post

    1) if your class name is the same as your file name it will auto load it. Never had problems with that.
    2) if this is your dataobject...

    static $has_one = array('MyImage' => 'Image');

    I'd expect it to be...

    $data['MyImageID']

    but to be sure, turn the site to dev mode, enable logging and send $data to the logger and have a look at it

    4474 Views
Page: 1 2 3
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.