ElkArte Community

Elk Development => Bug Reports => Exterminated Bugs => Topic started by: scripple on March 02, 2014, 10:12:50 pm

Title: Error_Context removeError doesn't work on an error inserted as an array?
Post by: scripple on March 02, 2014, 10:12:50 pm
I wish to be able to selectively register people with the same email address.  So I integrated on your integrate_register_check hook inside Members.subs.php figuring I would just unset the 'email_in_use' error in the contexts I wish to do that.

Except it doesn't work.  I call $reg_errors->removeError('email_in_use') and nothing happens to $reg_errors.  Trying it on the error 'profile_error_password_short' the error does get removed.

This is what $reg_errors look like (with a fake email of course)
Code: [Select]
regErrors:Error_Context Object
(
    [_name:Error_Context:private] => register
    [_errors:Error_Context:private] => Array
        (
            [0] => Array
                (
                    [profile_error_password_short] => profile_error_password_short
                    [email_in_use] => Array
                        (
                            [0] => email_in_use
                            [1] => Array
                                (
                                    [0] => noone@nowhere.nowhere
                                )

                        )

                )

        )

    [_default_severity:Error_Context:private] => 0
    [_severity_levels:Error_Context:private] => Array
        (
            [0] => 0
            [1] => 1
        )

    [_language_files:Error_Context:private] => Array
        (
        )

)

I've tried calling with 'email_in_use', the actual email address, the the exact array that was handed to addError (which since removeError take the first element of the array was pointless) and none of them work.

Any tips on how to remove this error?  Any php function more appropriate here than in_array?
Title: Re: Error_Context removeError doesn't work on an error inserted as an array?
Post by: emanuele on March 03, 2014, 02:19:45 pm
Indeed my fault... :-[

In Errors.class.php:
Code: (find) [Select]
if (in_array($error, $errors))
Code: (replace with) [Select]
if (array_key_exists($error, $errors))

Pushed here:
https://github.com/emanuele45/Dialogo/commit/fd4badbf96259600130422d81340f1d7dbd21ade
Title: Re: Error_Context removeError doesn't work on an error inserted as an array?
Post by: Joshua Dickerson on March 03, 2014, 02:57:14 pm
$error shouldn't ever be null, right? Then use isset()
Title: Re: Error_Context removeError doesn't work on an error inserted as an array?
Post by: Spuds on March 03, 2014, 03:32:33 pm
Hey I thought you were over those micro optimizations :P
Title: Re: Error_Context removeError doesn't work on an error inserted as an array?
Post by: Joshua Dickerson on March 03, 2014, 03:39:08 pm
I am, but isset() is easier to read and more consistent.

... it was a suggestion ;)
Title: Re: Error_Context removeError doesn't work on an error inserted as an array?
Post by: emanuele on March 03, 2014, 04:13:39 pm
mmm... I'm always floating between one or the other, I usually prefer isset, this time I used array_key_exists because it didn't require changing the arguments... (Yeah, I know, strong reasoning behind LOL)
Title: Re: Error_Context removeError doesn't work on an error inserted as an array?
Post by: scripple on March 03, 2014, 10:21:00 pm
Nope, still not working as an empty array[severitylevel] still exists in the _errors array after the removeError call.  Here's the progression.

Original clean regErrors
Code: [Select]
regErrors:Error_Context Object
(
    [_name:Error_Context:private] => register
    [_errors:Error_Context:private] => Array
        (
        )

    [_default_severity:Error_Context:private] => 0
    [_severity_levels:Error_Context:private] => Array
        (
            [0] => 0
            [1] => 1
        )

    [_language_files:Error_Context:private] => Array
        (
        )

)

After array type error input.  There error is created under a [ 0 ] (severity) array.
Code: [Select]
regErrors:Error_Context Object
(
    [_name:Error_Context:private] => register
    [_errors:Error_Context:private] => Array
        (
            [0] => Array
                (
                    [email_in_use] => Array
                        (
                            [0] => email_in_use
                            [1] => Array
                                (
                                    [0] => noone@nowhere.nowhere
                                )

                        )

                )

        )

    [_default_severity:Error_Context:private] => 0
    [_severity_levels:Error_Context:private] => Array
        (
            [0] => 0
            [1] => 1
        )

    [_language_files:Error_Context:private] => Array
        (
        )

)

After the error is "removed".  The [ 0 ](severity) array is still there.
Code: [Select]
regErrors:Error_Context Object
(
    [_name:Error_Context:private] => register
    [_errors:Error_Context:private] => Array
        (
            [0] => Array
                (
                )

        )

    [_default_severity:Error_Context:private] => 0
    [_severity_levels:Error_Context:private] => Array
        (
            [0] => 0
            [1] => 1
        )

    [_language_files:Error_Context:private] => Array
        (
        )
)

Now the problem.  hasErrors() seen here...
Code: [Select]
  public function hasErrors($severity = null)
  {
    if ($severity !== null && in_array($severity, $this->_severity_levels))
      return !empty($this->_errors[$severity]);
    elseif ($severity === null)
      return !empty($this->_errors);
    else
      return false;
  }
is called by registerMember without a severity.  So hasErrors just does a !empty($this->errors) which isn't empty anymore as the empty severity[ 0 ] array still exists.  It seems hasErrors needs to be smarter and not consider the existence of empty severity arrays as errors, or removeError needs to clean up those arrays when empty.  One or the other.
Title: Re: Error_Context removeError doesn't work on an error inserted as an array?
Post by: emanuele on March 04, 2014, 08:21:38 am
Meh... why didn't I add some tests for that class? :P

Fix:
https://github.com/emanuele45/Dialogo/commit/63a460e90d9294c6cde5d816fcb30ff9c76d2743

And few tests added as well:
https://github.com/emanuele45/Dialogo/commit/562cf65b1498831e0fb9e2795637427d8e997c0f
so that at least the basic methods are tested.
There is more to add, but depending on the fate of SimpleTest it may be worth considering the switch to PHPUnit before.

And thanks again for the testing! :D
Title: Re: Error_Context removeError doesn't work on an error inserted as an array?
Post by: scripple on March 04, 2014, 10:32:19 am
Works now.  Thanks. 

And silly me saw the MINOR/SERIOUS consts that are in the code (but not in my variable dumps obviously) and apologize for the now removed dead wrong editorial.  Sorry about that and thank you and the rest of the devs for always responding quickly to my bug reports.