Symfony 2.3表单没有将所有字段传递给formView

时间:2014-01-14 21:22:37

标签: php forms symfony doctrine-orm twig

我希望有人可以帮我调试我遇到的问题,表单字段没有传递到Symfony 2.3中的表单视图。

我有一张我正在制作的表格,这是我几个月前制作的。表单工作正常,直到我决定添加几个字段。我将字段添加到实体并更新数据库架构没有问题。我将字段添加到表单类型类,然后将它们添加到树枝模板中。

这就是问题出现的地方,我不断收到错误说

Method "adminFromEmail" for object "Symfony\Component\Form\FormView" does not exist in SaveAdminBundle:System:systemsettings.html.twig at line 38  

所以我开始做一些挖掘。我做了表格的dump(),得到了以下回复:

$form变量转储

object(stdClass)#2761 (13) { ["__CLASS__"]=> string(27) "Symfony\Component\Form\Form" 
["config"]=> object(stdClass)#2769 (4) { 
    ["__CLASS__"]=> string(34) "Symfony\Component\Form\FormBuilder" 
    ["children"]=> string(8) "Array(0)" 
    ["unresolvedChildren"]=> string(8) "Array(0)" 
    ["locked"]=> bool(true) } 
    ["parent"]=> NULL 
    ["children"]=> object(stdClass)#2773 (4) { 
        ["__CLASS__"]=> string(42) "Symfony\Component\Form\Util\OrderedHashMap" 
        ["elements"]=> string(9) "Array(14)" 
        ["orderedKeys"]=> string(9) "Array(14)" 
        ["managedCursors"]=> string(8) "Array(0)" 
    } 
    ["errors"]=> array(0) { } 
    ["submitted"]=> bool(false) 
    ["modelData"]=> object(stdClass)#2774 (17) { 
         ["__CLASS__"]=> string(38) "Save\AdminBundle\Entity\SystemSettings" 
         ["id"]=> int(1) 
         ["loginUrl"]=> string(36) "#######################" 
         ["resetUrl"]=> string(36) "#######################" 
         ["clientLoginUrl"]=> string(36) "####################" 
         ["adminLoginUrl"]=> string(36) "######################" 
         ["adminFromEmail"]=> string(0) ""
         ["adminFromName"]=> string(0) "" 
         ["userRegistrationEmail"]=> string(57) "Proxies\__CG__\Save\MailMessagesBundle\Entity\MailMessage" 
         ["userRegistrationByAdminEmail"]=> NULL 
         ["userRegistrationByClientEmail"]=> NULL 
         ["clientRegistrationEmail"]=> string(57) "Proxies\__CG__\Save\MailMessagesBundle\Entity\MailMessage" 
         ["clientRegistrationEmailAdminCopy"]=> string(57) "Proxies\__CG__\Save\MailMessagesBundle\Entity\MailMessage" 
         ["adminRegistrationEmail"]=> NULL 
         ["loginResetByAdminEmail"]=> string(57) "Proxies\__CG__\Save\MailMessagesBundle\Entity\MailMessage" 
         ["passwordResetStartEmail"]=> NULL 
         ["passwordResetEndEmail"]=> NULL 
     } 
     ["normData"]=> object(stdClass)#2788 (17) { 
         ["__CLASS__"]=> string(38) "Save\AdminBundle\Entity\SystemSettings" 
         ["id"]=> int(1) 
         ["loginUrl"]=> string(36) "###################" 
         ["resetUrl"]=> string(36) "###################" 
         ["clientLoginUrl"]=> string(36) "##################" 
         ["adminLoginUrl"]=> string(36) "###################" 
         ["adminFromEmail"]=> string(0) "" 
         ["adminFromName"]=> string(0) "" 
         ["userRegistrationEmail"]=> string(57) "Proxies\__CG__\Save\MailMessagesBundle\Entity\MailMessage" 
         ["userRegistrationByAdminEmail"]=> NULL 
         ["userRegistrationByClientEmail"]=> NULL 
         ["clientRegistrationEmail"]=> string(57) "Proxies\__CG__\Save\MailMessagesBundle\Entity\MailMessage" 
         ["clientRegistrationEmailAdminCopy"]=> string(57) "Proxies\__CG__\Save\MailMessagesBundle\Entity\MailMessage" 
         ["adminRegistrationEmail"]=> NULL 
         ["loginResetByAdminEmail"]=> string(57) "Proxies\__CG__\Save\MailMessagesBundle\Entity\MailMessage" 
         ["passwordResetStartEmail"]=> NULL 
         ["passwordResetEndEmail"]=> NULL 
     } 
     ["viewData"]=> object(stdClass)#2789 (17) { 
         ["__CLASS__"]=> string(38) "Save\AdminBundle\Entity\SystemSettings" 
         ["id"]=> int(1) 
         ["loginUrl"]=> string(36) "#####################" 
         ["resetUrl"]=> string(36) "#####################" 
         ["clientLoginUrl"]=> string(36) "#####################" 
         ["adminLoginUrl"]=> string(36) "#####################" 
         ["adminFromEmail"]=> string(0) "" 
         ["adminFromName"]=> string(0) "" 
         ["userRegistrationEmail"]=> string(57) "Proxies\__CG__\Save\MailMessagesBundle\Entity\MailMessage" 
         ["userRegistrationByAdminEmail"]=> NULL 
         ["userRegistrationByClientEmail"]=> NULL 
         ["clientRegistrationEmail"]=> string(57) "Proxies\__CG__\Save\MailMessagesBundle\Entity\MailMessage" 
         ["clientRegistrationEmailAdminCopy"]=> string(57) "Proxies\__CG__\Save\MailMessagesBundle\Entity\MailMessage" 
         ["adminRegistrationEmail"]=> NULL 
         ["loginResetByAdminEmail"]=> string(57) "Proxies\__CG__\Save\MailMessagesBundle\Entity\MailMessage" 
         ["passwordResetStartEmail"]=> NULL 
         ["passwordResetEndEmail"]=> NULL 
     } 
     ["extraData"]=> array(0) { } 
     ["synchronized"]=> bool(true) 
     ["defaultDataSet"]=> bool(true) 
     ["lockSetData"]=> bool(false) } 

但是,当我在调用$form->createView()后转储视图时,我得到了这个:

$view变量转储

object(stdClass)#2761 (5) { 
     ["__CLASS__"]=> string(31) "Symfony\Component\Form\FormView" 
     ["vars"]=> array(25) { 
         ["value"]=> string(38) "Save\AdminBundle\Entity\SystemSettings" 
         ["attr"]=> string(8) "Array(0)" 
         ["form"]=> string(31) "Symfony\Component\Form\FormView" 
         ["id"]=> string(14) "systemsettings" 
         ["name"]=> string(14) "systemsettings" 
         ["full_name"]=> string(14) "systemsettings" 
         ["disabled"]=> bool(false) 
         ["label"]=> NULL 
         ["multipart"]=> bool(false) 
         ["block_prefixes"]=> string(8) "Array(3)" 
         ["unique_block_prefix"]=> string(15) "_systemsettings" 
         ["translation_domain"]=> string(8) "messages" 
         ["cache_key"]=> string(30) "_systemsettings_systemsettings" 
         ["read_only"]=> bool(false) 
         ["errors"]=> string(8) "Array(0)" 
         ["valid"]=> bool(true) 
         ["data"]=> string(38) "Save\AdminBundle\Entity\SystemSettings" 
         ["required"]=> bool(true) 
         ["max_length"]=> NULL 
         ["pattern"]=> NULL 
         ["size"]=> NULL 
         ["label_attr"]=> string(8) "Array(0)" 
         ["compound"]=> bool(true) 
         ["method"]=> string(4) "POST" 
         ["action"]=> string(0) "" 
      } 
      ["parent"]=> NULL 
      ["children"]=> array(15) { 
          ["clientRegistrationEmail"]=> string(31) "Symfony\Component\Form\FormView" 
          ["adminRegistrationEmail"]=> string(31) "Symfony\Component\Form\FormView" 
          ["userRegistrationEmail"]=> string(31) "Symfony\Component\Form\FormView" 
          ["passwordResetStartEmail"]=> string(31) "Symfony\Component\Form\FormView" 
          ["passwordResetEndEmail"]=> string(31) "Symfony\Component\Form\FormView" 
          ["loginUrl"]=> string(31) "Symfony\Component\Form\FormView" 
          ["resetUrl"]=> string(31) "Symfony\Component\Form\FormView" 
          ["clientRegistrationEmailAdminCopy"]=> string(31) "Symfony\Component\Form\FormView" 
          ["clientLoginUrl"]=> string(31) "Symfony\Component\Form\FormView"   
          ["adminLoginUrl"]=> string(31) "Symfony\Component\Form\FormView" 
          ["userRegistrationByAdminEmail"]=> string(31) "Symfony\Component\Form\FormView" 
          ["userRegistrationByClientEmail"]=> string(31) "Symfony\Component\Form\FormView"  
          ["loginResetByAdminEmail"]=> string(31) "Symfony\Component\Form\FormView" 
          ["updateSettings"]=> string(31) "Symfony\Component\Form\FormView" 
          ["_token"]=> string(31) "Symfony\Component\Form\FormView" } 
          ["rendered"]=> bool(false) 
       }

如果仔细查看这两个打印输出,您可以看到adminFromEmailadminFromName在转储$form变量时出现,但在调用$form->createView()之后却没有出现我不知道为什么。

我包含与此相关的文件的相关区域。

表格类:

class SystemSettingType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        ...
        ->add('adminFromEmail', 'text') 
        ->add('adminFromName', 'text')
        ...
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver) 
    {
        $resolver->setDefaults(array(
            'data_class' => 'Save\AdminBuildle\Entity\SystemSettings'
        ));
    }

    public function getName()
    {
        return 'systemsettings';
    }
}

实体:

class SystemSettings
{
    ...

    /**
     * @var string
     * 
     * @ORM\Column(name="system_from_email", type="string", length=100)
     */
     private $adminFromEmail;

    /**
     * @var string
     * 
     * @ORM\Column(name="system_from_name", type="string", length=100)
     */
     private $adminFromName;

     ...
 }

注意: getter和setter都在Entity中正确形成(通过运行doctrine:generate:entities创建)

控制器:

public function listSystemSettingsAction(Request $request)
{
    $em = $this->getDoctrine()->getManager();
    $system_setting = $em->getRepository('SaveAdminBundle:SystemSettings')->find(1);
    if(is_null($system_setting)){
        $system_setting = new SystemSettings();
    }
    $form = $this->createForm(new SystemSettingsType(), $system_setting);

    $form->handleRequest($request);

    if($form->isValid() ){
        $em->persist($system_setting);
        $em->flush();
    }

    return $this->render('SaveAdminBundle:System:systemsettings.html.twig', array('form'=>$form->createView()));
}  

树枝模板:

...
{% block body %}
<div class="tabbed-box">
    <h1>System Settings</h1>
       <div class="tabbed-area">
           <ul class="tabs">
               <li><a href="#" class="tab active" id="tab_1" title="content_1">System Settings</a></li>
           </ul>
           <div class="content" id="content_1">
               {{ form_start(form) }}
               <fieldset>
                   <legend>System Settings</legend>
                   <ul class="form-fields">

                       ...

                       <li>
                           {{ form_row(form.adminFromEmail) }}                       
                       </li>
                       <li>
                           {{ form_row(form.adminFromName) }}
                       </li>

                       ...

                 </ul>
             </fieldset>
             <div class="button-bar">
                 {{ form_widget(form.updateSettings) }}
             </div>
         {{ form_end(form) }}
     </div>
</div>
...

如果有人知道为什么表单字段不会传递到表单视图,请告诉我。

1 个答案:

答案 0 :(得分:0)

对我来说,看起来像是一个明确的缓存问题,或者你可能缺少实体的use语句。代码对我来说是正确的。