2016-11-04 64 views
-1

我正在使用symfony2.Im使用FOSUserBundle,所以我想重写编辑用户的FormType以将名称添加到生成器。所以我应该怎么做呢?如何覆盖ProfileFormType来编辑用户? symfony2

什么我已经做了:

ProfileFormType:

<?php 

namespace UserBundle\Form\Type; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilder; 
use FOS\UserBundle\Form\Type\ProfileFormType as BaseType; 

class ProfileFormType extends BaseType { 

    public function buildForm(FormBuilderInterface $builder, array $options) { 
     parent::buildForm($builder, $options); 
     // add your custom field 
     $builder->add('name') 
       ->add('roles', 'collection', array(
        'type' => 'choice', 
        'options' => array(
         'choices' => array(
          'ROLE_ADMIN' => 'Admin')))) 
       ->add('image', new ImageType()) 

     ; 
    } 

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

} 

services.yml

user.edit.form.type: 
    class: UserBundle\Form\Type\ProfileFormType 
    arguments: [%fos_user.model.user.class%] 
    tags: 
     - { name: form.type, alias: user_edit_profile } 

edit.html.twig

{% extends "UserBundle::layout.html.twig" %} 


{% block body %} 
    <center> <h1> Modification de profile </h1> </center> 
    <aside class="col-sm-3"> 
     <div class="panel panel-default"> 
      <div class="panel-heading">Modification</div> 
      <div class="panel-body"> 
       Veuillez remplir les champs 
      </div> 
     </div> 
    </aside> 

    <!--timeline--> 
    <section class="timeline col-sm-9"> 
     <!--post Timeline--> 
     <div class="thumbnail thumbnail-post"> 
      <!--caption--> 
      <div class="caption"> 


       <form action="{{ path('fos_user_registration_register') }}" {{ form_enctype(form) }} method="POST" class="form-horizontal"> 


        <div class="form-group"> 

         {{ form_errors(form.name) }} 
         <div class="col-sm-9"> 
          Name {{ form_widget(form.name, { 'attr': {'class': 'form-control', 'placeholder': 'form.name'|trans } }) }} 
         </div> 

        </div> 



        <div class="form-group"> 

         {{ form_errors(form.email) }} 
         <div class="col-sm-9"> 
          Email {{ form_widget(form.email, { 'attr': {'class': 'form-control', 'placeholder': 'form.email'|trans } }) }} 
         </div> 

        </div> 
        <div class="form-group"> 

         {{ form_errors(form.username) }} 
         <div class="col-sm-9"> 
          Pseudo {{ form_widget(form.username, { 'attr': {'class': 'form-control', 'placeholder': 'form.username'|trans } }) }} 

         </div> 

        </div> 

        <div class="form-group"> 

         {{ form_errors(form.current_password) }} 
         <div class="col-sm-9"> 
          Mot de passe actuelle {{ form_widget(form.current_password, { 'attr': {'class': 'form-control', 'placeholder': 'form.current_password'|trans } }) }} 

         </div> 

        </div> 


        </br> 



        {{ form_rest(form) }} 


        <div class="form-group"> 
         <div class="col-md-4 col-sm-4 col-xs-12 col-md-offset-3"> 
          <input class="btn btn-default submit" type="submit" value="{{ 'registration.submit'|trans }}"> 
         </div> 
        </div> 
       </form> 
      </div> <!--#caption--> 


      <!--#post timeline--> 
     </div> 
     <!--#timeline--> 
    </section> 





{% endblock %} 

{% block js %} 

    <script> 
     $(document).ready(function() { 

      $('#roles').hide(); 
     }); 


    </script> 


{% endblock %} 

我得到这个错误:

Neither the property "name" nor one of the methods "name()", "getname()"/"isname()" or "__call()" exist and have public access in class "Symfony\Component\Form\FormView" in FOSUserBundle:Profile:edit.html.twig at line 28.

我该如何解决这个问题。

回答

0
profile: 
     form: 
      type: fos_user_profile 

它需要,在FOS

0

更新你的服务文件:

user.edit.form.type: 
    class: UserBundle\Form\Type\ProfileFormType 
    arguments: [%fos_user.model.user.class%] 
    decorates: fos_user.profile.form.type 
    tags: 
     - { name: form.type, alias: user_edit_profile } 

fos_user.profile.form.type是意味着我们的服务的名称将被更换我们的服务,它的名字user.edit.form.type

添加use Symfony\Component\Form\FormBuilderInterface;

你有错误:

Neither the property "name" nor one of the methods "name()", "getname()"/"isname()" or "__call()" exist and have public access in class "Symfony\Component\Form\FormView" in FOSUserBundle:Profile:edit.html.twig at line 28. 

由于fos_user_user表没有名称字段。如果你想添加一个字段,而不是在表格中定位,你应该在实体生成getters和setter字段中添加这些字段,最后创建新的迁移并迁移。

+0

的config.yml我不明白 – Ld91

+2

我更新了答案。 –

+0

我有这个错误: 编译错误:声明UserBundle \ Form \ Type \ ProfileFormType :: buildForm()必须与Symfony \ Component \ Form \ FormTypeInterface :: buildForm(Symfony \ Component \ Form \ FormBuilderInterface $ builder, array $ options) – Ld91