Skip to main content
Topic: Error_Context removeError doesn't work on an error inserted as an array? (Read 2529 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

Error_Context removeError doesn't work on an error inserted as an array?

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?

Re: Error_Context removeError doesn't work on an error inserted as an array?

Reply #1

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
Bugs creator.
Features destroyer.
Template killer.

Re: Error_Context removeError doesn't work on an error inserted as an array?

Reply #2

$error shouldn't ever be null, right? Then use isset()

Re: Error_Context removeError doesn't work on an error inserted as an array?

Reply #3

Hey I thought you were over those micro optimizations :P

Re: Error_Context removeError doesn't work on an error inserted as an array?

Reply #4

I am, but isset() is easier to read and more consistent.

... it was a suggestion ;)

Re: Error_Context removeError doesn't work on an error inserted as an array?

Reply #5

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)
Bugs creator.
Features destroyer.
Template killer.

Re: Error_Context removeError doesn't work on an error inserted as an array?

Reply #6

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.
Last Edit: March 04, 2014, 10:28:50 am by scripple

Re: Error_Context removeError doesn't work on an error inserted as an array?

Reply #7

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
Bugs creator.
Features destroyer.
Template killer.

 

Re: Error_Context removeError doesn't work on an error inserted as an array?

Reply #8

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.