在同一树枝文件中具有两种不同形式的问题

时间:2019-02-13 21:08:03

标签: symfony symfony-2.7

我在嵌入一个控制器时遇到了问题,该控制器创建了一个可以上传文件的表单。当在树枝文件的某些部分中呈现控制器时,出现此错误:

An exception has been thrown during the rendering of a template ("Expected argument of type "Symfony\Component\HttpFoundation\File\UploadedFile", "string" given").

这很奇怪,因为在同一树枝文件的其他部分中,给出的预期参数没有问题。问题似乎是同一树枝文件中的另一种形式与我的嵌入式控制器形式不能很好地配合。

似乎引起问题的部分:

<div id="payment_checkout_form">
    {% if cId and shippingRegionId %}
        {% set savedPath =path('cart_set_shipping', {'store_id': webstore.id, 'shippingRegion': shippingRegionId,'cId':cId}) %}
        {{ form_start(form, {'attr': {'id': 'form_checkout','data-url':savedPath}}) }}
    {% else %}
        {{ form_start(form, {'attr': {'id': 'form_checkout'}}) }}
    {% endif %}
{{ render(url('passport')) }}    

PassportType的相关部分:

public function buildForm(FormBuilderInterface $builder, array $options)
{

  $builder
    ->add('file', 'file', array('label' => false) , [
          'multiple' => true,
          'label' => '',
          'attr'  => [
              'accept' => 'image/*',
              'multiple' => 'multiple'
          ]
      ]
    )
    ->add('confirm', 'submit');
}

public function configureOptions(OptionsResolver $resolver){
    $resolver->setDefaults(array(
        'data_class' => 'AppBundle\Entity\Passport',
    ));
}

我的Passport实体的相关部分:

/**
 * @Assert\File(maxSize="6000000")
 */
private $file;

/**
 * Sets file.
 *
 * @param Symfony\Component\HttpFoundation\File\UploadedFile $file
 */
public function setFile(UploadedFile $file = null) {
    $this->file = $file;
}

我的Passport控制器的相关事件部分

/**
* @Route("/passport", name="passport")
*/
public function createPassportAction(Request $request)
{
    $request = $this->get('request_stack')->getMasterRequest();

    $passport = new Passport();
    $passport->setName('default');

    $form = $this->createForm(new PassportType(), $passport);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
      $files = $request->files->get('passportPhoto');

       if (!empty($files)) {
          $this->uploadFile($files);
       }
    }

    return $this->render('passport.html.twig', [
        'form' => $form->createView(),
        'isFormSubmitted' => $form->isSubmitted(),
        'passportImages' => $this->getDoctrine()->getRepository('AppBundle\Entity\Passport')->findAll(),
    ]);
}

{{ render(url('passport')) }} is the embedded controller that renders the file upload form. If I put the {{render(url('passport'))}}}在其他表单的form_start之上,一切正常。

0 个答案:

没有答案