RockLobsterInc\Swv\Rules

MaxFilesizeRule::validate │ public │ CF7 1.0

Validates the form data according to the logic defined by this rule.

Метод класса: MaxFilesizeRule{}

Хуков нет.

Возвращает

null. Ничего (null).

Использование

$MaxFilesizeRule = new MaxFilesizeRule();
$MaxFilesizeRule->validate( $form_data, $context );
$form_data(FormDataTree) (обязательный)
Form data.
$context(массив)
Optional context.
По умолчанию: []

Код MaxFilesizeRule::validate() CF7 6.2.4.1

public function validate(FormDataTree $form_data, array $context = [])
{
    $files = $form_data->getAllFiles($this->field);
    $files = array_flatten($files);

    if (empty($files)) {
        return true;
    }

    $file_size = array_reduce(
        $files,
        static function ($carry, $item) {
            $carry += $item->size();
            return $carry;
        },
        0,
    );

    if ((int) $this->threshold < $file_size) {
        throw new Invalidity($this, ["cause" => $file_size]);
    }

    return true;
}