views:

317

answers:

2

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?

+2  A: 

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.

develop7
Can you illustrate with an example?
No, right now I can't. Hint: dig into and override `sfWidgetFormSelectRadio::render()`.
develop7
Thanks.But I can't find the code to **dynamically** populate the options there.
@user198729 what you mean by "dynamically"? Can you describe how it should look like?I think you just have to pass urls as options' labels.
develop7
A: 

In your form configuration insert the following:

$this->widgetSchema['choices'] = new sfWidgetFormChoice (array('choices' => $custom_values_array));
Radu Dragomir
Can't you move the logic behind the building of $custom_values_array from the action to the form?
Radu Dragomir