PHP message: PHP Fatal error: Uncaught TypeError: Argument 2 passed to Magento\Eav\Model\Attribute\Data\Text::validateLength() must be of the type string, null given code example

Example: PHP message: PHP Fatal error: Uncaught TypeError: Argument 2 passed to Magento\Eav\Model\Attribute\Data\Text::validateLength() must be of the type string, null given

public function validateValue($value)
{
    $errors = [];
    $attribute = $this->getAttribute();

    if ($value === false) {
        // try to load original value and validate it
        $value = $this->getEntity()->getDataUsingMethod($attribute->getAttributeCode());
    }

    if (!$attribute->getIsRequired() && empty($value)) {
        return true;
    }

    if (empty($value) && $value !== '0' && $attribute->getDefaultValue() === null) {
        $label = __($attribute->getStoreLabel());
        $errors[] = __('"%1" is a required value.', $label);
    }

    $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/validatelog.log');
    $logger = new \Zend\Log\Logger();
    $logger->addWriter($writer);
    $logger->info($attribute->getName());

    $validateLengthResult = $this->validateLength($attribute, $value);
    $errors = array_merge($errors, $validateLengthResult);

    $validateInputRuleResult = $this->validateInputRule($value);
    $errors = array_merge($errors, $validateInputRuleResult);

    if (count($errors) == 0) {
        return true;
    }

    return $errors;
}

Tags:

Misc Example