Come creare con Symfony 1.4.22 un semplice form per i contatti.

Form dentro al Controller

1. apps/modules/contenuto/actions/actions.class.php


<?php

class contenutoActions extends sfActions // estende la classe Symfony
{
  public function executeContact($request) // crea la pagina http://localhost/jobeet/web/contenuto/contact
    {
      $this->form = new sfForm(); // istanzia la classe di Symfony per la creazione dei form
      // setta i widget, cioè il tipo di campo e i parametri
      $this->form->setWidgets(array(
        'name'    => new sfWidgetFormInputText(),
        'email'   => new sfWidgetFormInputText(array('default' => 'me@example.com')),
        'subject' => new sfWidgetFormChoice(array('choices' => array('Subject A', 'Subject B', 'Subject C'))),
        'message' => new sfWidgetFormTextarea(),
      ));
      // la richiesta inviata a contactSuccess.php sarà un oggetto composto da array
      // l'oggetto è contenuto nella variabile  - form - in contactSuccess.php sarà - echo $form -
    }
}// END class

2. apps/modules/contenuto/templates/contactSuccess.php


<?php echo $form->renderFormTag('contenuto/contact') ?>
  <table>
    <?php echo $form ?>
    <tr>
      <td colspan="2">
        <input type="submit" />
      </td>
    </tr>
  </table>
</form>

3. Puntare il browser a: http://localhost/jobeet/web/frontend_dev.php/contenuto/contact


<form action="/frontend_dev.php/contenuto/contact" method="POST">
  <table>
    <tr>
      <th><label for="name">Name</label></th>
      <td><input type="text" name="name" id="name" /></td>
    </tr>
    <tr>
      <th><label for="email">Email</label></th>
      <td><input type="text" name="email" id="email" value="me@example.com" /></td>
    </tr>
    <tr>
      <th><label for="subject">Subject</label></th>
      <td>
        <select name="subject" id="subject">
          <option value="0">Subject A</option>
          <option value="1">Subject B</option>
          <option value="2">Subject C</option>
        </select>
      </td>
    </tr>
    <tr>
      <th><label for="message">Message</label></th>
      <td><textarea rows="4" cols="30" name="message" id="message"></textarea></td>
    </tr>
    <tr>
      <td colspan="2">
        <input type="submit" />
      </td>
    </tr>
  </table>
</form>

Reference:
http://symfony.com/legacy/doc/gentle-introduction/1_4/it/10-forms