{"id":202,"date":"2021-06-11T11:55:44","date_gmt":"2021-06-11T11:55:44","guid":{"rendered":"https:\/\/magebrew.com\/blog\/?p=202"},"modified":"2021-09-05T11:57:41","modified_gmt":"2021-09-05T11:57:41","slug":"update-validation-rule-for-customer-address-attribute-in-magento-2","status":"publish","type":"post","link":"https:\/\/magebrew.com\/blog\/update-validation-rule-for-customer-address-attribute-in-magento-2\/","title":{"rendered":"Update validation rule for customer address attribute in Magento 2"},"content":{"rendered":"\n<p>Validation rules for customer address attributes  are stored in <em>customer_eav_attribute<\/em> table. Below is example of how to adjust a rule for specific attribute. Let&#8217;s say we want first name to be minimum 3 chars long. It is done via data patch.<\/p>\n\n\n\n<!--more-->\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\ndeclare(strict_types=1);\n\nnamespace Magento\\Customer\\Setup\\Patch\\Data;\n\nuse Magento\\Customer\\Setup\\CustomerSetup;\nuse Magento\\Customer\\Setup\\CustomerSetupFactory;\nuse Magento\\Framework\\Setup\\ModuleDataSetupInterface;\nuse Magento\\Framework\\Setup\\Patch\\DataPatchInterface;\n\nclass UpdateCustomerAddressAttributeValidationRule implements DataPatchInterface\n{\n    \/**\n     * @var ModuleDataSetupInterface\n     *\/\n    private $moduleDataSetup;\n\n    \/**\n     * @var CustomerSetupFactory\n     *\/\n    private $customerSetupFactory;\n\n    \/**\n     * UpdateCustomerAddressAttributeValidationRule constructor.\n     * @param ModuleDataSetupInterface $moduleDataSetup\n     * @param CustomerSetupFactory $customerSetupFactory\n     *\/\n    public function __construct(\n        ModuleDataSetupInterface $moduleDataSetup,\n        CustomerSetupFactory $customerSetupFactory\n    ) {\n        $this->moduleDataSetup = $moduleDataSetup;\n        $this->customerSetupFactory = $customerSetupFactory;\n    }\n\n    \/**\n     * @inheritDoc\n     *\/\n    public function apply()\n    {\n        \/** @var CustomerSetup $customerSetup *\/\n        $customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);\n        $this->updateCustomerAddressAttributeValidationRule($customerSetup);\n\n        return $this;\n    }\n\n    \/**\n     * @inheritDoc\n     *\/\n    public function getAliases()\n    {\n        return [];\n    }\n\n    \/**\n     * @inheritDoc\n     *\/\n    public static function getDependencies()\n    {\n        return [\n            DefaultCustomerGroupsAndAttributes::class,\n        ];\n    }\n\n    \/**\n     * Update customer address validation rule\n     *\n     * @param CustomerSetup $customerSetup\n     *\n     * @return void\n     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)\n     *\/\n    private function updateCustomerAddressAttributeValidationRule($customerSetup)\n    {\n        $entityAttributes = [\n            'customer_address' => [\n                'firstname' => [\n                    'validate_rules' => '{\"max_text_length\":255,\"min_text_length\":3}'\n                ]\n            ],\n        ];\n\n        $customerSetup->upgradeAttributes($entityAttributes);\n    }\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Validation rules for customer address attributes are stored in customer_eav_attribute table. Below is example of how to adjust a rule for specific attribute. Let&#8217;s say we want first name to be minimum 3 chars long. It is done via data patch.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[19],"tags":[28,20],"_links":{"self":[{"href":"https:\/\/magebrew.com\/blog\/wp-json\/wp\/v2\/posts\/202"}],"collection":[{"href":"https:\/\/magebrew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/magebrew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/magebrew.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/magebrew.com\/blog\/wp-json\/wp\/v2\/comments?post=202"}],"version-history":[{"count":3,"href":"https:\/\/magebrew.com\/blog\/wp-json\/wp\/v2\/posts\/202\/revisions"}],"predecessor-version":[{"id":206,"href":"https:\/\/magebrew.com\/blog\/wp-json\/wp\/v2\/posts\/202\/revisions\/206"}],"wp:attachment":[{"href":"https:\/\/magebrew.com\/blog\/wp-json\/wp\/v2\/media?parent=202"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/magebrew.com\/blog\/wp-json\/wp\/v2\/categories?post=202"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/magebrew.com\/blog\/wp-json\/wp\/v2\/tags?post=202"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}