我正在使用没有类的表单构建器,并且有两个字段,每个字段都有约束:
$form = $this->createFormBuilder()
->add('name', 'text', array(
'required'=>false,
'constraints'=> new Length(array('min'=>3)
))
->add('dob', 'date', array(
'required'=>false,
'constraints'=> new Date()
))
->getForm()
->handleRequest($request);
这很好用,但我想检查两个字段是否都是emtpy,并显示错误。似乎无法解决这个问题。有人可以提供帮助???
答案 0 :(得分:3)
最简单的解决方案是根据需要设置两个......
但..在帖子上你可以检查就像
一样简单if( empty($form->get('name')->getData()) && empty($form->get('dob')->getData())){
$form->addError(new FormError("fill out both yo"));
// ... return your view
}else {
// ... do your persisting stuff
}
...
symfony方式可能会添加自定义验证器 我建议你查看custom validator,尤其是part
伪:
namespace My\Bundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class CheckBothValidator extends ConstraintValidator
{
public function validate($foo, Constraint $constraint)
{
if (!($foo->getName() && $foo->getDob()) {
$this->context->addViolationAt('both', $constraint->message, array(), null);
}
}
}
答案 1 :(得分:2)
在SELECT
*
FROM
q_products p
JOIN (
SELECT productid FROM q_product_apply_access paa
WHERE paa.productid = p.productid
AND paa.partnerid = 'partnerid'
AND paa.apply_status = 0
AND p.visibility = 3
) paa ON paa.productid = p.productid
JOIN (
SELECT productid FROM q_product_aff_access paf
WHERE paf.productid = p.productid
AND paf.accid = 'accountid'
AND p.visibility = 2
) paf ON paf.productid = p.productid
WHERE
p.status = 0
AND p.productId IN ( productIdList )
文件夹中创建文件名Bundle->Resources->Config
然后
validation.yml
一旦您检查提交的表单数据 isValid()
,验证就会发挥作用namespace\YourBundle\Entity\EntityName:
properties:
dob://field that you want to put validation on
- NotBlank: { message: "Message that you want to display"}
gender:
- NotBlank: { message: "Message that you want to display" }