Question

I have a class with the property "homepages" where urls are stored in an array. Now I want to create an admin form to edit this field, but I don't get it to display the stored values or to accept adding a new value to the property. All I get is an empty form field.

To simply display the values in a partial is easy with the code below, but how do I get it to render the input field with all the previously stored values?

<div class="sf_admin_form_row">
<label>
    <?php echo $form['homepages']->renderLabel('homepages') ?>
</label>
<?php foreach(sfOutputEscaper::unescape($form->getObject()->getHomepages()) as    $key=>$value): ?>
<div class="content"><?php echo $value ?></div>
<?php endforeach ?>
</div>

Any help will be much appreciated.

Was it helpful?

Solution

What I would try to do is either create a collection of embedded forms or write my own widget that will handle the field.

If you try with the first solution you would have to unserialize your property inside of the form class. Then iterate through the array and embedd a simple form with one input field and a value of your field. Then add an option to add new values (by embedding another form) or deleting them. You would have to also overwrite the save methods of your forms so they properly handle the field (i.e. serialize the field).

Alternatively you can create a widget class which will properly handle displaying of the collection of inputs (again - unserialize the value and display an input for each URL). You would also have to create a validator class which will take the values from the inputs and serialize them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top