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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Form Questions /

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

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


Go to End


6 Posts   5284 Views

Avatar
iraira88

Community Member, 19 Posts

28 August 2013 at 1:06am

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?

Avatar
Devlin

Community Member, 344 Posts

29 August 2013 at 12:06am

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

Avatar
iraira88

Community Member, 19 Posts

29 August 2013 at 1:01am

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!

Avatar
Devlin

Community Member, 344 Posts

29 August 2013 at 1:07am

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

Avatar
iraira88

Community Member, 19 Posts

29 August 2013 at 1:21am

Awesome! Thank you very much!

Avatar
iraira88

Community Member, 19 Posts

19 October 2013 at 5:47am

Edited: 19/10/2013 5:47am

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!