Add Custom Validation Street Address Field in backend Magento 2
-
04-03-2021 - |
Вопрос
I Want To Set Minimum Length Value For Street Address Field On Admin Side
I want to set min_text_length = 5
For Street Address
Field In Customer Address Form
I am Trying To Update Attribute but Not Working
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
if (version_compare($context->getVersion(), '1.0.1', '<')) {
$customerSetup = $this->customerSetup->create(['setup' => $setup]);
$customerSetup->addAttribute(
'customer_address',
'street',
[
'type' => 'static',
'label' => 'Street Address',
'input' => 'multiline',
'backend' => \Magento\Eav\Model\Entity\Attribute\Backend\DefaultBackend::class,
'sort_order' => 70,
'multiline_count' => 4,
'validate_rules' => '{"max_text_length":255,"min_text_length":5}',
'position' => 70,
]
);
}
}
In Database Already Set {"max_text_length":255,"min_text_length":5}
But Validation not Working
Note :- Admin Side Not Frontend Side.
Решение
You Can do it by Script and update validation within it.You have to change
'validate_rules' => '{"max_text_length":255,"min_text_length":5}',
You can change it directly from DB you have to do it carefully.
- Open table
eav_attribute
and findattribute_code
street and getattribute_id
from it (generally attribute_id is 28).
- Open table
customer_eav_attribute
and find thatattribute_id
(28 in my case). update invalidate_rules
column{"max_text_length":255,"min_text_length":5}
Note: You have to remove if it occurs in front
Другие советы
i am not sure if this works
<rule name="required-entry" xsi:type="boolean">true</rule>
Example from first name
<field name="city" formElement="input">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="source" xsi:type="string">address</item>
</item>
</argument>
<settings>
remove this validation
<validation>
<rule name="required-entry" xsi:type="boolean">true</rule>
</validation>
up to here
<dataType>text</dataType>
</settings>
</field>
Не связан с magento.stackexchange