ZF-11863: Validation fails using 'à' char

Description

I've created a form using the following code to add an input element:


$Element = new Zend_Form_Element_Text('field');
$Element->setRequired();
$Element->addValidators(array(
    new Zend_Validate_Regex('/^[[:alpha:]\']+$/iu'),
    new Zend_Validate_StringLength(array('min' => 3, 'max' => 50))));
$Element->addFilters(array(new Zend_Filter_StringTrim()));
$this->addElement($Element);
unset($Element);

This is a summary of what I input in the field, what I should got and what I really got. ||Input||Expected||Gotten|| |test|true|true| |test99|false|false| |òèùtestòò|true|true| |testà|true|"There was an internal error while using the pattern '/^[[:alpha:]\']+$/iu'"| |teààst|true|true| |ààòòìùéé|true|true| |èùòìiieeà|true|"There was an internal error while using the pattern '/^[[:alpha:]\']+$/iu'"|

Moreover, if I try to run the input in a normal preg_match, I got the expected results as shown in the following table: ||Input||Expected||Gotten|| |test|true|true| |test99|false|false| |òèùtestòò|true|true| |testà|true|true| |teààst|true|true| |ààòòìùéé|true|true| |èùòìiieeà|true|true|

As you can see, the validator seems to fail when the 'à' is at the end of the string. The same weird behavior can be seen using the Zend_Validate_Alpha too. In this case, sometimes, the view shows an empty error string. I mean that it will show the list tag (li) as he wants to show an error, but this one is empty.

Note: I found that the problem is exactly in this method of the Zend_Filter_StringTrim


    protected function _unicodeTrim($value, $charlist = '\\\\s')
    { // here the value it's ok
        $chars = preg_replace(
            array( '/[\^\-\]\\\]/S', '/\\\{4}/S', '/\//'),
            array( '\\\\\\0', '\\', '\/' ),
            $charlist
        );

        $pattern = '^[' . $chars . ']*|[' . $chars . ']*$';
        return preg_replace("/$pattern/sSD", '', $value);  // here the last à (a multibyte char) has been truncated
    }

Comments

Added related unittests with GH-592... no failure reproduceable for the validator

I found the exact method where the issue came (and I've updated the post). I don't know if it has been fixed in ZF2.

When the failure does not occur with ZF2 it must have been fixed in past.

I found that the problem I've reported is a duplicate of this issue: http://framework.zend.com/issues/browse/ZF-11533

This occurs due a PHP bug for versions lower than v5.3.4 (https://bugs.php.net/bug.php?id=52971)

As a workaround you can set the locale (setlocale) with a locale that supports the characters.