2

In magento module for the back-end part, for admin form edit tab we are using

addValues()

and

setValues()

I want to know difference between those 2 functions

  • 1
    In simple term setValues reset the value and addValue append the value – Murtuza Zabuawala Oct 9 '18 at 5:15
4

You can find both function's definitions in file vendor\magento\framework\Data\Form.php.

public function setValues($values)
{
    foreach ($this->_allElements as $element) {
        if (isset($values[$element->getId()])) {
            $element->setValue($values[$element->getId()]);
        } else {
            $element->setValue(null);
        }
    }
    return $this;
}

The setValues($values) function set values according to the array passed in it and set null for the keys which are not set.

Where addValues($values) function does the first part only means it only sets the keys which are available.

public function addValues($values)
{
    if (!is_array($values)) {
        return $this;
    }
    foreach ($values as $elementId => $value) {
        $element = $this->getElement($elementId);
        if ($element) {
            $element->setValue($value);
        }
    }
    return $this;
}

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.