zendframework / zf1

This project reached its end-of-life on 2016-09-28. Contains conversion of ZF1 subversion repo to git, from version 15234 forward, and only containing master and release-1.12 branches and 1.12 tags.
https://framework.zend.com/blog/2016-06-28-zf1-eol.html
BSD 3-Clause "New" or "Revised" License
357 stars 796 forks source link

An Ungreedy IBAN validation is not possible, if you have a Zend_Locale in your Zend_Registry. #527

Open sPooKee opened 9 years ago

sPooKee commented 9 years ago

An Ungreedy IBAN validation with new Zend_Validate_Iban(['locale' => false]) is not possible, if you have a Zend_Locale in your Zend_Registry.

Zend/Validate/Iban.php:143ff sets $locale = $locale['locale'];, which is false, and since empty(false) is true, $locale gets overwritten by Zend_Registry::get('Zend_Locale');.

if (is_array($locale)) {
  if (array_key_exists('locale', $locale)) {
    $locale = $locale['locale'];
  } else {
    $locale = null;
  }
}

if (empty($locale)) {
  require_once 'Zend/Registry.php';
  if (Zend_Registry::isRegistered('Zend_Locale')) {
    $locale = Zend_Registry::get('Zend_Locale');
  }
}
froschdesign commented 9 years ago

I think, changing the behaviour in the constructor at this point is a bad idea. (This can be a BC break.)

But you can use the setLocale method:

public function testIgnoreLocaleFromRegistry()
{
    require_once 'Zend/Registry.php';
    Zend_Registry::set('Zend_Locale', new Zend_Locale('de_AT'));

    $validator = new Zend_Validate_Iban();
    $validator->setLocale(false);
    $this->assertTrue($validator->isValid('DE68210501700012345678'));
}
sPooKee commented 9 years ago

@froschdesign In your Example Zend_Validate_Iban will still use de_AT, because empty($locale) is still false.

By the way, I've just overwritten the class and used the following in my __construct() function:

...
if (false !== $locale && empty($locale)) {
    require_once 'Zend/Registry.php';
    if (Zend_Registry::isRegistered('Zend_Locale')) {
        $locale = Zend_Registry::get('Zend_Locale');
    }
}
...
froschdesign commented 9 years ago

In your Example Zend_Validate_Iban will still use de_AT, because empty($locale) is still false.

No. Please have a look:

$validator->setLocale(false);
sPooKee commented 9 years ago

Ah, okay, sorry, I see ;) But that doesn't help me in a "short" FormElement creation:

...
    $e['IBAN'] = $this->createElement('text', 'IBAN')
            ->addFilters([
                ['StringTrim'],
                ['StringToUpper']
            ])
            ->addValidators([
                ['Iban', false, ['locale'=> false]],
            ])
            ->setRequired();
...
froschdesign commented 9 years ago

But that doesn't help me in a "short" FormElement creation:

->addValidators(
        [
            call_user_func(function () {
                $validator = new Zend_Validate_Iban();
                $validator->setLocale(false);

                return $validator;
            }),
        ])

:wink:

froschdesign commented 9 years ago

The problem still exists: This can be a BC break.