2015-09-04 140 views
0

我不明白爲什麼我的正則表達式不起作用。任何密碼總是有錯誤信息。Phalcon正則表達式驗證密碼不起作用

$this->add('password', new RegexValidator(array(
      'pattern' => '.{0}|.{4,}', 
      'message' => '0 or 4 char minimum' 
))); 

錯誤是:0或4個字符最小值。

+1

確切地說,錯誤信息是什麼? –

+0

this:0或4個字符最小值。這是我的消息 – John

+0

這是應該在你的問題中的重要信息。評論意味着是暫時的。請[編輯]你的問題。 –

回答

1

剛剛與allowEmpty選項結合StringLenght驗證:

$badPassMessage = 'Define a password using exactly 4 characters or leave it empty!'; 
$validation->add('password', new StringLength(array(
     'max' => 4, 
     'min' => 4, 
     'allowEmpty' => true, 
     'messageMaximum' => $badPassMessage, 
     'messageMinimum' => $badPassMessage 
))); 
BONUS

我建議你總是與這個優秀的工具來測試模式:

regular expressions 101

始終使確定正則表達式的風格設置爲pcre (php)

+0

哦太棒了。 allowEmpty參數是解決方案。所以現在我可以使用帶有allowEmpty參數的StringLenght驗證https://docs.phalconphp.com/en/latest/api/Phalcon_Validation_Validator_StringLength.html。非常感謝 !!!!! – John

+0

@John我已經用你的解決方案更新了答案 – cvsguimaraes

+0

好了:)。但我沒有使用最大參數。只是最小。 – John