Symfony窗​​体声明 [英] Symfony Forms Assert

查看:132
本文介绍了Symfony窗​​体声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  class Event 
{
。 .....
/ **
* @var datetime $ date
*
* @ ORM\Column(name =date_debut_inscri,type =datetime)
* @ Assert\GreaterThanOrEqual(今日UTC)
* /
保护$ dateDebutInscri;
......
}

复选框被选中并且它在控制器中设置为空,问题是当这个字段被隐藏时,Assert错误消息仍然出现,这使我无法提交我的表单。



我希望它默认为null,如果用户选择填充此字段,它必须大于或等于当前日期。



我想知道我是否可以做一些事情如:

  * @ Assert\GreaterThanOrEqual(today UTC)或@ Assert \IsNull()

其中只需要两个Assert中的一个为真

< PS:dateDebutInscri是法文,意思是活动题词打开的日期。

能够使用GroupSequence解决这个问题,这里解释:

http://symfony.com/doc/current/validation/sequence_provider.html



增加了一些代码我的事件类,它都很好

 使用Symfony \Component\Validator\GroupSequenceProviderInterface; 
$ b $ ** b $ b * @ ORM\Table(name =event)
* @ ORM \Entity(repositoryClass =AppBundle\Repository\EventRepository )
* @ Assert\GroupSequenceProvider
* /
class Event implements GroupSequenceProviderInterface
{
......
/ **
* @var datetime $ date
*
* @ ORM\Column(name =date_debut_inscri,type =datetime)
* @ Assert\GreaterThanOrEqual(today UTC ,groups = {grp1})
* /
保护$ dateDebutInscri;
......

public function getGroupSequence(){
$ groups = ['Default','Event'];

if(!$ this-> getMyCheckBox())
{
$ groups [] ='grp1';
}
返回$ groups;
}
}


I have Event class that has a field like this :

class Event
{
    ......
    /**
     * @var datetime $date
     *
     * @ORM\Column(name="date_debut_inscri", type="datetime")
     * @Assert\GreaterThanOrEqual("today UTC")
     */
     protected $dateDebutInscri;
     ......
}

This field is hidden when a checkbox is checked and it's set to null in the controller, the problem is when this field is hidden the Assert error message still appears and that makes me unable to submit my form

I want it to be null by default and if the user chooses to fill this field it must be greater or equal than the current date

I was wondering if i could do soomething like :

* @Assert\GreaterThanOrEqual("today UTC") OR @Assert\IsNull()

where it requires only one of the two Assert to be true

PS : "dateDebutInscri" is in french and it means the date when the inscription to the event is open

解决方案

I was able to so solve this problem using GroupSequence, it's explained here :

http://symfony.com/doc/current/validation/sequence_provider.html

Added some code to my Event class and it's all good

use Symfony\Component\Validator\GroupSequenceProviderInterface;

/**
 * @ORM\Table(name="event")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\EventRepository")
 * @Assert\GroupSequenceProvider
 */
class Event implements GroupSequenceProviderInterface
{
......
   /**
    * @var datetime $date
    *
    * @ORM\Column(name="date_debut_inscri", type="datetime")
    * @Assert\GreaterThanOrEqual("today UTC", groups = {grp1})
    */
    protected $dateDebutInscri;
......

    public function getGroupSequence(){
            $groups = ['Default', 'Event'];

            if(!$this->getMyCheckBox())
            {
                $groups[] = 'grp1';
            }
            return $groups;
    }
}

这篇关于Symfony窗​​体声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆