The forms "sub-framework" has been my greatest hurdle with using sf 1.4. I am attempting to embed the i18n form in the main form and customize the display of the form on the page. If I just use <?php echo $form ?>, then I have no problems... it saves to the database fine, but if I try to customized the display it seems it is no longer treated as one object. I'm sure this is rather simple and straightforward, as it seems like a common requirement, but for the life of me I cannot figure it out. Any help would be greatly appreciated.
I have an internationalized model:
- Code: Select all
Table1:
actAs:
Timestampable: ~
I18n:
fields: [name, description]
columns:
name: { type: string(255), notnull: true }
description: { type: string(1000) }
is_public: { type: boolean, default: 1 }
Below is my Table1Form.class.php
- Code: Select all
class Table1Form extends BaseTable1Form
{
public function configure()
{
$this->embedI18n($this->getCurrentCulture());
}
public function getCurrentCulture()
{
return isset($this->options['culture']) ? explode(',',str_replace(' ', '', $this->options['culture'])) : array('en');
}
}
in my Action:
- Code: Select all
$this->form = new Table1Form(null, array('culture'=> 'en,es'));
if($request->isMethod('post')){
$this->form->bind($request->getParameter('table1'));
if($this->form->isValid()){
$table1 = $this->form->save();
$this->redirect($url.'?id='.$table1->getId());
}
}
If I use the following piece of code, it all works fine:
- Code: Select all
<table><?php echo $form?>
<tr>
<td><input type="submit" /></td>
</tr>
</table>
But this is not very flexible, so I tried this:
- Code: Select all
<?php echo $form->renderFormTag('/table1/create')?>
<?php echo $form->renderHiddenFields()?>
<?php foreach ($form->getEmbeddedForms() as $i18n){?>
<div class="form_row">
<?php echo $i18n['name']->renderLabel()?><?php echo $i18n['name']->renderError()?>
<div><?php echo $i18n['name']->render()?></div>
</div>
<div class="form_row">
<?php echo $i18n['description']->renderLabel()?><?php echo $i18n['description']->renderError()?>
<div><?php echo $i18n['description']->render()?></div>
</div>
<?php }?>
<div class="form_row">
<?php echo $form['is_public']->renderLabel()?><?php echo $form['is_public']->renderError()?>
<div><?php echo $form['is_public']->render()?></div>
</div>
<button type="submit" class="button"><?php echo __('Next')?></button>
</form>
The form looks great now and I can apply my css, but now it no longer binds correctly. Perhaps there is another way to loop through the embeded forms. When I customize the form this way, the name of the "name" input field is - name="table1_translation[name]", instead of - name="table1[en][name]" as it should be (and is if I just use <?php echo $form ?>). What am I doing wrong? How can I preserve this nested structure of the request parameters so I can retrieve them and bind them properly with $this->form->bind($request->getParameter('table1'));
Thanks for your help!!
