所以,我有一个表单,允许用户更新他们的个人资料详细信息(姓名、电子邮件等)
表单使用指向用户实体的表单类型 (UserType) 正确显示。UserType 使用用户名、电子邮件构建表单,然后扩展到 ProfileType - 另一种表单类型,包括他们的真实姓名、地址等。
正如我所提到的,表单正确显示在页面上。
现在,在我的 validation.yml 文件中,我指向 User 和 Profile 实体,对于 User,我定义了 3 个验证规则,分别用于用户名、电子邮件和密码。对于 Profile,我正在定义名称和公司名称的规则。所有规则都有 NotBlank,电子邮件也有检查以确保它是有效的,并且密码有最大和最小长度定义。
现在,最近添加了密码规则。以前,此表单工作正常,没有问题。
添加密码规则,当表单提交时,它会尝试检查这个验证规则,但是页面上没有呈现密码字段,甚至在表单类型中提到了用户类型。
当然,如果我不使用输入,它不应该根据验证规则检查它吗?
下面的代码:
验证.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: ~
用户类型.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';
}
}
配置文件类型.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';
}
}
仪表板控制器.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>