phparraysmultidimensional-arrayzend-framework2

Zend Framework2 Form Errors Array To JSON


I'm receiving this array from zend form

Array
(
    [user] => Array
        (
            [firstName] => Array
                (
                    [isEmpty] => Please enter First Name
                )

            [lastName] => Array
                (
                    [isEmpty] => Please enter Last Name
                )

            [password] => Array
                (
                    [isEmpty] => Please enter Password
                )

            [confirmPassword] => Array
                (
                    [isEmpty] => Please enter Confirm Password
                )

            [email] => Array
                (
                    [isEmpty] => Please enter Email Address
                )

            [gender] => Array
                (
                    [notInArray] => Gender must be Male OR Female
                    [isEmpty] => Please select your gender
                )

            [phone] => Array
                (
                    [isEmpty] => Value is required and can't be empty
                )

            [birthDate] => Array
                (
                    [isEmpty] => Please enter Birth Date
                )

            [country] => Array
                (
                    [id] => Array
                        (
                            [isEmpty] => Please select Country
                        )

                )

            [userGroup] => Array
                (
                    [id] => Array
                        (
                            [isEmpty] => Please select User Group
                        )

                )

        )

)

and I want this array to be converted into the following array:

Array
(
    [user[firstName]] => [{"Please enter First Name"}]
    [user[lastName]] => [{"Please enter Last Name"}]

    ...........

    [user[gender]] => [{"Gender must be Male OR Female","Please select your gender"}]

    ..............
    [user[country][id]] => [{"Please select Country"}]
)

I tried the following code but It didn't work

$errors = $form->getMessages();
$newErrors = array();
foreach ($errors as $key => $value) {
    $elementName = $key;
    if(is_array($value)) {
        foreach ($value as $k => $v) {
            $elementName .= $k;
            if(!is_array($v)) {
                $message = $v;
                $newErrors[$elementName] = json_encode($v);
            } else {
                foreach ($v as $ke => $va) {
                    $elementName .= $ke;
                    if(!is_array($va)) {
                        $newErrors[$elementName] = json_encode($va);
                    }
                }
            }
        }
    }
}

But it didn't work.


Solution

  • To directly answer your question.

    This has not been tested, although I believe it should work.

    public function formatElementErrors(FieldsetInterface $collection, array $data, $parent = null)
    {
        $results = [];
    
        foreach($data as $name => $errors) {
    
            if (! $collection->has($name) || ! is_array($errors)) {
                continue;
            }
    
            $element = $collection->get($name);
    
            $name = isset($parent) ? sprintf('%s[%s]', $parent, $name) : $name;
    
            if ($element instanceof FieldsetInterface) {
    
                $results = array_merge(
                    $results,
                    $this->formatElementErrors($element, $errors, $name)
                );
    
            } else {
                if (! isset($results[$name])) $results[$name] = [];
    
                foreach($errors as $message) {
                    $results[$name][] = $message;
                }
            }
        }
        return $results;
    }
    
    $errors = $this->formatElementErrors($form, $errors);