Bug Report #4385

ORM_Validation_Exception has_many bug

Added by Tomek Rychlik over 2 years ago. Updated over 2 years ago.

Status:NewStart date:01/04/2012
Priority:NormalDue date:
Assignee:-% Done:

0%

Category:-
Target version:Kohana v3.x - Unscheduled
Resolution: Points:1

Description

I think generate_errors in ORM_Validation_Exception has bug when adds new objects with has_many.

It generates wrong errors file names.

For example: add three has many obejects:

$user_exception = new ORM_Validation_Exception($user->errors_filename(), $user_validation);
$user_exception->add_object('user_point', $user_point_validation_1, TRUE);
$user_exception->add_object('user_point', $user_point_validation_2, TRUE);
$user_exception->add_object('user_point', $user_point_validation_3, TRUE);
$errors = $user_excepiton->errors('user');

User error file are ok ('message/user'), but user points error files are wrong:

message/user/0
message/user/1
message/user/2

I would like to have one error file for has many objects i.e:

message/user_point

In other words, it should allways return the same error file, no matter if has_many is TRUE, FALSE or string;

So, i think you should use this variable:
// We will need this when generating errors
$this->_objects[$alias]['_has_many'] = ($has_many !== FALSE);

You said you need them later, but you never used it.

My simple fix:

    protected function generate_errors($alias, array $array, $directory, $translate)
    {
        $errors = array();

        foreach ($array as $key => $object)
        {
            if (is_array($object))
            {
                if ( ! Arr::get($array, '_has_many'))
                {
                    $alias = $key;
                }

                                $errors[$key] = $this->generate_errors($alias, $object, $directory, $translate);

            }
            elseif ($object instanceof Validation)
            {
                if ($directory === NULL)
                {
                    // Return the raw errors
                    $file = NULL;
                }
                else
                {
                    $file = trim($directory.'/'.$alias, '/');
                }

                echo $file.'   |   ';

                // Merge in this array of errors
                $errors += $object->errors($file, $translate);
            }
        }

        return $errors;
    }

History

#1 Updated by Woody Gilk over 2 years ago

  • Target version set to Unscheduled

Also available in: Atom PDF