Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use localized field-names in error- and validation-messages. #383

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion code/account/ShopAccountForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"
);
Expand Down
10 changes: 7 additions & 3 deletions code/account/ShopMemberFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
6 changes: 5 additions & 1 deletion code/checkout/components/AddressBookCheckoutComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 6 additions & 2 deletions code/checkout/components/MembershipCheckoutComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* - member identifier, and password fields.
* - required membership fields
* - validating data
*
*
*/
class MembershipCheckoutComponent extends CheckoutComponent{

Expand Down Expand Up @@ -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
);
}
Expand Down