Reputation: 567
I want override the form of RegistrationType of FOSUserBundle, but in the registration_content.html.twig template I got this error:
Neither the property "firstname" nor one of the methods "firstname()", "getfirstname()"/"isfirstname()"/"hasfirstname()" or "__call()" exist and have public access in class "Symfony\Component\Form\FormView".
This my RegistrationType Form:
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 'app_user_registration';
}
}
fos_user.yaml:
fos_user:
registration:
confirmation:
enabled: true
form:
name: app_user_registration
db_driver: orm
user_class: App\Entity\User
firewall_name: main
from_email:
address: [email protected]
sender_name: [email protected]
User Entity:
class User extends BaseUser {
//
/**
* @ORM\Column(type="string", length=50, nullable=true)
*/
protected $firstname;
}
registration_content.html.twig:
{{ form_start(form, {'method': 'post', 'action': path('fos_user_registration_register'), 'attr': {'class': 'fos_user_registration_register', 'id': 'form-sign-up'}}) }}
<div class="form-group form-group--float form-group--centered">
{{ form_widget(form.firstname, {'attr': {'class': 'form-control'}}) }}
<label>{{ 'firstname'|trans }}</label>
<i class="form-group__bar"></i>
</div>
{{ form_end(form) }}
In my config service I've already put this:
app.form.registration:
class: App\Form\RegistrationType
tags:
- { name: form.type, alias: app_user_registration }
Upvotes: 3
Views: 2965
Reputation: 31
In my case, I did the same thig as Robert Saylor but edited this:
public function getBlockPrefix()
{
return 'app_user_registration';
}
That worked fine to me !
Upvotes: 0
Reputation: 2446
Symfony 4.2.3 | friendsofsymfony/user-bundle v2.1.2
Overrides FOSUserBundle registration form with our own class and remove the 'username' field for the sake of an example.
From your symfony root directory:
mkdir ./src/Form/Type
cp ./vendor/friendsofsymfony/user-bundle/Form/Type/RegistrationFormType.php
./config/packages/fos_user.yaml
# fos_user.yaml
fos_user:
#... other configurations...
registration:
form:
type: App\Form\Type\RegistrationType
./src/Form/Type/RegistrationFormType.php
// Our specific example. edit this as you please...
<?php
namespace App\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use FOS\UserBundle\Form\Type\RegistrationFormType as BaseRegistrationFormType;
class RegistrationFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->remove('username');
}
public function getParent() {
return BaseRegistrationFormType::class;
}
}
Note: Since our example is only removing a single field, we remove the code we are not changing and add the following method to keep things simple:
public function getParent() {
return BaseRegistrationFormType::class;
}
getParent
method but it seems to inherit from the original RegistrationFormType.php
and simply add our changes made in the buildForm
method.Upvotes: 5
Reputation: 1369
Create a file named RegistrationFormType.php and add the following content:
namespace App\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use FOS\UserBundle\Form\Type\RegistrationFormType as BaseRegistrationFormType;
class RegistrationFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('first');
$builder
->add('last');
}
public function getParent()
{
return BaseRegistrationFormType::class;
}
}
The above is assuming you have added two new fields to your User entity and that you have ran your Doctrine migrations to add the new fields. Tip: set the fields as nullable so you do not the FOS command line tools.
registration:
form:
type: App\Form\RegistrationFormType
Add the following to services.yaml:
app.form.registration:
class: App\Form\RegistrationFormType
Refresh your form and your new fields will be in the form.
Upvotes: 2
Reputation: 376
I came across the same issue when following the official tutorial https://symfony.com/doc/current/bundles/FOSUserBundle/overriding_forms.html
I can't explain why we got this issue but it seems related to aliases. Anyway, I can't get it working in Symfony4 and in Symfony 3.4 neither !
I used the following code to circumvent the problem :
class RegistrationType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('firstname');
}
public function getParent() {
return 'FOS\UserBundle\Form\Type\RegistrationFormType';
}
}
And then add this line to your fos_user.yaml :
fos_user:
registration:
form:
type: App\Form\Type\RegistrationType
I hope this will help, until someone find a way to make it work as expected.
Upvotes: 3