• Модуль: iblock
  • Путь к файлу: ~/bitrix/modules/iblock/lib/orm/commonelementtable.php
  • Класс: BitrixIblockORMCommonElementTable
  • Вызов: CommonElementTable::onBeforeAdd
static function onBeforeAdd(Event $event)
{
	$object = $event->getParameter('object');
	$fields = static::getEntity()->getFields();

	$result = new EventResult;

	foreach ($fields as $field)
	{
		// check required properties
		$hasEmptyRequiredValue = false;

		if ($field instanceof PropertyReference || $field instanceof PropertyOneToMany)
		{
			$property = $field->getIblockElementProperty();

			if ($property->getIsRequired())
			{
				/** @var ValueStorage $valueContainer */
				$valueContainer = $object->get($field->getName());

				if (empty($valueContainer))
				{
					$hasEmptyRequiredValue = true;
				}

				// check with GetLength
				if ($valueContainer instanceof ValueStorage)
				{
					$userType = CIBlockProperty::GetUserType($property->getUserType());

					if(array_key_exists("GetLength", $userType))
					{
						$length = call_user_func_array(
							$userType["GetLength"],
							[
								$property->collectValues(),
								["VALUE" => $valueContainer->getValue()]
							]
						);
					}
					else
					{
						$length = mb_strlen($valueContainer->getValue());
					}

					$hasEmptyRequiredValue = ($length <= 0);
				}


				if ($hasEmptyRequiredValue)
				{
					$result->addError(new FieldError(
						$field,
						Loc::getMessage(
							"MAIN_ENTITY_FIELD_REQUIRED",
							["#FIELD#" => $property->getName()]
						),
						FieldError::EMPTY_REQUIRED
					));
				}
			}
		}
	}

	return $result;
}