2015-11-04 30 views
-1

我創造的Symfony2和一個字段形式如下:正則表達式不會對錶單驗證工作

->add('periodicity', 'number', array(
       'required' => true, 
       'attr'  => array(
        'autocomplete' => 'off', 
        'placeholder' => '2', 
       ), 
       'label'  => 'periodicity*', 
       'trim'  => true, 
       'constraints' => array(
        new Constraints\NotBlank(), 
        new Assert\Regex(array(
         'pattern' => "/^[1-9]|1[0-2]$/", 
         'match' => true, 
         'message' => "Tapez des chiffres entre 1 et 12")), 
       ) 
      )) 

在窗體的驗證,當我把一個數比優勢12我沒有得到任何錯誤。

+1

我知道你有一個答案,但'範圍'約束可能會更好地爲這個用例。 http://symfony.com/doc/current/reference/constraints/Range.html – qooplmao

回答

2

您需要將它們放入組中。

^([1-9]|1[0-2])$