php 在symfony中,如何设置表单域的值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1029636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:41:49  来源:igfitidea点击:

In symfony, how to set the value of a form field?

phpmysqlsymfony1propel

提问by James Skidmore

I'm overriding my doSave() method to basically do the following: I have a sfWidgetFormPropelChoice field that the user can either choose from, or type a new option. How can I change the widget's value? Or maybe I am approaching this the wrong way. So here is how I overrode the doSave() method:

我正在覆盖我的 doSave() 方法以基本上执行以下操作:我有一个 sfWidgetFormPropelChoice 字段,用户可以从中进行选择或键入新选项。如何更改小部件的值?或者也许我以错误的方式接近这个。所以这里是我如何覆盖 doSave() 方法:

public function doSave($con = null)
{
    // Save the manufacturer as either new or existing.
    $manufacturer_obj = ManufacturerPeer::retrieveByName($this['manufacturer_id']->getValue());
    if (!empty($manufacturer_obj))
    {
        $this->getObject()->setManufacturerId($manufacturer_obj->getId()); // NEED TO CHANGE THIS TO UPDATE WIDGET'S VALUE INSTEAD?
    }
    else
    {
        $new = new Manufacturer();
        $new->setName($this['manufacturer_id']->getValue());
        $new->save();
        $this->getObject()->setManufacturerId($new->getId()); // NEED TO CHANGE THIS TO UPDATE WIDGET'S VALUE INSTEAD?
    }

    parent::doSave($con);
}

回答by Henrik Bj?rnskov

You should use setDefault or setDefaults and then it will autopopulate with the bound values.

您应该使用 setDefault 或 setDefaults ,然后它将使用绑定值自动填充。

(sfForm) setDefault ($name, $default)
(sfForm) setDefaults ($defaults)

usage

用法

$form->setDefault('WidgetName', 'Value');
$form->setDefaults(array(
    'WidgetName' => 'Value',
));

回答by Dreur

You could do it in the action :

你可以在行动中做到:

$this->form->getObject()->setFooId($this->foo->getId()) /*Or get the manufacturer id or name from request here */
$this->form->save();

But I prefer to do the kind of work you are doing with your manufacturer directly in my Peer so my business logic is always at the same place.

但我更喜欢直接在我的 Peer 中与制造商一起做那种工作,所以我的业务逻辑总是在同一个地方。

What I put in my forms is mainly validation logic.

我放在表单中的主要是验证逻辑。

Example of what to put in the save method of the Peer :

在 Peer 的 save 方法中放入内容的示例:

public function save(PropelPDO $con= null)
{
  if ($this->isNew() && !$this->getFooId())
  {
    $foo= new Foo();
    $foo->setBar('bar');
    $this->setFoo($foo);
   } 
}

回答by bb.

Two assumption here: a) your form gets the name of the manufacturer and b) your model wants the ID of a manufacturer

这里有两个假设:a)您的表单获取制造商的名称 b)您的模型需要制造商的 ID

public function doSave($con = null)
{
    // retrieve the object from the DB or create it
    $manufacturerName = $this->values['manufacturer_id'];
    $manufacturer = ManufacturerPeer::retrieveByName($manufacturerName);
    if(!$manufacturer instanceof Manufacturer)
    {
        $manufacturer = new Manufacturer();
        $manufacturer->setName($manufacturerName);
        $manufacturer->save();
    }

    // overwrite the field value and let the form do the real work
    $this->values['manufacturer_id'] = $manufacturer->getId();

    parent::doSave($con);
}