Jump to:

1999 Posts in 1408 Topics by 613 members

Form Questions

SilverStripe Forums » Form Questions » Images next to checkboxes -> Fatal error: Call to a member function getURL() on a non-object

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

Page: 1
Go to End
Author Topic: 1080 Views
  • iraira88
    Avatar
    Community Member
    17 Posts

    Images next to checkboxes -> Fatal error: Call to a member function getURL() on a non-object Link to this post

    Hello.

    I'm trying to create a form where the visitor of the website can buy a book/catalog. The visitor should be able to choose which of these catalogs he wants by selecting one or more checkboxes. Next to each checkbox there should be a thumbnail image of the appropriate catalog. The person who will fill the website with content later in the cms should be able to upload or change these images. There will be a limited number of catalogs (two exists already - a few will follow soon), so for now I need only five checkboxes which should only appear if an appropriate image is uploaded. I tried to implement it like this:

    in the php-file:

    static $db = array(
          'Mailto' => 'Varchar(100)',
          'SubmitText' => 'Text',
          'Shop' => 'HTMLText',
          'Book1Txt' => 'Text',
          'Book2Txt' => 'Text',
          'Book3Txt' => 'Text',
          'Book4Txt' => 'Text',
          'Book5Txt' => 'Text'
       );

    (…)

    function ContactForm() {
             
          $book1 = $this->Book1();
          $book2 = $this->Book2();
          $book3 = $this->Book3();
          $book4 = $this->Book4();
          $book5 = $this->Book5();
          
          if (!empty($book1)) {$book1 = $this->Book1()->CroppedImage(200,200)->getURL();}
          if (!empty($book2)) {$book2 = $this->Book2()->CroppedImage(200,200)->getURL();}
          if (!empty($book3)) {$book3 = $this->Book3()->CroppedImage(200,200)->getURL();}
          if (!empty($book4)) {$book4 = $this->Book4()->CroppedImage(200,200)->getURL();}
          if (!empty($book5)) {$book5 = $this->Book5()->CroppedImage(200,200)->getURL();}
          

          $fields = new FieldSet(
           new CheckboxSetField(
             $name = "Books",
             $title = "Bitte wählen Sie mindestens ein Buch aus",
             $source = array(
             "1" => "<img src='$book1' />",
             "2" => "<img src='$book2' />",
             "3" => "<img src='$book3' />",
             "4" => "<img src='$book4' />",
             "5" => "<img src='$book5' />"
             ),
             $value = "1"
           ),
           new TextField('Name', 'Name*'),
           new TextField('Firstname', 'Vorname*'),
           new TextField('Adress', 'Adresse*'),
           new EmailField('Mail', 'Email*'),
           new TextareaField('Comments','Kommentar*')
          );
          
          // Create action
          $actions = new FieldSet(
           new FormAction('SendContactForm', 'Send')
          );
          
          // Create Validators
          $validator = new RequiredFields('Name', 'Firstname', 'Adress', 'Mail');
          
          return new Form($this, 'ContactForm', $fields, $actions, $validator);
       }

    and in my Template:

    <div id="Content" class="typography">
    <% if Success %>
    $SubmitText
    <% else %>
    $ContactForm
    <% end_if %>
    </div>

    But this doesn't seem to work..I always get this error:

    "Fatal error: Call to a member function getURL() on a non-object in (...)"

    However, I am not good enough in PHP and SilverStripe and do not know how to solve this..can anyone help me, please?

  • Devlin
    Avatar
    Community Member
    215 Posts

    Re: Images next to checkboxes -> Fatal error: Call to a member function getURL() on a non-object Link to this post

    Try:

    $source = array();
    if( $this->Book1() && $this->Book1()->is_a('Image') ) {
    $image = $this->Book1()->CroppedImage(200,200);
    if( $image && $image->is_a('Image') ) { $source[1] = $image->getURL(); }
    }
    // ...

    new CheckboxSetField(
    $name = "Books",
    $title = "Bitte w&auml;hlen Sie mindestens ein Buch aus",
    $source,
    $value = "1"
    )

  • iraira88
    Avatar
    Community Member
    17 Posts

    Re: Images next to checkboxes -> Fatal error: Call to a member function getURL() on a non-object Link to this post

    Hi Devlin,

    thank you for answering! I've tried your code..now there is no error message anymore but I get only the path of the images ( e.g. assets/Uploads/_resampled/croppedimage200200-resizedimage340340-buch2.jpg) next to my checkboxes, not the image itself. Any ideas how to fix that?

    Thank you!

  • Devlin
    Avatar
    Community Member
    215 Posts

    Re: Images next to checkboxes -> Fatal error: Call to a member function getURL() on a non-object Link to this post

    Oh yeah, sorry.

    Change:

    if( $image && $image->is_a('Image') ) { $source[1] = $image->getURL(); }

    to:

    if( $image && $image->is_a('Image') ) { $source[1] = $image->getTag(); }

  • iraira88
    Avatar
    Community Member
    17 Posts
  • iraira88
    Avatar
    Community Member
    17 Posts

    Re: Images next to checkboxes -> Fatal error: Call to a member function getURL() on a non-object Link to this post

    Hello again!

    Now I have a further question referring to the same issue. I tried to add some text to the pictures? Something like a heading or a caption for the product. But I just don't get it..how can I integrate this into the array? Or: what is the best solution for this?

    Thank you, any help would be great!

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