RockLobsterInc\Swv\Rules

MinLengthRule::validate │ public │ CF7 1.0

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

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

Хуков нет.

Возвращает

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

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

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

Код MinLengthRule::validate() CF7 6.2.4.1

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

    if (empty($values) or !is_numeric($this->threshold)) {
        return true;
    }

    $total = 0;

    foreach ($values as $value) {
        $length = count_code_units($value);

        if (false === $length) {
            // mbstring is not loaded.
            return true;
        }

        $total += $length;
    }

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

    return true;
}