浏览代码

FormContext error() should return array with error names as keys.

Fixes #13235
Robert Pustułka 6 年之前
父节点
当前提交
fcdfd9ae2e
共有 2 个文件被更改,包括 5 次插入5 次删除
  1. 1 1
      src/View/Form/FormContext.php
  2. 4 4
      tests/TestCase/View/Form/FormContextTest.php

+ 1 - 1
src/View/Form/FormContext.php

@@ -226,6 +226,6 @@ class FormContext implements ContextInterface
      */
     public function error($field)
     {
-        return array_values((array)Hash::get($this->_form->getErrors(), $field, []));
+        return (array)Hash::get($this->_form->getErrors(), $field, []);
     }
 }

+ 4 - 4
tests/TestCase/View/Form/FormContextTest.php

@@ -295,9 +295,9 @@ class FormContextTest extends TestCase
 
         $context = new FormContext($this->request, ['entity' => $form]);
         $this->assertEquals([], $context->error('empty'));
-        $this->assertEquals(['The provided value is invalid'], $context->error('email'));
-        $this->assertEquals(['The provided value is invalid'], $context->error('name'));
-        $this->assertEquals(['The provided value is invalid'], $context->error('pass.password'));
+        $this->assertEquals(['format' => 'The provided value is invalid'], $context->error('email'));
+        $this->assertEquals(['length' => 'The provided value is invalid'], $context->error('name'));
+        $this->assertEquals(['length' => 'The provided value is invalid'], $context->error('pass.password'));
         $this->assertEquals([], $context->error('Alias.name'));
         $this->assertEquals([], $context->error('nope.nope'));
 
@@ -307,7 +307,7 @@ class FormContextTest extends TestCase
         $form->validate([]);
         $context = new FormContext($this->request, ['entity' => $form]);
         $this->assertEquals(
-            ['should be an array, not a string'],
+            ['_required' => 'should be an array, not a string'],
             $context->error('key'),
             'This test should not produce a PHP warning from array_values().'
         );