how to add custom rule from js for firstname in magento2?

                    <rule name="required-entry" xsi:type="boolean">true</rule>

this is core validation.I need to add my validation

                    <rule name="custom-validation" xsi:type="boolean">true</rule>
                    <class name="additionalClasses">true</class>
                    <class name="my-custom-class">true</class>

this validation is works when replace the core validation for firstname.But its not working on custom module. Any help ?

Thanks in Advance

1 Answer 1


The required entry is just an option to make the field mandatory threw Ui components. Nothing more. It doesn't add any kind of validation or control.

If you need to control really the field, then what you might want to do is to go straight to the controller that handle your entity saving data.

Ex :


CONST PARAMS_TO_SAVE = ['store_id','product_id','sort_order'];

public function execute()
    $entity = $this->yourEntityFactory->create();
    try {

       //assuming id is your entity identifier
        $id = $this->_request->getParam('id');
        if ($id) $entity->load($id);

        foreach (self::PARAMS_TO_SAVE as $paramName) {
            //This is the step where you set the data and can control it
            $entity->setData($paramName, $this->_request->getParam($paramName));

  • Thanks Claims, on this where we control mandatory fields.
    – Ajith
    Commented Feb 3, 2022 at 11:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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