PrestaShop 1.7结账时强制显示并必填地址别名字段求助
Hey there! I’ve been through similar PrestaShop customization headaches before, so let’s break down how to make the address alias field required and visible during checkout step-by-step:
1. Lock in the Frontend (You’re Already Halfway There!)
You mentioned you’ve got the alias field showing up in address-form.tpl—now let’s make it frontend-required and add error handling:
<div class="form-group"> <label for="alias">{l s='Address alias'}</label> <input type="text" name="alias" id="alias" class="form-control" value="{$address->alias|default:''}" required > {if isset($errors.alias)} <div class="text-danger mt-1">{$errors.alias}</div> {/if} </div>
The required attribute triggers browser-level validation, and the error block will catch backend validation messages too.
2. Fix the Backend Validation (The Real Missing Piece)
Your hunch about CustomerAddressFormatter.php was close—but the actual form rules live in classes/form/CustomerAddressForm.php. This file defines which fields are included and required for address forms across the site (account pages and checkout).
Open this file, find the buildForm() method, and add the alias field to the fields array with required => true:
$this->fields_form = [ 'legend' => [ 'title' => $this->trans('Add an address', [], 'Shop.Theme.Checkout'), 'icon' => 'icon-map-marker', ], 'fields' => [ // Keep all existing fields (firstname, lastname, etc.) 'alias' => [ 'type' => 'text', 'label' => $this->trans('Address alias', [], 'Shop.Forms.Labels'), 'required' => true, 'maxlength' => 32, ], // Rest of the fields... ], 'submit' => [ 'title' => $this->trans('Save', [], 'Shop.Actions'), ], ];
This ensures the alias field is part of the checkout address form’s validation schema.
3. Double-Check Model-Level Validation
PrestaShop’s CustomerAddress model already has built-in validation for the alias field (in classes/CustomerAddress.php’s validateFields() method):
if (!Validate::isGenericName($this->alias)) { $this->errors[] = Tools::displayError('Invalid address alias.'); return false; }
Since we’ve marked the field as required in the form, this validation will automatically fire when the user submits an empty alias during checkout.
4. Clear Cache & Test
Don’t forget to clear PrestaShop’s cache (Advanced Parameters > Performance) to ensure your changes take effect. Then test the checkout flow:
- Try submitting an address without filling the alias field—you should get both browser and backend error messages.
- Verify the alias saves correctly when filled out.
This should align the checkout address form with the account page’s behavior, making the alias field visible and required for all address additions.
内容的提问来源于stack exchange,提问作者John Doe

