commonUser
commonUser

Reputation: 629

Symfony form data are not formatted to camelCase

I'm trying to create a simple form with Symfony version 3.4.

I just created a Client type for the form, a Client controller to handle the creation of the client and a Client entity.

This is the code for the buildForm method:

        $builder->add('firstName', TextType::class)
            ->add('lastName', TextType::class)
            ->add('save', SubmitType::class, ['label' => 'Add Client'])
        ;

This is the Client entity code:

/**
 * @ORM\Entity(repositoryClass="App\Repository\ClientRepository")
 */
class Client
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $firstName;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $lastName;


    public function getId(): ?int
    {
        return $this->id;
    }

    public function getFirstName(): ?string
    {
        return $this->firstName;
    }

    public function setFirstName(string $firstName): self
    {
        $this->firstname = $firstName;

        return $this;
    }

the form is displayed correctly and when I submit it the data are passed this way by the client:

client[firstName]   Mario
client[lastName]    Rossi
client[save]    
client[_token]  3cqO2C63eLhKrcBdvSYgyT5qXwNBVL7T5fYvhGWRkYQ

but when I var_dump the object coming from the $form->getData() in the controller handler I get this array:

object(App\Entity\Client)#385 (5) { ["id":"App\Entity\Client":private]=> NULL ["firstName":"App\Entity\Client":private]=> NULL ["lastName":"App\Entity\Client":private]=> NULL ["firstname"]=> string(5) "Mario" ["lastname"]=> string(5) "Rossi" } 

As you can see the camelCased keys are NULL but the data are stored within non camelCased keys instead (firstname and lastname) and so I receive error from Doctrine/SQL complaining about columns cannot be NULL.

Any clue on this one? Thanks.

Upvotes: 3

Views: 983

Answers (1)

Jakumi
Jakumi

Reputation: 8374

in the chat discussion, the code was linked in full and I noticed a typo in both the setters:

public function setFirstName(string $firstName): self
{
    $this->firstname = $firstName; 
    //          ^ should be a capital N

    return $this
}

and the same for the setLastName

Upvotes: 2

Related Questions