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?

5 Answers 5

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
}
Sign up to request clarification or add additional context in comments.

6 Comments

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.
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).
There is not need for else, just return something after the if finished.
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.
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.
|
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.

Comments

0

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

Comments

0

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

Comments

0
    return $helpers->json(array(
            "status" => "error",
            "data" => "Send json with post !!"
        ));

Comments

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.