0

I have this issue here with Symfony3. I am pretty new at this so I have no idea where to debug.

UserController.php

public function userEdit($id, Request $request)
{
    $user = $this->getDoctrine()
        ->getRepository('AppBundle:Users')
        ->find($id);

    $user->setEmail($user->getEmail());
    $user->setPassword($user->getPassword());
    $user->setPhone($user->getPhone());
    $user->setType($user->getType());
    $user->setName($user->getName());
    $user->setFeedback($user->getFeedback());
    $user->setPicture($user->getPicture());
    $user->setRating($user->getRating());
    $user->setInfo($user->getInfo());
    $user->setDatecreated($user->getDatecreated());

    $form = $this->createFormBuilder($user)
        ->add('email', EmailType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
        ->add('password', RepeatedType::class, array(
            'type' => PasswordType::class,
            'invalid_message' => 'The password fields must match.',
            'options' => array('attr' => array('class' => 'password-field form-control', 'style' => 'margin-bottom:15px')),
            'required' => true,
            'first_options' => array('label' => 'Password'),
            'second_options' => array('label' => 'Repeat Password'),
        ))
        ->add('phone', TextType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
        ->add('type', ChoiceType::class, array('choices' => array('Client' => 'Client', 'Builder' => 'Builder'), 'attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
        ->add('name', TextType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
        ->add('picture', FileType::class, array('data_class' => null,'attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
        ->add('info', TextareaType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
        ->add('save', SubmitType::class, array('label' => 'Register', 'attr' => array('class' => 'btn btn-primary', 'style' => 'margin-bottom:15px')))
        ->getForm();

    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        //Get Data
        $email = $form['email']->getData();
        $password = $form['password']->getData();
        $phone = $form['phone']->getData();
        $type = $form['type']->getData();
        $name = $form['name']->getData();
        $picture = $form['picture']->getData();
        $info = $form['info']->getData();

        $now = new\DateTime('now');
        $em = $this->getDoctrine()->getManager();
        $user = $em->getRepository('AppBundle:Users')->find($id);


        $user->setEmail($email);
        $user->setPassword($password);
        $user->setPhone($phone);
        $user->setType($type);
        $user->setName($name);
        $user->setFeedback($user->getFeedback());
        $user->setPicture($picture);
        $user->setRating($user->getRating());
        $user->setInfo($info);
        $user->setDatecreated($now);
        $images = base64_encode(stream_get_contents($user->getPicture()));

        $em->flush();
        $this->addFlash(
            'notice',
            'User Updated'
        );

        return $this->render('home/useredit.html.twig', array(
            'user' => $user,
            'form' => $form->createView(),
            'images' => $images,

        ));
    }
}

At first i was getting this error : The form's view data is expected to be an instance of class Symfony\Component\HttpFoundation\File\File, but is a(n) resource. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms a(n) resource to an instance of Symfony\Component\HttpFoundation\File\File.

Then i read some posts here and I added 'data_class' => null, where the FileType::class is added to the form.

And from then on I get this error: The controller must return a response (null given). Did you forget to add a return statement somewhere in your controller?

This is the file that i`m rendering the form to: useredit.html.twig

{% extends 'base.html.twig' %}
{% block body %}
<h2 class="page-header">EDIT USER {{ user.name }}</h2>
{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}
{% endblock %}

Ideas?

Georgi Georgiev
  • 117
  • 1
  • 1
  • 13

5 Answers5

1

What if the form is not submitted? You don't return anything, just as the error states.

You could move your return $this->render at the end below the next } so that this is called whether a form is submitted or not.

Or do something custom:

if ($form->isSubmitted() && $form->isValid()) {
    ....
} else {
    // Return something if the form is not submitted
}
Egg
  • 1,782
  • 1
  • 12
  • 28
  • Okay, but what if the form is not valid? I mean, I don't want to submit it if it's not valid. All I`m trying to figure out is why is this form not valid/ not submitted. Your answer solves my problem for now. – Georgi Georgiev Mar 23 '16 at 14:19
  • If the for is submitted and value, PHP will process what's inside the `if` statement. If it's not submitted (or not valid) you don't do anything (there is no `else` statement). – Egg Mar 23 '16 at 14:23
  • There is not need for else, just return something after the if finished. – abdiel Mar 23 '16 at 14:37
  • I understand, now after I added the else statement with the same return, It's working fine. I am just wondering why is the form not valid or not submitted. – Georgi Georgiev Mar 23 '16 at 14:37
  • Maybe because you are calling the function as GET for retrieving the empty form, or maybe because you are submitting wrong data and some validation error are in place. – abdiel Mar 23 '16 at 14:41
  • Since i`m here, I'm not going to post another question about it. I managed to display blob images from database MySQL following the answer from this topic http://stackoverflow.com/questions/28294077/display-image-stored-in-blob-database-in-symfony Now, after I fixed the issue that I originally posted, the new images that i put in the DB or the ones that I am trying to EDIT are not displayed anymore. Any idea why? – Georgi Georgiev Mar 23 '16 at 14:49
1

The problem is that this condition if ($form->isSubmitted() && $form->isValid()) { is resulting in false and for that reason nothing is returning.

Hope this help you.

abdiel
  • 2,078
  • 16
  • 24
0

You return your twig template only if your form is submitted and valid...

miltone
  • 4,416
  • 11
  • 42
  • 76
0

You put your return statement inside if ($form->isSubmitted() && $form->isValid()) { } condition, so method returns null.

overgapo
  • 457
  • 4
  • 10
0
    return $helpers->json(array(
            "status" => "error",
            "data" => "Send json with post !!"
        ));
Antonio
  • 1
  • 4