views:

47

answers:

1

In action.class.php:

$form = new NewsForm();
$form->setWidget('thumbnail', new sfWidgetFormSelect(array('choices' => $news['images'])));
$form->getWidget('summarize')->setDefault($news['summarize']);
$form->getWidget('title')->setDefault($news['title']);

Where $news is generated in previous steps;

It looks redundant,how to refactor it?

A: 

Well your code is not redunant until you use the same code in mutliple actions.
To make it reusable, you can make use of the options parameter in the the form constructor and change the form as follows:

class NewsForm extends ... {

    public function configure() {
        //Whatever you do here
        //....

        // if a news is set we configure certain fields
        if($news = $this->getOption('news', false)) {
            $this->setWidget('thumbnail', new sfWidgetFormSelect(array('choices' => $news['images'])));
            $this->setDefault('summarize', $news['summarize']);
            $this->setDefault('title', $news['title']);
        }
    }
}

The you can create the form with:

$form = new NewsForm(array(), array('news' => $news));

Reference: sfForm - getOption

Felix Kling