Contao\Widget::validator PHP Method

validator() protected method

Recursively validate an input variable
protected validator ( mixed $varInput ) : mixed
$varInput mixed The user input
return mixed The original or modified user input
    protected function validator($varInput)
    {
        if (is_array($varInput)) {
            foreach ($varInput as $k => $v) {
                $varInput[$k] = $this->validator($v);
            }
            return $varInput;
        }
        if (!$this->doNotTrim) {
            $varInput = trim($varInput);
        }
        if ($varInput == '') {
            if (!$this->mandatory) {
                return '';
            } else {
                if ($this->strLabel == '') {
                    $this->addError($GLOBALS['TL_LANG']['ERR']['mdtryNoLabel']);
                } else {
                    $this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['mandatory'], $this->strLabel));
                }
            }
        }
        if ($this->minlength && $varInput != '' && Utf8::strlen($varInput) < $this->minlength) {
            $this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['minlength'], $this->strLabel, $this->minlength));
        }
        if ($this->maxlength && $varInput != '' && Utf8::strlen($varInput) > $this->maxlength) {
            $this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['maxlength'], $this->strLabel, $this->maxlength));
        }
        if ($this->minval && is_numeric($varInput) && $varInput < $this->minval) {
            $this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['minval'], $this->strLabel, $this->minval));
        }
        if ($this->maxval && is_numeric($varInput) && $varInput > $this->maxval) {
            $this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['maxval'], $this->strLabel, $this->maxval));
        }
        if ($this->rgxp != '') {
            switch ($this->rgxp) {
                // Special validation rule for style sheets
                case strncmp($this->rgxp, 'digit_', 6) === 0:
                    $textual = explode('_', $this->rgxp);
                    array_shift($textual);
                    if (in_array($varInput, $textual) || strncmp($varInput, '$', 1) === 0) {
                        break;
                    }
                    // DO NOT ADD A break; STATEMENT HERE
                    // Numeric characters (including full stop [.] and minus [-])
                // DO NOT ADD A break; STATEMENT HERE
                // Numeric characters (including full stop [.] and minus [-])
                case 'digit':
                    // Support decimal commas and convert them automatically (see #3488)
                    if (substr_count($varInput, ',') == 1 && strpos($varInput, '.') === false) {
                        $varInput = str_replace(',', '.', $varInput);
                    }
                    if (!\Validator::isNumeric($varInput)) {
                        $this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['digit'], $this->strLabel));
                    }
                    break;
                    // Natural numbers (positive integers)
                // Natural numbers (positive integers)
                case 'natural':
                    if (!\Validator::isNatural($varInput)) {
                        $this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['natural'], $this->strLabel));
                    }
                    break;
                    // Alphabetic characters (including full stop [.] minus [-] and space [ ])
                // Alphabetic characters (including full stop [.] minus [-] and space [ ])
                case 'alpha':
                    if (!\Validator::isAlphabetic($varInput)) {
                        $this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['alpha'], $this->strLabel));
                    }
                    break;
                    // Alphanumeric characters (including full stop [.] minus [-], underscore [_] and space [ ])
                // Alphanumeric characters (including full stop [.] minus [-], underscore [_] and space [ ])
                case 'alnum':
                    if (!\Validator::isAlphanumeric($varInput)) {
                        $this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['alnum'], $this->strLabel));
                    }
                    break;
                    // Do not allow any characters that are usually encoded by class Input ([#<>()\=])
                // Do not allow any characters that are usually encoded by class Input ([#<>()\=])
                case 'extnd':
                    if (!\Validator::isExtendedAlphanumeric(html_entity_decode($varInput))) {
                        $this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['extnd'], $this->strLabel));
                    }
                    break;
                    // Check whether the current value is a valid date format
                // Check whether the current value is a valid date format
                case 'date':
                    if (!\Validator::isDate($varInput)) {
                        $this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['date'], \Date::getInputFormat(\Date::getNumericDateFormat())));
                    } else {
                        // Validate the date (see #5086)
                        try {
                            new \Date($varInput, \Date::getNumericDateFormat());
                        } catch (\OutOfBoundsException $e) {
                            $this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['invalidDate'], $varInput));
                        }
                    }
                    break;
                    // Check whether the current value is a valid time format
                // Check whether the current value is a valid time format
                case 'time':
                    if (!\Validator::isTime($varInput)) {
                        $this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['time'], \Date::getInputFormat(\Date::getNumericTimeFormat())));
                    }
                    break;
                    // Check whether the current value is a valid date and time format
                // Check whether the current value is a valid date and time format
                case 'datim':
                    if (!\Validator::isDatim($varInput)) {
                        $this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['dateTime'], \Date::getInputFormat(\Date::getNumericDatimFormat())));
                    } else {
                        // Validate the date (see #5086)
                        try {
                            new \Date($varInput, \Date::getNumericDatimFormat());
                        } catch (\OutOfBoundsException $e) {
                            $this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['invalidDate'], $varInput));
                        }
                    }
                    break;
                    // Check whether the current value is a valid friendly name e-mail address
                // Check whether the current value is a valid friendly name e-mail address
                case 'friendly':
                    list($strName, $varInput) = \StringUtil::splitFriendlyEmail($varInput);
                    // no break;
                    // Check whether the current value is a valid e-mail address
                // no break;
                // Check whether the current value is a valid e-mail address
                case 'email':
                    if (!\Validator::isEmail($varInput)) {
                        $this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['email'], $this->strLabel));
                    }
                    if ($this->rgxp == 'friendly' && !empty($strName)) {
                        $varInput = $strName . ' [' . $varInput . ']';
                    }
                    break;
                    // Check whether the current value is list of valid e-mail addresses
                // Check whether the current value is list of valid e-mail addresses
                case 'emails':
                    $arrEmails = \StringUtil::trimsplit(',', $varInput);
                    foreach ($arrEmails as $strEmail) {
                        $strEmail = \Idna::encodeEmail($strEmail);
                        if (!\Validator::isEmail($strEmail)) {
                            $this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['emails'], $this->strLabel));
                            break;
                        }
                    }
                    break;
                    // Check whether the current value is a valid URL
                // Check whether the current value is a valid URL
                case 'url':
                    if (!\Validator::isUrl($varInput)) {
                        $this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['url'], $this->strLabel));
                    }
                    break;
                    // Check whether the current value is a valid alias
                // Check whether the current value is a valid alias
                case 'alias':
                    if (!\Validator::isAlias($varInput)) {
                        $this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['alias'], $this->strLabel));
                    }
                    break;
                    // Check whether the current value is a valid folder URL alias
                // Check whether the current value is a valid folder URL alias
                case 'folderalias':
                    if (!\Validator::isFolderAlias($varInput)) {
                        $this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['folderalias'], $this->strLabel));
                    }
                    break;
                    // Phone numbers (numeric characters, space [ ], plus [+], minus [-], parentheses [()] and slash [/])
                // Phone numbers (numeric characters, space [ ], plus [+], minus [-], parentheses [()] and slash [/])
                case 'phone':
                    if (!\Validator::isPhone(html_entity_decode($varInput))) {
                        $this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['phone'], $this->strLabel));
                    }
                    break;
                    // Check whether the current value is a percent value
                // Check whether the current value is a percent value
                case 'prcnt':
                    if (!\Validator::isPercent($varInput)) {
                        $this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['prcnt'], $this->strLabel));
                    }
                    break;
                    // Check whether the current value is a locale
                // Check whether the current value is a locale
                case 'locale':
                    if (!\Validator::isLocale($varInput)) {
                        $this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['locale'], $this->strLabel));
                    }
                    break;
                    // Check whether the current value is a language code
                // Check whether the current value is a language code
                case 'language':
                    if (!\Validator::isLanguage($varInput)) {
                        $this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['language'], $this->strLabel));
                    }
                    break;
                    // Check whether the current value is a Google+ ID or vanity name
                // Check whether the current value is a Google+ ID or vanity name
                case 'google+':
                    if (!\Validator::isGooglePlusId($varInput)) {
                        $this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['invalidGoogleId'], $this->strLabel));
                    }
                    break;
                    // Check whether the current value is a field name
                // Check whether the current value is a field name
                case 'fieldname':
                    if (!\Validator::isFieldName($varInput)) {
                        $this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['invalidFieldName'], $this->strLabel));
                    }
                    break;
                    // HOOK: pass unknown tags to callback functions
                // HOOK: pass unknown tags to callback functions
                default:
                    if (isset($GLOBALS['TL_HOOKS']['addCustomRegexp']) && is_array($GLOBALS['TL_HOOKS']['addCustomRegexp'])) {
                        foreach ($GLOBALS['TL_HOOKS']['addCustomRegexp'] as $callback) {
                            $this->import($callback[0]);
                            $break = $this->{$callback[0]}->{$callback[1]}($this->rgxp, $varInput, $this);
                            // Stop the loop if a callback returned true
                            if ($break === true) {
                                break;
                            }
                        }
                    }
                    break;
            }
        }
        if ($this->isHexColor && $varInput != '' && strncmp($varInput, '$', 1) !== 0) {
            $varInput = preg_replace('/[^a-f0-9]+/i', '', $varInput);
        }
        if ($this->nospace && preg_match('/[\\t ]+/', $varInput)) {
            $this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['noSpace'], $this->strLabel));
        }
        if ($this->spaceToUnderscore) {
            $varInput = preg_replace('/\\s+/', '_', trim($varInput));
        }
        if (is_bool($this->trailingSlash) && $varInput != '') {
            $varInput = preg_replace('/\\/+$/', '', $varInput) . ($this->trailingSlash ? '/' : '');
        }
        return $varInput;
    }

Usage Example

Exemplo n.º 1
0
 public function validator($varInput)
 {
     return parent::validator($varInput);
 }
All Usage Examples Of Contao\Widget::validator