所以,我有一個表單,允許用戶正確地更新他們的個人資料的詳細信息(姓名,電子郵件等等等等)Symfony2中 - 形式試圖驗證未使用的字段
的形式顯示,使用表單類型(用戶類型)指向用戶實體。 UserType使用用戶名,電子郵件構建表單,然後擴展到ProfileType - 包含其真實姓名,地址等的另一種表單類型。
正如我所提到的,表單正確顯示在頁面上。
現在,在我的validation.yml文件中,我指向用戶和配置文件實體,對於用戶,我爲用戶名,電子郵件和密碼定義了3個驗證規則。對於Profile,我正在定義名稱和公司名稱的規則。所有規則都有NotBlank,電子郵件也有檢查以確保它是有效的,並且密碼已經定義了最大和最小長度。
現在,密碼規則是最近添加的。以前,這種形式運行良好,沒有問題。
在提交表單時,它會嘗試檢查此驗證規則,但頁面上沒有呈現密碼字段,甚至沒有在表單類型,用戶類型中提及密碼規則。
當然,如果我不使用輸入,它不應該根據驗證規則檢查它嗎?下面
代碼:
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>