Symfony2 - Form正在尝试验证未使用的字段

时间:2012-04-13 08:29:09

标签: php symfony

所以,我有一个表单,允许用户更新他们的个人资料详细信息(姓名,电子邮件等)

使用指向用户实体的表单类型(UserType)正确显示表单。 UserType使用用户名,电子邮件构建表单,然后扩展到ProfileType - 另一种包含其真实姓名,地址等的表单类型。

正如我所提到的,表单正确显示在页面上。

现在,在我的validation.yml文件中,我指向User和Profile实体,而对于User,我正在定义3个验证规则,用于用户名,电子邮件和密码。对于Profile,我正在定义名称和公司名称的规则。所有规则都有NotBlank,电子邮件也有检查以确保它有效并且密码定义了最大和最小长度。

现在,最近添加了密码规则。以前,这个表格工作正常,没有问题。

添加密码规则,在提交表单时,它会尝试检查此验证规则,但页面上没有呈现密码字段,甚至在表单类型,UserType中也提到过。

当然,如果我不使用输入,它不应该根据验证规则检查它?

以下代码:

Validation.yml

DEMO\DemoBundle\Entity\User\Profile:
properties:
    name:
        - NotBlank: ~
    pubName:
        - NotBlank: ~

DEMO\DemoBundle\Entity\User\User:
    properties:
        username:
            - NotBlank:
                message: Username cannot be blank.
        email:
            - NotBlank: ~
            - Email:
                message: The email "{{ value }}" is not a valid email.
                checkMX: true
        password:
            - MaxLength: { limit: 20, message: "Your password must not exceed {{ limit }} characters." }
            - MinLength: { limit: 4, message: "Your password must have at least {{ limit }} characters." }
            - NotBlank: ~

UserType.php

namespace DEMO\DemoBundle\Form\Type\User;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\CallbackValidator;

use DEMO\DemoBundle\Form\Type\User\ProfileType;

class UserType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder->add('username');
        $builder->add('email');
        $builder->add('profile', new ProfileType());
    }

    public function getDefaultOptions(array $options)
    {
        return array(
            'data_class' => 'DEMO\DemoBundle\Entity\User\User',
        );
    }

    public function getName()
    {
        return 'user';
    }
}

ProfileType.php

namespace DEMO\DemoBundle\Form\Type\User;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\CallbackValidator;

class ProfileType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder->add('name');
        $builder->add('companyName', null, array('label' => 'Company Name'));
        $builder->add('address1', null, array('label' => 'Address 1'));
        $builder->add('address2', null, array('label' => 'Address 2'));
        $builder->add('city');
        $builder->add('county');
        $builder->add('postcode');
        $builder->add('telephone');
    }

    public function getDefaultOptions(array $options)
    {
        return array(
            'data_class' => 'DEMO\DemoBundle\Entity\User\Profile',
        );
    }

    public function getName()
    {
        return 'profile';
    }
}

DashboardController.php

public function profileAction(Request $request)
    {

        $user = $this->get('security.context')->getToken()->getUser();

        $form = $this->createForm(new UserType(), $user);

        if ($request->getMethod() == 'POST') {
            $form->bindRequest($request);

            if ($form->isValid()) {
                // Get $_POST data and submit to DB
                $em = $this->getDoctrine()->getEntityManager();
                $em->persist($user);
                $em->flush();

                // Set "success" flash notification
                $this->get('session')->setFlash('success', 'Profile saved.');
            }

        }

        $breadcrumbs = $this->get("white_october_breadcrumbs");
        $breadcrumbs->addItem("Home", $this->get("router")->generate("shop"));
        $breadcrumbs->addItem("My Account", $this->get("router")->generate("shop"));
        $breadcrumbs->addItem("My Profile", $this->get("router")->generate("shop"));

        return $this->render('DEMODemoBundle:User\Dashboard:profile.html.twig', array('form' => $form->createView()));
    }

profile.html.twig

<div class="container">
    <div class="row">
        <div class="span12">
            {% form_theme form 'DEMODemoBundle:User\\Form:fields.html.twig' %}

            {{ form_errors(form) }}

            <form action="{{ path('tk_update_profile') }}" class="form-horizontal" method="post" {{ form_enctype(form) }} novalidate="novalidate">
                <fieldset>
                    <legend>Your Details</legend>
                    {{ form_row(form.username) }}
                    {{ form_row(form.profile.name) }}
                    {{ form_row(form.email) }}
                </fieldset>
                <fieldset>
                    <legend>Company Details</legend>
                    {{ form_row(form.profile.companyName) }}
                    {{ form_row(form.profile.address1) }}
                    {{ form_row(form.profile.address2) }}
                    {{ form_row(form.profile.city) }}
                    {{ form_row(form.profile.county) }}
                    {{ form_row(form.profile.postcode) }}
                    {{ form_row(form.profile.telephone) }}
                </fieldset>
                <fieldset>
                    <div class="form-actions">
                        {{ form_rest(form) }}
                        <input class="btn-primary" type="submit" value="Save" />
                        <input class="btn-warning" type="reset" value="Reset">
                    </div>
                </fieldset>
            </form>
        </div>
    </div>
</div>

1 个答案:

答案 0 :(得分:3)

在填写表单时,由User实体支持,系统将尝试创建新的User对象并验证属于该实体的每个规则。
您需要的是验证组,您基本上只是说“检查”当我执行&#34;编辑&#34;时所适用的约束。操作&#39;

您可以在此处查看:http://symfony.com/doc/current/book/validation.html#validation-groups