doutangu4671 2015-05-03 03:36
浏览 10
已采纳

Symfony 2编辑操作不填充表单域

I'm attempting to create an edit action for a user address entity but whenever you load the page it doesn't seem to load the associated data. below is the code for my controller and view.

Profile Controller:

public function addressEditAction($id) {
        $user = $this->getUser();
        if (!is_object($user) || $user instanceof UserInterface) {
            throw new AccessDeniedException('This user does not have access to this section.');
        }

        if ($this->container->get('security.context')->isGranted('IS_AUTHENTICATED_FULLY')) {
            $em = $this->getDoctrine()->getEntityManager();
            $address = $em->getRepository('SCWDesignsBundle:UserAddresses')->getAddressById($user->getId(), $id);
            if (!$address) {
                throw new NotFoundHttpException(sprintf('Could not find address id: "%s"', $id));
            }

            $form = $this->createForm(new UserAddressesFormType(), $address);

            $request = $this->getRequest();
            if ($request->getMethod() == 'POST') {
                $form->bind($request);
                if ($form->isValid()) {
                    $utilites = new Utilities;
                    $address->setPhone($utilites->stripPhoneNumbers($address->getPhone()));
                    $em->persist($address);
                    $em->flush();

                    return $this->redirect($this->generateUrl('fos_user_profile_show'));
                }
            }
        }

        return $this->render('SCWDesignsBundle:Profile\Address:new.html.twig', array(
            'active_page' => 'profile',
            'form' => $form->createView()
        ));  
    }

View:

{% trans_default_domain 'SCWDesignsBundle' %}

{% block modal_block %}
<div class="item-box">
    <h2>Address Details</h2>

    {% include 'SCWDesignsBundle:Default:form_errors.html.twig' %}

    <form action="{{ path('scw_designs_address_add') }}" {{ form_enctype(form) }} method="POST" class="scw_designs_address_new clearfix">
        <div class="address-new">
            <div class="form-item">
                <div class="form-label">
                    <label for="first-name">{{ 'profile.first_name'|trans }}: <span class="required">*</span></label>
                </div>
                <div class="form-field">
                    {{ form_widget(form.first_name) }}
                </div>          
            </div>

            <div class="form-item">
                <div class="form-label">
                    <label for="last-name">{{ 'profile.last_name'|trans }}: <span class="required">*</span></label>
                </div>
                <div class="form-field">
                    {{ form_widget(form.last_name) }}
                </div>
            </div>

            <div class="form-item">
                <div class="form-label">
                    <label for="address">{{ 'profile.address'|trans }}: <span class="required">*</span></label>
                </div>
                <div class="form-field">
                    {{ form_widget(form.address_one) }}
                    {{ form_widget(form.address_two) }}
                </div>              
            </div>

            <div class="form-item">
                <div class="form-label">
                    <label for="city">{{ 'profile.city'|trans }}: <span class="required">*</span></label>
                </div>
                <div class="form-field">
                    {{ form_widget(form.city) }}
                </div>
            </div>

            <div class="form-item">
                <div class="form-label">
                    <label for="country">{{ 'profile.country'|trans }}: <span class="required">*</span></label>
                </div>
                <div class="form-field">
                    {{ form_widget(form.country) }}
                </div>
            </div>

            <div class="form-item">
                <div class="form-label">
                    <label for="state">{{ 'profile.state'|trans }}: <span class="required">*</span></label>
                </div>
                <div class="form-field">
                    {{ form_widget(form.state) }}
                </div>
            </div>

            <div class="form-item">
                <div class="form-label">
                    <label for="zipcode">{{ 'profile.zipcode'|trans }}: <span class="required">*</span></label>
                </div>
                <div class="form-field">
                    {{ form_widget(form.zipcode, { 'attr': { 'class': 'zipcodeUS' }}) }}
                </div>
            </div>

            <div class="form-item">
                <div class="form-label">
                    <label for="phone">{{ 'profile.phone'|trans }}: <span class="required">*</span></label>
                </div>
                <div class="form-field">
                    {{ form_widget(form.phone, { 'attr': { 'class': 'phoneUS' }}) }}
                </div>
            </div>
        </div>

        <div class="modal-button-wrapper">
            {{ form_widget(form._token) }}
            <button type="submit" class="btn-submit primary-button">Save Changes</button>
        </div>
    </form>
</div>
{% endblock %}

In the profilecontroller you can see me call a method from the entities respository, here's the var_dump of the array $address:

$address:

array (size=1)
  0 => 
  array (size=11)
    'id' => string '4' (length=1)
    'firstName' => string 'Bruce' (length=5)
    'lastName' => string 'Wayne' (length=5)
    'addressOne' => string '123 Gotham Lane' (length=15)
    'addressTwo' => null
    'city' => string 'Gotham' (length=6)
    'country' => string 'USA' (length=3)
    'zipcode' => string '51478' (length=5)
    'phone' => string '3344796239' (length=10)
    'code' => string 'NH' (length=2)
    'state' => string 'New Hampshire' (length=13)

So as you can see I've gotten the proper data from the database, I've followed the instructions on binding the data to the form object, it just simply doesn't persist into the view. I've tried multiple things but can't seem to get it working. Any advice/help would be greatly appreciated.

Thanks,

Shawn

  • 写回答

1条回答 默认 最新

  • duanou3868 2015-05-04 20:30
    关注

    You are getting from the repository a two dimensional $address array when the form is expecting a single object probably.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题