Magento 2.4.5- Add required validation for city on checkout page
2 Answers
You can do this using overriding LayoutProcessor.
Create di.xml file to override LayoutProcessor and add code below:
<type name="Magento\Checkout\Block\Checkout\LayoutProcessor">
<plugin name="add-custom-city-validation" type="VendorName\ModuleName\Model\Checkout\LayoutProcessor" sortOrder="10"/>
Then after in overrided file put below code to make city field required:
namespace VendorName\ModuleName\Model\Checkout;
class LayoutProcessor
{
public function afterProcess(
\Magento\Checkout\Block\Checkout\LayoutProcessor $subject,
array $jsLayout
) {
$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
['shippingAddress']['children']['shipping-address-fieldset']['children']['city'] = [
'required' => true,
];
return $jsLayout;
}
}
You can use the Magento Default validations in the checkout page for the city
This is the list of these validations :
You can add Magento by default validation or custom Js Validation in the checkout Address Form
List of the Magento default validation rules :
validate-grouped-qty
validate-one-checkbox-required-by-name
validate-date-between
validate-dob
max-words
min-words
range-words
letters-with-basic-punc
alphanumeric
letters-only
no-whitespace
zip-range
integer
vinUS
dateITA
dateNL
time
time12h
phoneUS
phoneUK
mobileUK
stripped-min-length
email2
url2
credit-card-types
ipv4
ipv6
pattern
allow-container-className
You can also use this link for more details "
https://developer.adobe.com/commerce/frontend-core/guide/validations/rule-list/
If you need more help, you can ping me again