From 1a4212602fe2ca63701bbc6c742dc3531615cd3d Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Thu, 17 Jul 2008 21:58:59 +0000 Subject: [PATCH] BUGFIX (by cinek) PHP validation field name for [Custom]RequiredField: Server side validation displays field name instead of field label in messages, e.g. "EditableEmailField?1 is required" instead of e.g. "E-mail address is required" for a field with label "Email address". (patch from #2560) git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@58305 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- forms/CustomRequiredFields.php | 20 +++++++++++--------- forms/RequiredFields.php | 10 +++++----- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/forms/CustomRequiredFields.php b/forms/CustomRequiredFields.php index a03ae5951..78547f336 100755 --- a/forms/CustomRequiredFields.php +++ b/forms/CustomRequiredFields.php @@ -32,7 +32,7 @@ class CustomRequiredFields extends RequiredFields{ if(is_array($this->required)){ foreach($this->required as $field) { - if(is_array($field) && $field['js']){ + if(is_array($field) && isset($field['js'])){ $code .= $field['js'] . "\n"; }else if($fields->dataFieldByName($field)) { $code .= " require('$field');\n"; @@ -56,15 +56,17 @@ class CustomRequiredFields extends RequiredFields{ $valid = ($field->validate($this) && $valid); } if($this->required){ - foreach($this->required as $key => $field) { - if(is_array($field) && $field['php']){ - eval($field['php']); - }else if($fields->dataFieldByName($field)) { + foreach($this->required as $key => $fieldName) { + $formField = $fields->dataFieldByName($fieldName); + if(is_array($fieldName) && isset($fieldName['php'])){ + eval($fieldName['php']); + }else if($formField) { // if an error is found, the form is returned. - if(!$data[$field] || preg_match('/^\s*$/', $data[$field])) { + if(!$data[$fieldName] || preg_match('/^\s*$/', $data[$fieldName])) { $this->validationError( - $field, - sprintf(_t('Form.FIELDISREQUIRED', "%s is required"), $field), + $fieldName, + sprintf(_t('Form.FIELDISREQUIRED', "%s is required"), + $formField->Title()), "required" ); return false; @@ -83,4 +85,4 @@ class CustomRequiredFields extends RequiredFields{ } } -?> +?> \ No newline at end of file diff --git a/forms/RequiredFields.php b/forms/RequiredFields.php index 4c9e86d5e..d01384424 100755 --- a/forms/RequiredFields.php +++ b/forms/RequiredFields.php @@ -95,14 +95,14 @@ JS; $valid = ($field->validate($this) && $valid); } if($this->required) { - foreach($this->required as $field) { - $fieldObj = $fields->dataFieldByName($field); - if($fieldObj && !$data[$field]) { + foreach($this->required as $fieldName) { + $formField = $fields->dataFieldByName($fieldName); + if($formField && !$data[$fieldName]) { $this->validationError( - $field, + $fieldName, sprintf( _t('Form.FIELDISREQUIRED'), - strip_tags($fieldObj->Title()) + strip_tags($formField->Title()) ), "required" );