我遇到问题,我有一个公司实体使用doctrine和zend表单注释,所以每当我尝试添加一个新公司时它都是无效的,检查它告诉我的消息' active&# 39;,这是一个必填字段,在我的表单中为空或null,这是因为我在我的数据库中设置了默认值1。所以我的问题是,有没有办法忽视“活跃”的问题。绑定并验证我的表单的属性?
这是我的添加动作
public function addAction() {
$view = new ViewModel();
$company = new Company();
$entityManager = $this->entityManager();
$builder = new DoctrineAnnotationBuilder($entityManager);
$form = $builder->createForm($company);
$request = $this->getRequest();
if ($request->isPost()){
$form->bind($company);
$form->setData($request->getPost());
if ($form->isValid()){
print_r($form->getData());exit;
}
print_r($form->getMessages());
}
$view->form = $form;
return $view;
}
这是我的实体
namespace Nomina\Entity;
use Doctrine\ORM\Mapping as ORM;
use Zend\Form\Annotation;
/**
* Company
*
* @ORM\Table(name="companies")
* @ORM\Entity
* @Annotation\Hydrator("Zend\Stdlib\Hydrator\ObjectProperty")
* @Annotation\Name("CompanyForm")
*/
class Company
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255, nullable=false)
* @Annotation\Type("Zend\Form\Element\Text")
* @Annotation\Required({"required":"true"})
* @Annotation\Filter({"name":"StripTags"})
* @Annotation\Options({"label":"Company name: "})
* @Annotation\Attributes({"class":"form-control"})
* @Annotation\Validator({"name":"NotEmpty","options":{"messages":{"isEmpty":"Empresa no puede ser vacio"}}})
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="rfc", type="string", length=45, nullable=false)
* @Annotation\Type("Zend\Form\Element\Text")
* @Annotation\Required({"required":"true"})
* @Annotation\Filter({"name":"StripTags"})
* @Annotation\Options({"label":"RFC: "})
* @Annotation\Attributes({"class":"form-control"})
*/
private $rfc;
/**
* @var string
*
* @ORM\Column(name="street", type="string", length=255, nullable=false)
* @Annotation\Type("Zend\Form\Element\Text")
* @Annotation\Required({"required":"true"})
* @Annotation\Filter({"name":"StripTags"})
* @Annotation\Options({"label":"Street: "})
* @Annotation\Attributes({"class":"form-control"})
*/
private $street;
/**
* @var string
*
* @ORM\Column(name="City", type="string", length=255, nullable=false)
* @Annotation\Type("Zend\Form\Element\Text")
* @Annotation\Required({"required":"true"})
* @Annotation\Filter({"name":"StripTags"})
* @Annotation\Options({"label":"City: "})
* @Annotation\Attributes({"class":"form-control"})
*/
private $city;
/**
* @var string
*
* @ORM\Column(name="number", type="string", length=45, nullable=false)
* @Annotation\Type("Zend\Form\Element\Text")
* @Annotation\Required({"required":"true"})
* @Annotation\Filter({"name":"StripTags"})
* @Annotation\Options({"label":"Number: "})
* @Annotation\Attributes({"class":"form-control"})
*/
private $number;
/**
* @var string
*
* @ORM\Column(name="phone", type="string", length=45, nullable=true)
* @Annotation\Type("Zend\Form\Element\Text")
* @Annotation\Filter({"name":"StripTags"})
* @Annotation\Options({"label":"Phone: "})
* @Annotation\Attributes({"class":"form-control"})
*/
private $phone;
/**
* @var integer
*
* @ORM\Column(name="active", type="integer", nullable=false)
*/
private $active = '1';
/**
* @Annotation\Type("Zend\Form\Element\Submit")
* @Annotation\Attributes({"value":"Procesar"})
* @Annotation\Attributes({"class":"btn btn-primary"})
*/
public $submit;
和我的添加视图
$title = 'Add Company';
$this->headTitle($title);
?>
<h1><?php echo $this->escapeHtml($title); ?></h1>
<hr />
<?php
$form = $this->form;
$form->setAttribute('action', $this->url('company', array('action' => 'add')));
$form->prepare();
echo $this->form()->openTag($form);
echo $this->formRow($form->get('name'));
echo $this->formRow($form->get('rfc'));
echo $this->formRow($form->get('street'));
echo $this->formRow($form->get('number'));
echo $this->formRow($form->get('city'));
echo $this->formRow($form->get('phone'));
echo $this->formRow($form->get('submit'));
echo $this->form()->closeTag();
当我打印消息后,我得到了isValid y得到此消息
Array ( [active] => Array ( [isEmpty] => Value is required and can't be empty ) )
答案 0 :(得分:0)
根据具体情况,只需在您的操作中设置验证组
$form->setValidationGroup('name', 'email', 'subject', 'message');
$form->setData($data);
if ($form->isValid()) {
// Contains only the "name", "email", "subject", and "message" values
$data = $form->getData();
}
以防您需要添加验证码:这是一个完整的示例:
$entityManager = $this->entityManager();
$builder = new DoctrineAnnotationBuilder($entityManager);
$form = $builder->createForm($company);
$form->add(array(
'name' => 'captcha',
'type' => 'Zend\Form\Element\Captcha',
'options' => array(
'captcha' => new \Zend\Captcha\Figlet(array(
'wordLen' => 3,
)),
),
));
$request = $this->getRequest();
if ($request->isPost()){
$form->bind($company);
$form->setValidationGroup('captcha',...);
$form->setData($request->getPost());
请记住:使用setValidationGroup()的任何排除字段都不会包含在表单数据中!
官方文档中的更多信息http://framework.zend.com/manual/2.3/en/modules/zend.form.quick-start.html#validation-groups