[rb]symfony’s 1.4 sfForm isValid()

Someone asked me a few days ago how can he use the sfform validation feature to validate an object created “by hand”. While i’m not quite sure yet of the usage of such a thing, here’s my approach(we’ll assume we have a table called Selections with 4 fields like id(incremental/key/bigint), target_id(bigint), type(varchar), name(varchar)):

public function executeAdd(sfWebRequest $request) {
if($request->isXmlHttpRequest()) {


$this->forward404Unless($id = $request->getParameter('id'));
$this->forward404Unless($type = $request->getParameter('type'));
$selection = new Selections();

$form = new SelectionsForm(null, array(), false);
$values = $selection->toArray();

if($form->isValid()) { $form->save(); echo 'OK'; }
else {
echo 'ERROR';

return sfView::NONE;
} else $this->forward404();