dongli1920 2014-01-14 21:22
浏览 45
已采纳

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

I am hoping someone can help me debug an issue I am having with form fields not being passed to the form view in Symfony 2.3.

I have a form that I am working on that I built originally a few months ago. The form worked fine until I decided to add a couple of fields to it. I added the fields to the Entity and updated the database schema without an issue. I added the fields to the form type class and then added them to the twig template.

That is where the issue arose, I keep getting an error saying that

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

So I started doing some digging. I did a dump() of the form and got the following response back:

$form variable dump

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) } 

However, when I did a dump of the view after calling $form->createView(), I got this:

$view variable dump

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) 
       }

If you look closely at the two printouts you can see that adminFromEmail and adminFromName both show up when dumping the $form variable but not after calling $form->createView() and I'm not sure why.

I am including the relevant areas of the files associated with this.

Form Class:

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';
    }
}

The Entity:

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;

     ...
 }

Note: The getters and setters are both properly formed in the Entity ( created by running doctrine:generate:entities )

The Controller:

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()));
}  

The Twig Template:

...
{% 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>
...

If anyone has an idea as to why the form fields would not be passed on to the form view, please let me know.

  • 写回答

1条回答 默认 最新

  • dqjcb132285 2014-01-14 21:52
    关注

    to me that looks like either a clear cache issue or you may be missing a use statement for your entity. The code looks correct to me.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示