Symfony3控制器必须返回响应(给定null)

时间:2016-03-23 14:07:28

标签: php symfony

我在这里遇到Symfony3这个问题。我是新手,所以我不知道在哪里调试。

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,

        ));
    }
}

起初我遇到了这个错误: 表单的视图数据应该是Symfony \ Component \ HttpFoundation \ File \ File类的实例,但是是(n)资源。您可以通过将“data_class”选项设置为null或添加将(n)资源转换为Symfony \ Component \ HttpFoundation \ File \ File的实例的视图转换器来避免此错误。

然后我在这里阅读了一些帖子,并添加了'data_class' => null,,其中FileType::class已添加到表单中。

从那时起,我收到此错误: 控制器必须返回响应(给定null)。您是否忘记在控制器中的某处添加返回语句?

这是我将表单呈现为的文件: 的 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 %}

想法?

5 个答案:

答案 0 :(得分:1)

如果表单未提交怎么办?你不会返回任何东西,就像错误一样。

您可以将return $this->render移到下一个}下方的末尾,以便无论是否提交表单都会调用此字段。

或做一些自定义的事情:

if ($form->isSubmitted() && $form->isValid()) {
    ....
} else {
    // Return something if the form is not submitted
}

答案 1 :(得分:1)

问题是这个条件if ($form->isSubmitted() && $form->isValid()) {导致错误,因此没有任何东西返回。

希望这对你有所帮助。

答案 2 :(得分:0)

只有在您的表单提交并且有效时才会返回您的树枝模板...

答案 3 :(得分:0)

您将return语句放在if ($form->isSubmitted() && $form->isValid()) { }条件中,因此方法返回null。

答案 4 :(得分:0)

{{1}}