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.

General Questions /

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

Required Fields in CMS - has_one, has_many


Go to End
Reply


22 Posts   4815 Views

Avatar
ttyl

Community Member, 114 Posts

24 June 2010 at 7:59am

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?

Avatar
TotalNet

Community Member, 181 Posts

24 June 2010 at 9:46am

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

Avatar
ttyl

Community Member, 114 Posts

25 June 2010 at 2:02am

Edited: 25/06/2010 2:11am

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.

Avatar
ttyl

Community Member, 114 Posts

26 June 2010 at 6:04am

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

Avatar
ttyl

Community Member, 114 Posts

9 July 2010 at 2:56am

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

Avatar
swaiba

Forum Moderator, 1805 Posts

9 July 2010 at 3:10am

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

Avatar
ttyl

Community Member, 114 Posts

9 July 2010 at 3:38am

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;
   }
}

Avatar
swaiba

Forum Moderator, 1805 Posts

9 July 2010 at 4:44am

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 :)

Go to Top