Posts Tagged ‘Doctrine’

Allow empty selection in sfWidgetFormDoctrineChoice / Unline doctrine relations

Tuesday, October 12th, 2010

The standard sfWidgetFormDoctrineChoice “add_empty” option is only useful if the relation has not yet been set. However, once a relation is saved, selecting the empty value again will result in an validation error.

Here is a simple way to allow users to unlink relations by selecting the empty value of a sfWidgetFormDoctrineChoice select list. Simply add these lines to your form’s configure()-method:

$this->widgetSchema['rdr_images_list']->setOption('add_empty', true);
$valuesRaw = sfContext::getInstance()->getRequest()->getParameter($this->getName());
if (1 === count($valuesRaw['images_list']) && '' === $valuesRaw['images_list'][0]) {
  $this->validatorSchema['images_list'] = new sfValidatorPass();
}