Phalcon\Validation\Validator\Decimal::validate PHP Method

validate() public method

public validate ( Phalcon\Validation $validation, string $attribute ) : boolean
$validation Phalcon\Validation
$attribute string
return boolean
    public function validate(Validation $validation, $attribute)
    {
        $value = $validation->getValue($attribute);
        $field = $this->getOption('label');
        if (empty($field)) {
            $validation->getLabel($attribute);
        }
        if (false === $this->hasOption('places')) {
            throw new Exception('A number of decimal places must be set');
        }
        if ($this->hasOption('digits')) {
            // Specific number of digits
            $digits = '{' . (int) $this->getOption('digits') . '}';
        } else {
            // Any number of digits
            $digits = '+';
        }
        if ($this->hasOption('point')) {
            $decimal = $this->getOption('point');
        } else {
            // Get the decimal point for the current locale
            list($decimal) = array_values(localeconv());
        }
        $result = (bool) preg_match(sprintf('#^[+-]?[0-9]%s%s[0-9]{%d}$#', $digits, preg_quote($decimal), $this->getOption('places')), $value);
        if (!$result) {
            $message = $this->getOption('message');
            $replacePairs = [':field' => $field];
            if (empty($message)) {
                $message = ':field must contain valid decimal value';
            }
            $validation->appendMessage(new Message(strtr($message, $replacePairs), $attribute, 'Decimal'));
            return false;
        }
        return true;
    }
Decimal