2017-02-04 122 views
1

我正在使用Doctrine和FOSBundle 2.0開發Symfony3中的應用程序。嘗試將字段添加到註冊表單時,無法加載類型「app_user_registration」

我一直在試圖兩個字段添加到我的註冊欄的firstNamelastName的

I have found this tutorial on how do do precisely what I'm hoping,不幸的是,前幾個步驟(前處理)我知道我得到這個錯誤後:

Could not load type "app_user_registration"

我使用的是exacly複製從網站的代碼,唯一的區別是我的課看起來像這樣

<?php 
// src/AppBundle/Entity/User.php 

namespace AppBundle\Entity; 

use FOS\UserBundle\Model\User as BaseUser; 
use Doctrine\ORM\Mapping as ORM; 
use Symfony\Component\Validator\Constraints as Assert; 

/** 
* @ORM\Entity 
* @ORM\Table(name="fos_user") 
*/ 
class User extends BaseUser 
{ 
    /** 
    * @ORM\Id 
    * @ORM\Column(type="integer") 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $id; 

    /** @ORM\Column(type="integer") */ 
    protected $carma; 

    /** 
    * @ORM\Column(type="string", length=255) 
    * 
    * @Assert\NotBlank(message="Please enter your first name.", groups={"Registration", "Profile"}) 
    * @Assert\Length(
    *  min=3, 
    *  max=255, 
    *  minMessage="The name is too short.", 
    *  maxMessage="The name is too long.", 
    *  groups={"Registration", "Profile"} 
    *) 
    */ 
    protected $firstName; 

    /** 
    * @ORM\Column(type="string", length=255) 
    * 
    * @Assert\NotBlank(message="Please enter your first name.", groups={"Registration", "Profile"}) 
    * @Assert\Length(
    *  min=3, 
    *  max=255, 
    *  minMessage="The name is too short.", 
    *  maxMessage="The name is too long.", 
    *  groups={"Registration", "Profile"} 
    *) 
    */ 
    protected $lastName; 

相同效應發生時,有一個叫......「名」

變量

我花了最後幾個小時試圖弄清楚。 有什麼我曾嘗試:

其他一切表明我的輸出代碼爲答案的問題。

任何人都可以幫助一個可憐的靈魂嗎?

作爲補充,這裏是我的其他文件:

//config.yml 
/*...*/ 
fos_user: 
    db_driver: orm 
    firewall_name: main 
    user_class: AppBundle\Entity\User 
    registration: 
     form: 
      type: AppBundle\Form\RegistrationType 

//services.yml 
services: 
    app.form.registration: 
     class: AppBundle\Form\RegistrationType 
     tags: 
      - { name: form.type, alias: app_user_registration } 

//RegistraionType.php 
<?php 
// src/AppBundle/Form/RegistrationType.php 

namespace AppBundle\Form; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilderInterface; 

class RegistrationType extends AbstractType 
{ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder->add('firstName'); 
    } 

    public function getParent() 
    { 
     return 'fos_user_registration'; 
    } 

    public function getName() 
    { 
     return $this->getBlockPrefix(); 
    } 

    public function getBlockPrefix() 
    { 
     return 'app_user_registration'; 
    } 
} 
+0

此錯誤是不是在你的用戶的實體,它試圖使用你已經設置別名在您的應用程序來/ config/services.yml參考您的用戶註冊表單。 – pogeybait

+0

你是否也做過表單類型的東西,因爲這是錯誤來自哪裏。當你尋找其他例子時要小心。您發佈的第二個鏈接是針對舊版本的FOSUserBundle。事實上,你會發現大多數例子都過時了。 – Cerad

回答

0

我懷疑你一直在努力也跟着教程延長登記表。你最有可能應該在你的應用程序/配置/ services.yml文件中的以下條目:

app.form.registration: 
    class: AppBundle\Form\RegistrationType 
    tags: 
     - { name: form.type, alias: app_user_registration } 

然後你的src /的appbundle /表格/ RegistrationType.php您應該具有以下功能:

public function getBlockPrefix() 
{ 
    return 'app_user_registration'; 
} 

這很可能是錯誤來自getBlockPrefix()方法的地方。檢查你的services.yml並與我發佈的內容進行比較。我從我自己的應用程序抓住了這個工作正常。

+0

我已經意識到了這個問題,它實際上是在我的'getParent()'函數中,因爲它返回'app_user_registration'時應該返回'FOS \ UserBundle \ Form \ Type \ RegistrationFormType'。 – aln447

0

你應該使用這個tuorial代替(2.0.master): http://symfony.com/doc/master/bundles/FOSUserBundle/overriding_forms.html

您正在使用教程 「的1.3.x版本」。

對於Symfony3你必須讓這改變:

class RegistrationType extends AbstractType 
{ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder->add('name'); 
    } 

    public function getParent() 
    { 
     return 'FOS\UserBundle\Form\Type\RegistrationFormType'; 
    } 

    public function getBlockPrefix() 
    { 
     return 'app_user_registration'; 
    } 

}
 
services: 
    app.form.registration: 
     class: AppBundle\Form\RegistrationType 
     tags: 
      - { name: form.type, alias: app_user_registration } 
 
fos_user: 
    # ... 
    registration: 
     form: 
      type: AppBundle\Form\RegistrationType 
相關問題