0

I am trying to add a custom class for some validations and styling purposes in a Programatically created Product Attributes, I have tried frontend_class and class property but it didn't work.
Can anyone help me with that ?

4
  • where you want to display that attribute in frontend? Oct 30, 2019 at 10:25
  • on Add/Edit Product page
    – devZ
    Oct 30, 2019 at 10:33
  • Do you want in admin side? Oct 30, 2019 at 10:34
  • yes on Admin side
    – devZ
    Oct 30, 2019 at 10:35

1 Answer 1

1

With the help of Matthéo Geoffray, this is what I did to apply frontend validation for custom attributes.

[
    'type' => 'int',
    'backend' => '',
    'frontend' => '',
    'label' => 'XXXX',
    'input' => 'text',
    'frontend_class' => 'validate-greater-than-zero',
    'source' => '',
    'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
    'visible' => true,
    'required' => true,
    'user_defined' => false,
    'default' => 0,
    'searchable' => false,
    'filterable' => true,
    'comparable' => false,
    'visible_on_front' => false,
    'used_in_product_listing' => true,
    'unique' => false
]

This is the custom attribute in install script.

I added plugin in di.xml

<type name="Magento\Catalog\Ui\DataProvider\CatalogEavValidationRules">
      <plugin name="namespace_custom_validation_for_product_attribute" type="Namespace\Module\Model\Plugin\Product\ValidationRules"/>
</type>

Here is the plugin code.


namespace Namespace\Module\Model\Plugin\Product;

use Closure;

class ValidationRules
{

    /**
     * @param \Magento\Catalog\Ui\DataProvider\CatalogEavValidationRules $rulesObject
     * @param callable $proceed
     * @param \Magento\Catalog\Api\Data\ProductAttributeInterface $attribute,
     * @param array $data
     * @return array
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function aroundBuild(
        \Magento\Catalog\Ui\DataProvider\CatalogEavValidationRules $rulesObject,
        Closure $proceed,
        \Magento\Catalog\Api\Data\ProductAttributeInterface $attribute,
        array $data
    ){
        $rules = $proceed($attribute,$data);
        if($attribute->getAttributeCode() == 'xyz'){ //custom filter
            $validationClasses = explode(' ', $attribute->getFrontendClass());
            foreach ($validationClasses as $class) {
                $rules[$class] = true;
            }
        }
        return $rules;
    }
}

Basically in \Magento\Catalog\Ui\DataProvider\CatalogEavValidationRules, the method called mapRules only matches the frontend class against limited number of validation rules. To apply more validation rules we need to append rules using plugin.

9
  • will that be validating like JS or it will be Form Submit validation?
    – devZ
    Oct 30, 2019 at 10:35
  • it will validate like JS. (frontend validation) Oct 30, 2019 at 10:37
  • ok great and is it possible to add custom class as well ?
    – devZ
    Oct 30, 2019 at 10:39
  • you can find frontend class in script. you can add there Dont forget to mark it as solution. Oct 30, 2019 at 10:44
  • Thank you @Ali but it just only allows Default validation classes and not the custom rule or class
    – devZ
    Oct 30, 2019 at 11:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.