doujiao3346 2017-03-24 08:55
浏览 95

不规则的Symfony异常:renderBlock()必须是FormView的一个实例,null给定

We have a strange error in our Symfony 2 project. We use Symfony forms, and we don't have any problems in our development environment, but do receive messages of an error in production. An error we can't replicate, and never had. Everything works fine when we test (both in prod & dev) but we do receive notifications of this error happening.

We are notified of the error:

"exception":"[object] (Symfony\Component\Debug\Exception\FatalThrowableError(code: 0): Type error: Argument 1 passed to Symfony\Component\Form\FormRenderer::renderBlock() must be an instance of Symfony\Component\Form\FormView, null given

Most important code (Stripped for compactness & readability )

Formtype:

class WishlistType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('rank', HiddenType::class)
            ->add('description', TextType::class)
            // Image is deprecated and will be removed.
            ->add('image');
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(
            [
                'data_class' => Item::class,
            ]
        );
    }
}

Controller:

public function showAction(Request $request, $url)
{
    /** @var Participant $participant */
    $participant = $this->get('participant_repository')->findOneByUrl($url);
    if ($participant === null) {
        throw new NotFoundHttpException();
    }

    $wishlistForm = $this->createForm(
        WishlistType::class,
        $participant,
        [
            'action' => $this->generateUrl(
                'wishlist_update',
                ['url' => $participant->getUrl()]
            ),
        ]
    );

    if (!$request->isXmlHttpRequest()) {
        return [
            'entry' => $participant,
            'wishlistForm' => $wishlistForm->createView(),
        ];
    }
}

Twig template:

    {{ form_start(wishlistForm, {'attr': {'id': 'add_item_to_wishlist_form'}}) }}
    {{ form_row(wishlistForm._token) }}
    <table>

          <!-- ADD SOMETHING TO YOUR WISHLIST -->
          <thead>
          <tr>
              <th>#</th>
              <th>{{ 'entry_show_valid.label.description'|trans }}</th>
              <th>
                  <button type="button" class="btn btn-mini btn-success add-new-entry">
                      {{ 'entry_show_valid.btn.add_wishlist'|trans }}
                  </button>
              </th>
          </tr>
          </thead>

          <!-- OVERVIEW OF ITEMS IN WISHLIST, AND POSSIBILITY TO REMOVE SINGLE ITEMS-->
          <tbody>
            {% for item in wishlistForm.wishlistItems %}
                <tr>
                    <td>{{ form_widget(item.rank) }}
                        <span>{{ item.rank.vars.value }}</span>
                    </td>
                    <td> 
                        {{ form_widget(item.description, {'attr': {'class': 'wishlistitem-description'} }) }}
                    </td>
                    <td>
                        <button type="submit" >
                            <span>{{ 'entry_show_valid.btn.update_item'|trans }}</span>
                        </button>
                        <button type="button">
                            <span>{{ 'entry_show_valid.btn.remove_item'|trans }}</span>
                        </button>
                    </td>
                </tr>
            {% endfor %}
        </tbody>
    </table>
    {{ form_end(wishlistForm, {'render_rest': false}) }}

Form handling is done over Ajax, but request is handled as regular symfony form ($wishlistForm->handleRequest($request); etc...).

  • 写回答

1条回答 默认 最新

  • dtyyrt4545 2017-03-24 09:35
    关注

    I don't understand how you can do this :

     {% for item in wishlistForm.wishlistItems %}
    

    because I don't see any 'wishlistItems' in your form declaration. For me, this code has to fail at this line...

    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?