Antoine D
Antoine D

Reputation: 128

Symfony 4.3: FormType add extra "Entity" type

I want to create a form to register a new Number so i've created a NumberType(code below). But on the top of the form it adds a field as bellow :

<input type="text" id="number" name="number" required="required" class="form-control">

NumberType.php

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('created_by', EmailType::class, [
            'label' => 'Email *',
            'attr' => array(
                'placeholder' => '[email protected]',
            )
        ])
        ->add('title', TextType::class, [
            'label' => 'Titre *'
        ])
        ->add('value', TextType::class, [
            'label' => 'Valeur *'
        ])
        ->add('source', UrlType::class, [
            'label' => 'Source *'
        ])
        ->add('description', TextareaType::class, [
            'label' => 'Description',
            'required' => false,
            'attr' => array(
                'placeholder' => 'Description du chiffre proposé'                    
            )
        ])
        ->add('tags')
        ->add('recaptcha', EWZRecaptchaType::class)
        ->add('send', SubmitType::class, ['label' => 'Envoyer'])
    ;
}

AdminController.php

$number = new Number();
// Form creation
$form = $this->createForm(NumberType::class, $number);

When I call the url associated with my function in my controller I had an error which was saying

Can't convert Number to string

So I've added a __tostring() function but I don't understand why it needs it.

Thanks,

Upvotes: 0

Views: 238

Answers (1)

Jakub Matczak
Jakub Matczak

Reputation: 15686

It looks like you use Symfony's NumberType instead your own.

Check the namespace at the top of AdminController file, which NumberType is being used.

If there's:

use Symfony\Component\Form\Extension\Core\Type\NumberType;

then it means you're using wrong class.

It should be something like:

use App\Some\Namespace\To\Your\NumberType;

Upvotes: 1

Related Questions