• Edit
  • Delete

symfony sfForm cheat sheet

Get list/array of field names

$form->getWidgetSchema()->getPositions();

Custom render form

<table>

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

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

</table>

 

Embeded forms

http://jmather.com/2011/01/29/6-things-to-know-about-embedded-forms-in-symfony/

http://arialdomartini.wordpress.com/2011/04/01/how-to-kill-symfony%E2%80%99s-forms-and-live-well/

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();

    $form->mergePostValidator(
      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;
  }

Architecture

  • 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
  •