0
下面是我對形式的Symfony2表單驗證
class PracticeType extends AbstractType {
/**
* Returns the name of this type.
*
* @return string The name of this type
*/
function getName()
{
return "practice";
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'Shwetanka\SampleBundle\Entity\Practice',
'csrf_protection' => true,
'csrf_field_name' => '_token',
'intention' => 'practice_item'
);
}
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('name', 'text', array(
'required' => true,
'label' => 'Clinic Name'
));
}
}
我的實體
/**
* Shwetanka\SampleBundle\Entity\Practice
*
* @ORM\Table(name="practice")
* @ORM\Entity
*/
class Practice
{
/**
* @var integer $id
*
* @ORM\Column(name="practiceId", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string $name
*
* @ORM\Column(name="name", type="string", length=255)
* @Assert\NotBlank()
*/
private $name;
//getters and setters
}
我的控制器代碼 -
...
$form = $this->createForm(new PracticeType());
if($request->getMethod() == 'POST'){
$form->bindRequest($request);
if($form->isValid()){
$practice = EntityHelper::preparePracticeForSave($form);
return array('hello' => 'world');
}else {
$view = View::create();
$view->setTemplate('ShwetankaSampleBundle:Practice:new_practice.html.twig');
$view->setFormat('html');
$view->setData(array('form' => $form->createView(), 'logout' => true));
return $this->get('fos_rest.view_handler')->handle($view);
}
}
...
當我提交表單,我得到驗證錯誤($form.isValid() is false)
但是當我檢查$form['name']
我得到了我在表單中輸入的值。我無法理解爲什麼驗證失敗。需要幫忙。
它只是顯示4個語句,每個語句都是 - '這個值不應該是空白的。'我不知道哪些價值。 – Shwetanka 2012-07-23 07:23:22