Symfony2,表达式验证器不起作用

时间:2015-12-22 19:44:44

标签: validation symfony

表达式验证器无效。 我正在关注Symfony2文档示例: http://symfony.com/doc/2.7/reference/constraints/Expression.html

在我的简化案例中, C:\ Bitnami \ wampstack-5.5.30-0 \ sym_prog \ proj3_27 \应用\配置\ validation.yml

    MeetingBundle]Entity\SearchLtk:
        constraints:
            - Expression:
                expression: "this.keywL<2"
                message: "Input more search words"

C:\ Bitnami \ wampstack-5.5.30-0 \ sym_prog \ proj3_27 \应用\配置\ config.yml

framework:
    validation:    { enabled: true, enable_annotations: true } 

C:\ Bitnami \ wampstack-5.5.30-0 \ sym_prog \ proj3_27 \ SRC \ MeetingBundle \实体\ SearchLtk.php

    /**
     * SearchLtk
     *
     * @ORM\Table(name="search_ltk",indexes={@ORM\Index(columns={"zip"}, flags={"fulltext"})})
     * @ORM\Entity(repositoryClass="MeetingBundle\Repository\SearchLocRepository" )
     */
    class SearchLtk
    {....

         /**
         * @var string
         *
         * @ORM\Column(name="keyw", type="string", nullable = true)
         */
        private $keyw;    

   ... 
        /**
         *
         * @return integer
         */
            public function keywL($keyw)
        {
            $kArr = preg_split( "/[;,\.]+/", $keyw ); 
            foreach ( $kArr as $key=>$item ) {
                $item=trim($item); 
    // the string becomes "", but length is 1, count returns 1
    //but empty returns correctly 
                if( count($item) == 0 or (empty($item))){
                    unset($kArr[$key]);
                }
            }
            return count($kArr );
        } 
...

1 个答案:

答案 0 :(得分:0)

1)您不应该在实体中拥有方法。

2)结帐此功能http://php.net/manual/en/function.str-word-count.php