user198729
user198729

Reputation: 63626

How to dynamically populate a choices widget in symfony?

Suppose there is an image_url column in database.

I want the user to choose from several recommended images,which is something like this:

<input type="radio" value="domain.com/path_to_img1" name="image_url" />
<img src="domain.com/path_to_img1" />
<input type="radio" value="domain.com/path_to_img2" name="image_url" />
<img src="domain.com/path_to_img2" />

Where the image urls are generated on the fly.

How to do this in a symfony flavor by sfForm?

I tried this:

$form->widgetSchema['key'] = new sfWidgetFormSelect(...)

But get a fatal error:

Cannot access protected property

But what's the exact way to do this?

Upvotes: 1

Views: 1060

Answers (2)

Radu Dragomir
Radu Dragomir

Reputation: 660

In your form configuration insert the following:

$this->widgetSchema['choices'] = new sfWidgetFormChoice (array('choices' => $custom_values_array));

Upvotes: 0

Andrei Dziahel
Andrei Dziahel

Reputation: 969

Well, standart approach is to write a widget.

In your concrete case it seems you actually want to perform a choice, right? So you have to implement another sfWidgetFormChoice renderer. To do that, inherit sfWidgetFormSelectRadio (let's call them sfWidgetFormSelectRadioImage) to learn them to render labels as <img> tags. Then ask sfWidgetFormChoice explicitly to render itself with sfWidgetFormSelectRadioImage class, and that should be all.

Upvotes: 2

Related Questions