Symfony 1.2 - sfForm - unsetAllExcept - better way?

It’s kind of update of function: unsetAllExcept. Lately I had a problem with unsetting some default widgets. Using for example propel and relations m-n, auto-generated forms have widgets that cannot be unsets using the current version of function - it only unset widgets by form object fields.

I think it could be useful:


<?php

  /**
   *  unset all fields except given parameters
   *
   * @param array $fields Array of fields
   */
  public function unsetAllExcept($fields = array())
  {
  	$tmp = array_keys($this->widgetSchema->getFields());

  	foreach(array_diff($tmp, $fields) as $value){

  		unset($this[$value]);
  	}
  }

?>

1 Response to “Symfony 1.2 - sfForm - unsetAllExcept - better way?”


  1. 1 Jasper Lammertink

    Hidden fields can’t be removed because they are beeing used in CRUD actions and maybe also in other actions.

    Use this instead:

    [code]
    public function useFields($fields)
    {
    foreach ($this->getWidgetSchema()->getFields() as $name => $widget)
    {
    if (!in_array($name, $fields) && !$widget->isHidden())
    {
    unset($this[$name]);
    }
    }
    }
    [/code]

  1. 1 Symfony 1.2 - using sfForm with jquery validation plugin - part 2 | Projektant - Programista PHP

Leave a Reply

PodglÄ…d komentarza:




About me:

  • PHP programmer
  • Symfony developer
  • Zend framework developer

Categories: