symfony sfForm cheat sheet

Get list/array of field names


Custom render form


    <?php foreach ($form as $position): ?>
        <?php echo $position->renderLabel() ?>
    <?php endforeach ?>

      <?php foreach ($form as $position): ?>
          <?php echo $position->render() ?>
          <?php echo $position->renderError() ?>
      <?php endforeach ?>



Move a form field

   * Moves a field in a given position
   * Available actions are:
   *  * sfWidgetFormSchema::BEFORE
   *  * sfWidgetFormSchema::AFTER
   *  * sfWidgetFormSchema::LAST
   *  * sfWidgetFormSchema::FIRST
   * @param string   $field  The field name to move
   * @param constant $action The action (see above for all possible actions)
   * @param string   $pivot  The field name used for AFTER and BEFORE actions
$this->getWidgetSchema()->moveField('is_active', sfWidgetFormSchema::AFTER, 'created_at');


Embeded forms

Render an embeded form:

// "attributes" is the name of the "field" for the embeded form
echo $form->offsetGet('attributes')->render();


Render a field of an embeded form:

echo $form->offsetGet('attributes')->offsetGet('my_field')->render();


Distinguish between a field and an embeded form:

  • A field is instance of 'sfFormField'
  • A embeded form is instance of 'sfFormFieldSchema'

Global / Post Validators

Make one field required depending on the status of another

  protected function modifyGeneratorAfterBuildForm($row)
    $form = $this->generator->getForm();

      new sfValidatorCallback(array('callback' => array($this, 'validatePaymentType')))
  protected function validatePaymentType($validator, $values)
    if (!$values['is_paid'] && !$values['ull_payment_type_id'])
      $error = new sfValidatorError($validator, 'Required');
      // throw an error bound to the password field
      throw new sfValidatorErrorSchema($validator, array('ull_payment_type_id' => $error));
    return $values;


  • sfForm
    • widgetSchema
      • class sfWidgetFormSchema
    • validatorSchema
    • errorSchema
    • formFieldSchema
      • getFormFieldSchema()
        • If none exists, it is created on the fly using widgetSchema and values:
          • if not bound - defaults
          • if bound - tainted values (post request data)
    • formFields
    • defaults
    • fieldNames
  • sfWidgetFormSchema
    • represents an array of fields.
    • Seems to be an array of widgets
  • sfFormField
    • represents a widget bind to a name and a value.
  • sfFormFieldSchema
    • represents an array of widgets bind to names and values