开发者

Help With Embedded BaseFormDoctrine Forms

开发者 https://www.devze.com 2023-01-14 00:30 出处:网络
I have the following code blocks: class MerchantStoreForm extends sfForm { public function configure() { $this->disableCSRFProtection();

I have the following code blocks:

class MerchantStoreForm extends sfForm
{
  public function configure()
  {
    $this->disableCSRFProtection();

    $this->setWidgets(array(
          'brand_id' => new sfWidgetFormDoctrineChoice(array('label'=> 'Store Brand','model'=>'Brand','add_empty'=>'-Select Brand-','method'=>'getName','key_method'=>'getId','order_by'=>array('name','asc'))),
          'newbrand'    => new sfWidgetFormInputCheckbox(array('label' => 'New'),array('value'=>'Y'))
    ));

    $this->setValidators(array(
        'new开发者_运维技巧brand' => new sfValidatorString(array('required'=>false)),          
        'brand_id'  => new sfValidatorDoctrineChoice(array('model'=>'Brand'))
    ));

     $brand = new Brand();
    $brand_form = new BrandForm();
    $brand_form->widgetSchema['name']->setAttribute('style','display:none');
    $this->embedForm('brand', $brand_form);

    $this->getWidgetSchema()->setNameFormat('store[%s]');
  }

  public function execute()
  { 
   $form_values = $this->getValues();

    if($form_values['newbrand'])
    {
        $brand_form = $this->getEmbeddedForm('brand');
        $brand_form->save();
        $brand = $brand_form->getObject();
    }
    else
    {
        $brand = doctrine::getTable('Brand')->findOneById($form_values['brand_id']);
    }

    return $brand->getId();
  }
}

Two questions:

1) The magic of $brand_form->save() doesn't work for me. I get a 500 Internal Server Error sfValidatorErrorSchema error pointing to the following piece of code in my symfony generated BaseBrandForm.class.php:

...
$this->widgetSchema->setNameFormat('brand[%s]');
$this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);
...

This works instead in replacement though:

        $brand_form->updateObject($form_values['brand']);
        $brand_form->getObject()->save();

Why is this?

2) Why do I get an undefined method error while calling getter method on the object of the BaseFormDoctrine embedded form: return $brand->getId();

Thanks in advance for your help.

Sharmil


1) BrandForm throws an exception because it doesn't have any values. Classes that extend sfFormObject don't play nicely when embedded directly into non object forms (like sfForm).

What is MerchantStoreForm doing? Depending on the situation, it should probably be extending sfFormObject or BrandForm should be the top level form. If this isn't possible, you'll have to write add a save method to MerchantStoreForm that calls updateObject and save. To better understand what's happening, go through the logic that takes place in sfFormObject - it's worth knowing especially if you're using embedded forms.

2) No clue here. I would see what $brand is actually an instance of. If it's a record and that record has an id field, there's no reason that shouldn't work.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号