Symfony form, updating entity results in wrong argument type












1














I have a form and I can create a new record in my database just fine.



Now I would like to update this record with the same form so I am loading the form with my entity from the database.



In this form there is a field called modelBuild and it is nullable and at the moment empty so when I load my form there is nothing in the field.



Until there this is straightforward



But now I try to update the database record so I send the form but the controller crashes at $form->handleRequest($request); saying:



Expected argument of type "string", "NULL" given at property path "modelBuild".



I don't understand because this field has always been null so why is it complaining now ?



if I put something in the field then the form submits fine and the database is updated



ENTITY FIELD:



/**
* @var string|null
*
* @ORMColumn(name="model_build", type="string", length=100, nullable=true)
*/
private $modelBuild;

public function getModelBuild(): ?string
{
return $this->modelBuild;
}

public function setModelBuild(string $modelBuild): self
{
$this->modelBuild = $modelBuild;

return $this;
}


TYPE



->add('modelBuild', TextType::class, array(
'label' => 'model build',
))


CONTROLLER:



function formHandle(Request $request) {

if ($this->usermgmt->isLogged()) {

$update = json_decode($request->request->get('update'));

$uid = $update[1];

// get user
$user = $this->usermgmt->getUser();

$itemEntity = $em->getRepository(Item::class)->findOneBy([
'uid' => $uid,
'user' => $user
]);


// create form
$form = $this->createForm(NewItemType::class, $itemEntity);

// handle
$form->handleRequest($request);

// rest of controller....
}









share|improve this question
























  • can you post your form please?
    – hoover_D
    Dec 27 '18 at 15:50










  • added the type. at the moment it looks like adding null to ``@var string|null seems to have solved the problem but that still looks freaky; I have no idea why this happened
    – Sam
    Dec 27 '18 at 17:09










  • I guess you dont need to set this null, nullable true it works only
    – hoover_D
    Dec 27 '18 at 17:14










  • yes that is what I'm thinking too and that is what I did to create the record in the first place but for some reason it seems to need this to update it for this particular field... the form sends an empty string which should be interpreted as null
    – Sam
    Dec 27 '18 at 17:26












  • Can we see modelBuild setter in the entity?
    – DonCallisto
    Dec 28 '18 at 8:29
















1














I have a form and I can create a new record in my database just fine.



Now I would like to update this record with the same form so I am loading the form with my entity from the database.



In this form there is a field called modelBuild and it is nullable and at the moment empty so when I load my form there is nothing in the field.



Until there this is straightforward



But now I try to update the database record so I send the form but the controller crashes at $form->handleRequest($request); saying:



Expected argument of type "string", "NULL" given at property path "modelBuild".



I don't understand because this field has always been null so why is it complaining now ?



if I put something in the field then the form submits fine and the database is updated



ENTITY FIELD:



/**
* @var string|null
*
* @ORMColumn(name="model_build", type="string", length=100, nullable=true)
*/
private $modelBuild;

public function getModelBuild(): ?string
{
return $this->modelBuild;
}

public function setModelBuild(string $modelBuild): self
{
$this->modelBuild = $modelBuild;

return $this;
}


TYPE



->add('modelBuild', TextType::class, array(
'label' => 'model build',
))


CONTROLLER:



function formHandle(Request $request) {

if ($this->usermgmt->isLogged()) {

$update = json_decode($request->request->get('update'));

$uid = $update[1];

// get user
$user = $this->usermgmt->getUser();

$itemEntity = $em->getRepository(Item::class)->findOneBy([
'uid' => $uid,
'user' => $user
]);


// create form
$form = $this->createForm(NewItemType::class, $itemEntity);

// handle
$form->handleRequest($request);

// rest of controller....
}









share|improve this question
























  • can you post your form please?
    – hoover_D
    Dec 27 '18 at 15:50










  • added the type. at the moment it looks like adding null to ``@var string|null seems to have solved the problem but that still looks freaky; I have no idea why this happened
    – Sam
    Dec 27 '18 at 17:09










  • I guess you dont need to set this null, nullable true it works only
    – hoover_D
    Dec 27 '18 at 17:14










  • yes that is what I'm thinking too and that is what I did to create the record in the first place but for some reason it seems to need this to update it for this particular field... the form sends an empty string which should be interpreted as null
    – Sam
    Dec 27 '18 at 17:26












  • Can we see modelBuild setter in the entity?
    – DonCallisto
    Dec 28 '18 at 8:29














1












1








1







I have a form and I can create a new record in my database just fine.



Now I would like to update this record with the same form so I am loading the form with my entity from the database.



In this form there is a field called modelBuild and it is nullable and at the moment empty so when I load my form there is nothing in the field.



Until there this is straightforward



But now I try to update the database record so I send the form but the controller crashes at $form->handleRequest($request); saying:



Expected argument of type "string", "NULL" given at property path "modelBuild".



I don't understand because this field has always been null so why is it complaining now ?



if I put something in the field then the form submits fine and the database is updated



ENTITY FIELD:



/**
* @var string|null
*
* @ORMColumn(name="model_build", type="string", length=100, nullable=true)
*/
private $modelBuild;

public function getModelBuild(): ?string
{
return $this->modelBuild;
}

public function setModelBuild(string $modelBuild): self
{
$this->modelBuild = $modelBuild;

return $this;
}


TYPE



->add('modelBuild', TextType::class, array(
'label' => 'model build',
))


CONTROLLER:



function formHandle(Request $request) {

if ($this->usermgmt->isLogged()) {

$update = json_decode($request->request->get('update'));

$uid = $update[1];

// get user
$user = $this->usermgmt->getUser();

$itemEntity = $em->getRepository(Item::class)->findOneBy([
'uid' => $uid,
'user' => $user
]);


// create form
$form = $this->createForm(NewItemType::class, $itemEntity);

// handle
$form->handleRequest($request);

// rest of controller....
}









share|improve this question















I have a form and I can create a new record in my database just fine.



Now I would like to update this record with the same form so I am loading the form with my entity from the database.



In this form there is a field called modelBuild and it is nullable and at the moment empty so when I load my form there is nothing in the field.



Until there this is straightforward



But now I try to update the database record so I send the form but the controller crashes at $form->handleRequest($request); saying:



Expected argument of type "string", "NULL" given at property path "modelBuild".



I don't understand because this field has always been null so why is it complaining now ?



if I put something in the field then the form submits fine and the database is updated



ENTITY FIELD:



/**
* @var string|null
*
* @ORMColumn(name="model_build", type="string", length=100, nullable=true)
*/
private $modelBuild;

public function getModelBuild(): ?string
{
return $this->modelBuild;
}

public function setModelBuild(string $modelBuild): self
{
$this->modelBuild = $modelBuild;

return $this;
}


TYPE



->add('modelBuild', TextType::class, array(
'label' => 'model build',
))


CONTROLLER:



function formHandle(Request $request) {

if ($this->usermgmt->isLogged()) {

$update = json_decode($request->request->get('update'));

$uid = $update[1];

// get user
$user = $this->usermgmt->getUser();

$itemEntity = $em->getRepository(Item::class)->findOneBy([
'uid' => $uid,
'user' => $user
]);


// create form
$form = $this->createForm(NewItemType::class, $itemEntity);

// handle
$form->handleRequest($request);

// rest of controller....
}






symfony symfony-forms






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 28 '18 at 8:34

























asked Dec 27 '18 at 15:14









Sam

142111




142111












  • can you post your form please?
    – hoover_D
    Dec 27 '18 at 15:50










  • added the type. at the moment it looks like adding null to ``@var string|null seems to have solved the problem but that still looks freaky; I have no idea why this happened
    – Sam
    Dec 27 '18 at 17:09










  • I guess you dont need to set this null, nullable true it works only
    – hoover_D
    Dec 27 '18 at 17:14










  • yes that is what I'm thinking too and that is what I did to create the record in the first place but for some reason it seems to need this to update it for this particular field... the form sends an empty string which should be interpreted as null
    – Sam
    Dec 27 '18 at 17:26












  • Can we see modelBuild setter in the entity?
    – DonCallisto
    Dec 28 '18 at 8:29


















  • can you post your form please?
    – hoover_D
    Dec 27 '18 at 15:50










  • added the type. at the moment it looks like adding null to ``@var string|null seems to have solved the problem but that still looks freaky; I have no idea why this happened
    – Sam
    Dec 27 '18 at 17:09










  • I guess you dont need to set this null, nullable true it works only
    – hoover_D
    Dec 27 '18 at 17:14










  • yes that is what I'm thinking too and that is what I did to create the record in the first place but for some reason it seems to need this to update it for this particular field... the form sends an empty string which should be interpreted as null
    – Sam
    Dec 27 '18 at 17:26












  • Can we see modelBuild setter in the entity?
    – DonCallisto
    Dec 28 '18 at 8:29
















can you post your form please?
– hoover_D
Dec 27 '18 at 15:50




can you post your form please?
– hoover_D
Dec 27 '18 at 15:50












added the type. at the moment it looks like adding null to ``@var string|null seems to have solved the problem but that still looks freaky; I have no idea why this happened
– Sam
Dec 27 '18 at 17:09




added the type. at the moment it looks like adding null to ``@var string|null seems to have solved the problem but that still looks freaky; I have no idea why this happened
– Sam
Dec 27 '18 at 17:09












I guess you dont need to set this null, nullable true it works only
– hoover_D
Dec 27 '18 at 17:14




I guess you dont need to set this null, nullable true it works only
– hoover_D
Dec 27 '18 at 17:14












yes that is what I'm thinking too and that is what I did to create the record in the first place but for some reason it seems to need this to update it for this particular field... the form sends an empty string which should be interpreted as null
– Sam
Dec 27 '18 at 17:26






yes that is what I'm thinking too and that is what I did to create the record in the first place but for some reason it seems to need this to update it for this particular field... the form sends an empty string which should be interpreted as null
– Sam
Dec 27 '18 at 17:26














Can we see modelBuild setter in the entity?
– DonCallisto
Dec 28 '18 at 8:29




Can we see modelBuild setter in the entity?
– DonCallisto
Dec 28 '18 at 8:29












1 Answer
1






active

oldest

votes


















0














The setter expects only a string and not accepting a null value. Change it to ?string






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53947188%2fsymfony-form-updating-entity-results-in-wrong-argument-type%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    The setter expects only a string and not accepting a null value. Change it to ?string






    share|improve this answer


























      0














      The setter expects only a string and not accepting a null value. Change it to ?string






      share|improve this answer
























        0












        0








        0






        The setter expects only a string and not accepting a null value. Change it to ?string






        share|improve this answer












        The setter expects only a string and not accepting a null value. Change it to ?string







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 28 '18 at 8:43









        Alexander Dimitrov

        1467




        1467






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53947188%2fsymfony-form-updating-entity-results-in-wrong-argument-type%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas