From c218231fed0e9cbd5d9153fc7ba7ba8b1d40d1db Mon Sep 17 00:00:00 2001 From: Roman Schmid Date: Thu, 29 Oct 2015 17:11:21 +0100 Subject: [PATCH] Use localized field-names in error- and validation-messages. --- code/account/ShopAccountForm.php | 7 ++++++- code/account/ShopMemberFactory.php | 10 +++++++--- .../components/AddressBookCheckoutComponent.php | 6 +++++- .../components/MembershipCheckoutComponent.php | 8 ++++++-- 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/code/account/ShopAccountForm.php b/code/account/ShopAccountForm.php index a8e95f24e..934c14dc6 100644 --- a/code/account/ShopAccountForm.php +++ b/code/account/ShopAccountForm.php @@ -84,12 +84,17 @@ public function php($data) { $currentmember = Member::currentUser(); //can't be taken if(DataObject::get_one('Member', "$field = '$uid' AND ID != ".$currentmember->ID)){ + // get localized field labels + $fieldLabels = $currentmember->fieldLabels(false); + // if a localized value exists, use this for our error-message + $fieldLabel = isset($fieldLabels[$field]) ? $fieldLabels[$field] : $field; + $this->validationError( $field, // re-use the message from checkout sprintf( _t("Checkout.MEMBEREXISTS", "A member already exists with the %s %s"), - $field, $uid + $fieldLabel, $uid ), "required" ); diff --git a/code/account/ShopMemberFactory.php b/code/account/ShopMemberFactory.php index ca8bd420f..e2bd9fc33 100644 --- a/code/account/ShopMemberFactory.php +++ b/code/account/ShopMemberFactory.php @@ -30,11 +30,15 @@ public function create($data) { throw new ValidationException($result); } $idval = $data[$idfield]; - if(ShopMember::get_by_identifier($idval)){ + if($member = ShopMember::get_by_identifier($idval)){ + // get localized field labels + $fieldLabels = $member->fieldLabels(false); + // if a localized value exists, use this for our error-message + $fieldLabel = isset($fieldLabels[$idfield]) ? $fieldLabels[$idfield] : $idfield; + $result->error(sprintf( _t("Checkout.MEMBEREXISTS", "A member already exists with the %s %s"), - _t("Member.".$idfield, $idfield), - $idval + $fieldLabel, $idval )); throw new ValidationException($result); } diff --git a/code/checkout/components/AddressBookCheckoutComponent.php b/code/checkout/components/AddressBookCheckoutComponent.php index be9fe5456..a65c73960 100644 --- a/code/checkout/components/AddressBookCheckoutComponent.php +++ b/code/checkout/components/AddressBookCheckoutComponent.php @@ -97,12 +97,16 @@ public function validateData(Order $order, array $data) { } else { // Otherwise, require the normal address fields $required = parent::getRequiredFields($order); + $addressLabels = singleton('Address')->fieldLabels(false); + foreach ($required as $fieldName) { if (empty($data[$fieldName])) { + // attempt to get the translated field name + $fieldLabel = isset($addressLabels[$fieldName]) ? $addressLabels[$fieldName] : $fieldName; $errorMessage = _t( 'Form.FIELDISREQUIRED', '{name} is required', - array('name' => $fieldName) + array('name' => $fieldLabel) ); $result->error($errorMessage, $fieldName); diff --git a/code/checkout/components/MembershipCheckoutComponent.php b/code/checkout/components/MembershipCheckoutComponent.php index 29e702d7a..b6de55039 100644 --- a/code/checkout/components/MembershipCheckoutComponent.php +++ b/code/checkout/components/MembershipCheckoutComponent.php @@ -5,7 +5,7 @@ * - member identifier, and password fields. * - required membership fields * - validating data - * + * */ class MembershipCheckoutComponent extends CheckoutComponent{ @@ -72,10 +72,14 @@ public function validateData(Order $order, array $data) { $idfield = Member::config()->unique_identifier_field; $idval = $data[$idfield]; if(ShopMember::get_by_identifier($idval)){ + // get localized field labels + $fieldLabels = $member->fieldLabels(false); + // if a localized value exists, use this for our error-message + $fieldLabel = isset($fieldLabels[$idfield]) ? $fieldLabels[$idfield] : $idfield; $result->error( sprintf( _t("Checkout.MEMBEREXISTS", "A member already exists with the %s %s"), - $idfield, $idval + $fieldLabel, $idval ), $idval ); }