alex
alex

Reputation: 143

Symfony 1.4 Label position in a form

Is there a way to change the position of the label in a form? I would like to have it above the input field (sfWidgetFormInputText and sfWidgetFormChoice) . Is that possible?

Upvotes: 0

Views: 1504

Answers (2)

antony
antony

Reputation: 2893

Depends on how you're rendering the form field. You can do it a few ways:

If you want to render an individual field differently to the rest in your form, you can render the label and widget separately like this in your template:

// \apps\myApp\modules\myModule\templates\_form.php
echo $form['field_name']->renderLabel(); // Render the label
echo $form['field_name']->render(); // Render the widget
echo $form['field_name']->renderError(); // Render the error
echo $form['field_name']->renderHelp(); // Render the help message

If you want to apply the layout across your entire form. You should create a new formatter class and apply it to your form:

// \lib\form\formatter\sfWidgetFormSchemaFormatterCustom.class.php
class sfWidgetFormSchemaFormatterCustom extends sfWidgetFormSchemaFormatter
{
  protected
    $rowFormat       = '<div class="label">%label%</div><div class="field">%field%</div>%error% \n %help %hidden_fields%',
    $errorRowFormat  = '<div>%errors%</div>',
    $helpFormat      = '<div class="form_help">%help%</div>',
    $decoratorFormat = '<div>\n  %content%</div>';
}

Apply it in your form's configure method like this:

// \lib\form\myForm.class.php
public function configure()
{   
    //....

    $formatter = new sfWidgetFormSchemaFormatterCustom($this->getWidgetSchema());
    $this->widgetSchema->addFormFormatter('custom', $formatter);
    $this->widgetSchema->setFormFormatterName('custom');
}

If you want the formatter to be used globally across your project, you can set it as the default formatter in your ProjectConfiguration.class.php.

// \config\ProjectConfiguration.class.php
class ProjectConfiguration extends sfProjectConfiguration
{
    public function setup()
    {
        // ...

        sfWidgetFormSchema::setDefaultFormFormatterName('custom');
    }
}

Upvotes: 2

Goku
Goku

Reputation: 2139

You have to do it in the template associated with the form in which you can reorganize all the form using html tags (<th>, <tr>, <td> ....).

Upvotes: 0

Related Questions