如何在控制器中编辑表单的字段?

时间:2017-01-17 14:48:45

标签: php forms symfony

我在制作一个在我的控制器中“编辑”我的表单字段的解决方案时遇到了麻烦。

这就是我所拥有的:

我有一个symfony2表单注册为我在控制器中的函数中调用的服务。我正在删除一些对于这个其他形式不必要的字段,我正在指导我的用户,然后添加其他一些。 (我意识到我可以创建另一个表单并创建另一个服务,但是出于我的目的,这将有点矫枉过正。我这样做是因为表单功能相同,但是不需要某些字段和一些新的特定字段是。)

我现在想基本上“编辑”这种形式的一个字段......'占领'字段。该字段是choice字段,充当由一系列选项填充的单选按钮。它是required,并且在原始状态下没有empty_value要求。

我想在我的控制器功能中编辑它以获得相同的确切值,但required值为falseempty_valuenull

使用下面注释掉的代码,结果是我的“新”形式中occupation字段的消失,并被空下拉列表替换。我意识到这是因为我压倒了下面的整个领域,但我无法弄清楚如何简单地编辑它。

代码:

/**
 * Explanation of addAndRemoveFieldsInRegisterForm function:
 *  The function gets the 'registration' form and removes any
 *      fields not needed for the 'in_registration' form 
 *      and then adds the necessary fields to the form.
 */
private function addAndRemoveFieldsInRegisterForm($user)
{
    $form = $this->createForm('user_registration', $user);

    // http://stackoverflow.com/questions/10920006/pass-custom-options-to-a-symfony2-form ---
                        // --- use that to help. Look at changing the value of array.

    $form->remove('title');
    $form->remove('company');
    $form->remove('username');
    $form->remove('city');
    $form->remove('state');
    $form->remove('country');
    $form->remove('gender');
    $form->remove('age');
    $form->remove('roles');

    // $form->remove('occupation');

    // $pr = $form->get('occupation');
    //     $pr->set('required' => false);

    // $form->get('occupation')->add('required'=>false, 'empty_value'=>null);

    // $form->add('occupation','choice', array(
    //     'required' => false,
    //     'empty_value' => null, 
    // ));        
    // echo "<pre>";
    // var_dump(get_class_methods($form));die;
    $form->add('occupation','choice', array(
            'required' => false,
            'empty_value' => null,
    ));

    $form->add('canEmail', 'checkbox', array(
        'label' => 'Can Email?',
        'required' => false,
    ));

    $form->add('sendEmail', 'choice', array(
        'label' => 'Send Welcome Email? ',
        'required' => true,
        'mapped' => false,
        'expanded' => true,
        'choices' => array(
            "yes" => "Yes",
            "no" => "No"
            ),
    ));

    return $form;

}

原始表格(用作服务的表格)

private $requireOccupation;

$this->requireOccupation = true;


->add('occupation','choice', $options['occupation'])


public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $occupation = array(
            "label" => "Which of these currently describes you best? (Occupation):",
            "expanded" => true,
            'required'=> $this->requireOccupation, 
            "choices" => array(
              "X" => "X",
              "B" => "B",
              "C" => "C",
              "J" => "J",
            ),
            'constraints' => array(
                new NotBlank()
            ));

    $resolver->setDefaults(array(
        'occupation' => $occupation,
    ));
}

2 个答案:

答案 0 :(得分:0)

我认为创建另一个表单会更好。它可以从您已定义的表单中删除仅更改您想要的字段

<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
        <PropertyGroup>
            <OutputType>library</OutputType>
            <TargetFrameworks>netstandard1.4;net452</TargetFrameworks>
            <AssemblyName>MyLibrary</AssemblyName>
            <Authors>XXX</Authors>
            <Description>YYY</Description>
            <PackageId>MyLibrary</PackageId>
            <PackageVersion>1.2.3</PackageVersion>
        </PropertyGroup>
</Project>

它有利于映射到不同的对象

答案 1 :(得分:0)

首先,我意识到我想解决这个问题的方式很奇怪,因为考虑到我可以用我想要使用的字段创建另一个表单,或者只需要更改一个字段并将表单注册为服务在其他地方使用它,但我的任务是以这种方式完成它。

其次,我的解决方案非常简单。我将值传递到我的表单中,并在表单中设置了默认值。

表格形式:

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $requireOccupation = true;
    $emptyValue = null;
    //whatever other values you want to set here

    $resolver->setDefaults(array(
        'requireOccupation' => $requireOccupation,
        'emptyValue' => $emptyValue,
    ));
}

然后是字段的属性:

$builder->add('occupation', 'choice', array(
   "label" => "Some sort of label",
   "required" => $options['requireOccupation'],
   "empty_value" => $options['emptyValue'],
   ...
));

现在在控制器中:

    $form = $this->createForm('registration', $user, array(
            'requireOccupation' => false, 'emptyValue' => null
        ));

在传递要用于该表单的值时调用您要生成表单的位置。

我绝不是Symfony的专家,这个解决方案可能会产生一些问题。但它对我有用。