Symfony 3.4动态名称和数据

时间:2018-02-12 20:41:50

标签: symfony-3.4

我将symfony版本更改为3.4并面对表单组件的问题 在使用像服务之类的表单之前,首先创建并修改然后创建它。但现在它改变了,create函数等待类名。而且我不知道在这种情况下如何定制。 symfony 3.3中的示例我有循环,我创建了许多具有特定名称和数据的表单

foreach ($taskExecutions as $i => $execution) {

            $wtf = $this->getChoiceFiledType();
            $wtf->setDataChoice($workTypeObjects);
            $name = 'work_type_object';
            $wtf->setName("{$name}_id_{$i}");
            $wtf->setPlaceHolder($this->getAdditionalFunction()
                ->transLocal('select_default_value', 'common'));
            $renderValue['renderFormWT'] = $this->getFormFactoryInterface()
                ->create(
                    $wtf,
                    null,
                    ['attr' => ['class' => 'work_type_object_id', 'id' => "{$name}_form_{$i}"]]
                )
                ->createView();

 }

我的表单来自自定义父母

class ChoiceFiledType extends ExtendsTimeSheetsFilterForm
{
/**
 * @param OptionsResolver $resolver
 */
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults([
        'required' => false,
        'choices' => $this->getDataChoice(),
        'empty_value' => $this->getPlaceHolder(),
    ]);
}

/**
 * @return string
 */
public function getParent()
{
    return 'Symfony\Component\Form\Extension\Core\Type\ChoiceType';
}
}

包含选项的父类:

class ExtendsTimeSheetsFilterForm extends AbstractType
{
/**
 * @var string
 */
private $name = 'timesheets';

/**
 * @var array
 */
private $dataChoice = [];

/**
 * @var string
 */
private $placeHolder = '';

/**
 * @return string
 */
public function getBlockPrefix()
{
    return $this->name;
}

/**
 * @param string $name
 */
public function setName($name)
{
    $this->name = $name;
}

/**
 * @return array
 */
public function getDataChoice()
{
    return $this->dataChoice;
}

/**
 * @param array $dataChoice
 */
public function setDataChoice($dataChoice)
{
    $this->dataChoice = $dataChoice;
}

/**
 * @return string
 */
public function getPlaceHolder()
{
    return $this->placeHolder;
}

/**
 * @param string $placeHolder
 */
public function setPlaceHolder($placeHolder)
{
    $this->placeHolder = $placeHolder;
}
}

在新版本的symfony中,我没有机会像服务一样获得表单,然后自定义一些像这样的数据

            $wtf = $this->getChoiceFiledType();
            $wtf->setDataChoice($workTypeObjects);
            $name = 'work_type_object';
            $wtf->setName("{$name}_id_{$i}");
            $wtf->setPlaceHolder($this->getAdditionalFunction()
                ->transLocal('select_default_value', 'common'));

我只能通过使用字符串类名来创建,但是......如何修改数据呢?示例自定义名称表单或我希望机会更改dataChoiceplaceholder

                $renderValue['renderFormWT'] = $this->getFormFactoryInterface()
                ->create(
                    ChoiceFiledType::class,
                    null,
                    ['attr' => ['class' => 'work_type_object_id', 'id' => "{$name}_form_{$i}"]]
                )
                ->createView();

它在symfony 3.4中是如何工作的,或者可能是另一种方式?

1 个答案:

答案 0 :(得分:0)

大多数时候,答案是in the documentation ......

我的服务定义:

app.security_handler:
  class: App\Listener\Security\SecurityHandler

App\Listener\Security\SecurityHandler: '@app.security_handler'
现在在security.yaml

form_login:
  login_path: /admin/login
  check_path: /admin/login
  default_target_path: /admin
  username_parameter: login[username_or_email]
  password_parameter: login[password]

  success_handler:    app.security_handler
  failure_handler:    app.security_handler