0

I'am working on symfony REST API and currently i stopped at PUT request to edit data. I don't know where either search for bug.

My code look like this.

Controller.php

    /**
 * User update
 *
 * @Rest\Put("/users/{id}", name="user_update")
 * @Rest\View()
 * @param  Request $request
 * @param          $id
 * @param Users    $restUser
 *
 *
 *
 * @return View
 */
public function updateUserAction( Request $request, $id, Users $restUser )
{
    $results = [];

    $user = $this->em->getRepository( User::class )
                     ->find( $id )
    ;
    /**
     * Data to update user
     */
    if ( ! $user )
    {
        $results[ 'message' ] = sprintf( 'No users found with {%s} ID', $id );
        $results[ 'status' ] = 'failure';

        return View::create( $results );
    }
    $results[ 'beforeUpdate' ] = $user;
    $newData = $request->request->all();

    /**
     * Try to update
     */
    try
    {
        $form = $this->createForm( UserType::class, $user );
        $form->submit( $newData );
        if ( $form->isValid() )
        {
            $results[ 'status' ] = 'success';
            $results[ 'message' ] = 'Pomyślnie zaaktualizowsano użytkownika';

            $this->em->persist( $user );
            $this->em->flush();
        }
        $results[ 'errors' ] = $form->getErrors( FALSE, TRUE );
    }
    catch( \Exception $exception )
    {
        $results[ 'status' ] = 'failure';
        $results[ 'message' ] = 'Wystąpił błąd przy aktualizacji';
        $results[ 'message_exception' ] = $exception->getMessage();

    }
    /**
     * Get BODY of request
     */
    $results[ 'sentData' ] = $newData;


    return View::create($results);
}

And UserFormType.php

class UserType extends AbstractType implements FormTypeInterface{

public function configureOptions( OptionsResolver $resolver )
{
    $resolver->setDefaults( [
        'data_class'         => User::class,
        'allow_extra_fields' => TRUE,
        'csrf_protection'    => FALSE,
    ] );
}

}

Basic question is that I do everything ok? Im new to Symfony and try my best everytime i try to do something i think i get each available error.

6
  • a form does only update fields that are defined in it's buildForm method. Also your blatant misuse of the form suggests, you only want the validator (which you could dependency-inject). the "allow_extra_fields" only avoids the error message when a form gets data for different fields than it expects. Commented Mar 21, 2019 at 23:53
  • the related question on the right might give you some hints on how to do this, also consult the tutorials on forms and symfony.com/doc/current/form/direct_submit.html for the final submit Commented Mar 21, 2019 at 23:58
  • @Jakumi, thanks for reply. I added to UserType.php method you mentioned ( here pastebin.com/zkJHteCp ). But now i noticed that data from request is null when i past them to $form->submit($newData). Any ideas what is wrong? $newData is from $request->request->all() Commented Mar 22, 2019 at 1:03
  • have you checked what is found in $newData? because I think that's probably the reason. I'm not sure what the @Rest/Put Annotation does. If it doesn't contribute to this, then you're probably on the wrong track: apparently "PUT" data isn't read into the Request as expected, but instead must be parsed by yourself, see: stackoverflow.com/questions/36858279/… and codereviewvideos.com/course/… (the second is for json though). Commented Mar 22, 2019 at 7:03
  • @Jakumi I figured out that i must send variables with exact naming system like i declared in formbuilder, for example i do form->add('firstName'), and in frontend i must sent data with camelCase naming sytem instead of snakecase (like i recieve from backend). I changed to $form->add('first_name') and it works. Don't know reason why it is done that. Thanks for help man Commented Mar 22, 2019 at 22:51

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.