From 7f6e54ea3af67c05c20cac7b73853578a7e2e32f Mon Sep 17 00:00:00 2001 From: joelmoss Date: Wed, 23 Apr 2008 14:14:58 +0000 Subject: [PATCH] Validation now supports error messages on multi record forms. Added tests to support this. Closes #4499. Thanks jose_zap git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6713 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/view/helpers/form.php | 9 +++++++++ cake/tests/cases/libs/view/helpers/form.test.php | 15 +++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/cake/libs/view/helpers/form.php b/cake/libs/view/helpers/form.php index 65f626934..251baf689 100644 --- a/cake/libs/view/helpers/form.php +++ b/cake/libs/view/helpers/form.php @@ -382,6 +382,15 @@ class FormHelper extends AppHelper { $options = array_merge(array('wrap' => true, 'class' => 'error-message', 'escape' => true), $options); if ($error = $this->tagIsInvalid()) { + if (is_array($error)) { + list(,,$field) = explode('.', $field); + if (isset($error[$field])) { + $error = $error[$field]; + } else { + return null; + } + } + if (is_array($text) && is_numeric($error) && $error > 0) { $error--; } diff --git a/cake/tests/cases/libs/view/helpers/form.test.php b/cake/tests/cases/libs/view/helpers/form.test.php index f5684e4c0..371f04f90 100644 --- a/cake/tests/cases/libs/view/helpers/form.test.php +++ b/cake/tests/cases/libs/view/helpers/form.test.php @@ -533,6 +533,21 @@ class FormHelperTest extends CakeTestCase { $this->assertPattern('/]*class="error-message"[^<>]*>You must provide a URL<\/div>/', $result); } + function testMultipleInputValidation() { + $this->Form->create(); + $this->Form->validationErrors['Address'][0]['title'] = 'This field cannot be empty'; + $this->Form->validationErrors['Address'][0]['first_name'] = 'This field cannot be empty'; + $this->Form->validationErrors['Address'][1]['last_name'] = 'You must have a last name'; + $result = $this->Form->input('Address.0.title'); + $this->assertPattern('/]*class="error-message"[^<>]*>This field cannot be empty<\/div>/', $result); + $result = $this->Form->input('Address.0.first_name'); + $this->assertPattern('/]*class="error-message"[^<>]*>This field cannot be empty<\/div>/', $result); + $result = $this->Form->input('Address.0.last_name'); + $this->assertNoPattern('/]*class="error-message"[^<>]*>.*<\/div>/', $result); + $result = $this->Form->input('Address.1.last_name'); + $this->assertPattern('/]*class="error-message"[^<>]*>You must have a last name<\/div>/', $result); + } + function testFormInput() { $result = $this->Form->input('Contact.email', array('id' => 'custom')); $expected = '
';