Jump to:

23551 Posts in 19348 Topics by 2891 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: 4528 Views
  • ttyl
    Avatar
    Community Member
    114 Posts

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

    then there just isn't a field. this is a page btw so even if I totally get rid of getCMSFields() I still get title, navigation label and content.

  • ttyl
    Avatar
    Community Member
    114 Posts

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

    this is all kinds of ugly but it seems to work...

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

       function php($data){
             
          $result = DB::query("SELECT * FROM EventPage WHERE ID = ".$data['ID']);

          foreach ($result as $row){
             if($row['ImageID'] != '0'){
                $has_image = true;
                break;
             } else {
                $has_image = false;
                break;
             }
          }

          $bRet = true;

          if (!$has_image){
             $this->validationError(
                'Image',
                'We need an image',
                "required"
             );

             $bRet = false;
          }

          return $bRet;
       }
    }
    ?>

    there *must* be a better way...

  • swaiba
    Avatar
    Forum Moderator
    1796 Posts

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

    Yeah that isn't the greatest, I'd at least use DataObject::get_by_id and not have two if's, but hey if it works, go go go

    Glad you got something in the end!

  • ttyl
    Avatar
    Community Member
    114 Posts

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

    turns out I only had to do this for images, for other relations you can check that there is anything returned in the array associated with the has_one or has_many. in this case I'm checking to make sure a page has related people:

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

          if (!$data['People'][0]){
             $this->validationError(
                'Content',
                'We Need Instructors',
                "required"
             );

             $bRet = false;
          }

          return $bRet;
       }

    and I'm trying to make a general purpose class for images, this is what I have so far but it's horrible code.

    <?php
    class RelationshipValidator{
       function has_image($table_name, $id, $field_name){
          $result = DB::query('SELECT * FROM '.$table_name.' WHERE ID = '.$id);

          foreach ($result as $row){
             if($row[$field_name] != '0'){
                return true;
             } else {
                return false;
             }
          }
       }
    }
    ?>

    the above in use:

          if (!RelationshipValidator::has_image('EventPage', $data['ID'], 'ImageID')){
             $this->validationError(
                'Image',
                'We need an image',
                "required"
             );

             $bRet = false;
          }

  • ttyl
    Avatar
    Community Member
    114 Posts

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

    Another thing I just realized, you need to append 'Form_EditForm_' to the field name when you are validating in the CMS to get the errors to appear next to the correct fields. again, hacky - there has to be a better way...

          if (!$data['Location'] || strlen($data['Location']) > 255){
             $this->validationError(
                'Form_EditForm_Location',
                'Location is Either Missing or Longer than 255 Characters',
                "required"
             );

             $bRet = false;
          }

  • Zyzzza
    Avatar
    Community Member
    25 Posts

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

    Bump -have you found any Javascript popup solution for that ? As I have has_one attached to another tab, thus user won't see the validation text - I need it to popup in a JS alert. Any ideas ?

    4528 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.