你可以寫一個自定義的驗證,檢查了這一點:http://framework.zend.com/manual/2.0/en/modules/zend.validator.writing-validators.html
class MyValid\PasswordStrength extends Zend\Validator\AbstractValidator
{
const LENGTH = 'length';
const UPPER = 'upper';
const LOWER = 'lower';
const DIGIT = 'digit';
protected $messageTemplates = array(
self::LENGTH => "'%value%' must be at least 8 characters in length",
self::UPPER => "'%value%' must contain at least one uppercase letter",
self::LOWER => "'%value%' must contain at least one lowercase letter",
self::DIGIT => "'%value%' must contain at least one digit character"
);
public function isValid($value)
{
$this->setValue($value);
$isValid = true;
if (strlen($value) < 8) {
$this->error(self::LENGTH);
$isValid = false;
}
if (!preg_match('/[A-Z]/', $value)) {
$this->error(self::UPPER);
$isValid = false;
}
if (!preg_match('/[a-z]/', $value)) {
$this->error(self::LOWER);
$isValid = false;
}
if (!preg_match('/\d/', $value)) {
$this->error(self::DIGIT);
$isValid = false;
}
return $isValid;
}
}
然後選擇該自定義的驗證:
$inputFilter->add($factory->createInput(array(
'name' => 'password',
'required' => true,
'validators' => array(
array(
// What ever your namespace is etc determines this..
'name' => 'My\Validator\PasswordStrength',
),
),
)));