Zend Framework

Zend_Validate_Regex will fail for certain valid regular expressions

Details

  • Type: Bug Bug
  • Status: Resolved Resolved
  • Priority: Minor Minor
  • Resolution: Not an Issue
  • Affects Version/s: 1.10.5
  • Fix Version/s: 1.10.7
  • Component/s: Zend_Validate
  • Labels:
    None

Description

Have created a form using the following element

form_signIn password element
$this->addElement("password", "pass_word", array(
 "label"=>"Password",
 "required"=>true,
 "validators"=>array(
  array("Regex", true, array("[a-zA-Z0-9~!@$%^&*]{5,15}"))
 )
));

I get the following error rendered in my browser

"Internal error while using the pattern '[a-zA-Z0-9~!@$%^&*]{5,15}'"

The regular expression matches correctly if I use a java pattern matching tool (Part of jEdit)

I have tracked the fault back to Zend/Validate/Regex.php Line 107

setPattern function
/**
 * Sets the pattern option
 *
 * @param  string $pattern
 * @throws Zend_Validate_Exception if there is a fatal error in pattern matching
 * @return Zend_Validate_Regex Provides a fluent interface
*/
public function setPattern($pattern)
{
 $this->_pattern = (string) $pattern;
 $status         = @preg_match($this->_pattern, "Test"); **** This is line 107 ****
 if (false === $status) {
  require_once 'Zend/Validate/Exception.php';
  throw new Zend_Validate_Exception("Internal error while using the pattern '$this->_pattern'");
 }

 return $this;
}

As you can see the subject of preg_match will never pass my regular expression. I know that workarounds exist. I could daisy chain the regex validator with the stringLength validator or I could alter my regex

Activity

Hide
Hendri Smit added a comment -

Your regex pattern doesn't have any delimiters so it is invalid. That's why the exception is thrown.
The preg_match function will only return false if the pattern is incorrect. It returns 0 if no match was found.

This is not a bug. Read http://php.net/manual/en/book.pcre.php for more info on regular expressions in PHP.

Show
Hendri Smit added a comment - Your regex pattern doesn't have any delimiters so it is invalid. That's why the exception is thrown. The preg_match function will only return false if the pattern is incorrect. It returns 0 if no match was found. This is not a bug. Read http://php.net/manual/en/book.pcre.php for more info on regular expressions in PHP.
Hide
Feighen Oosterbroek added a comment -

I know. I had posted the bug by the time I had realised why it was returning false, and couldn't find a way to resolve the bug myself

Show
Feighen Oosterbroek added a comment - I know. I had posted the bug by the time I had realised why it was returning false, and couldn't find a way to resolve the bug myself
Hide
Thomas Weidner added a comment -

Closing as non issue according to the responses

Show
Thomas Weidner added a comment - Closing as non issue according to the responses
Hide
Thomas Weidner added a comment -

Correct fix version as it has been deleted unintentionally by another user

Show
Thomas Weidner added a comment - Correct fix version as it has been deleted unintentionally by another user

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: