Validates date string against known formats

@param string $data
@return boolean
3556
private function isValidFormat($date)
    {
        foreach ($this->formats as $format) {
            if (date($format, strtotime($date)) == $date) {
                return true;
            }
        }

        return false;
    }
599308
public static function isValidDateFormat($format)
    {
        if (empty($format) || !is_string($format)) {
            return false;
        }

        $formatted = (new DateTime())->format($format);

        // Formatted date it's the format who is validated?
        // The format is invalid
        if ($formatted === $format) {
            return false;
        }

        // Validate the format used to create the datetime
        $fromFormat = DateTime::createFromFormat($format, $formatted);

        // It's instance of DateTime?
        // The format is valid
        if ($fromFormat instanceof DateTime) {
            return true;
        }

        return $fromFormat instanceof DateTime;
    }
154659
public function validPasswords($data) {
		$field = key($data);
		$data = reset($data);
		if (empty($data) || empty($field)) {
			return false;
		}

		$controlField = $field . '_confirm';
		if (!isset($this->data[$this->alias][$controlField])) {
			return false;
		}

		if ($data !== $this->data[$this->alias][$controlField]) {
			return false;
		}

		return true;
	}
385913
protected function validateDataFromCache($data): bool
    {
        if (!\is_array($data)) {
            return false;
        }

        foreach (['value', 'ttl'] as $missing) {
            if (!\array_key_exists($missing, $data)) {
                return false;
            }
        }

        return true;
    }
370655
static public function checkDate(string $date, string $format = 'Y-m-d'): bool
    {
        $d = DateTime::createFromFormat($format, $date);

        return $d && $d->format($format) == $date;
    }
924696
public function validate($data) {
        if (empty($data)) {
            return true;
        }

        try {
            \core_auth\digital_consent::parse_age_digital_consent_map($data);
        } catch (\moodle_exception $e) {
            return get_string('invalidagedigitalconsent', 'admin', $e->getMessage());
        }

        return true;
    }
1086059
protected function invalidPayload($data)
    {
        return ! is_array($data) || ! isset($data['iv']) || ! isset($data['value']) || ! isset($data['mac']);
    }
270178
public function validate($value)
    {
        $date = \DateTime::createFromFormat($this->specification, $value);

        return $date !== false;
    }
1076240
public static function hasFormat($date, $format)
    {
        try {
            // Try to create a DateTime object. Throws an InvalidArgumentException if the provided time string
            // doesn't match the format in any way.
            static::rawCreateFromFormat($format, $date);

            // createFromFormat() is known to handle edge cases silently.
            // E.g. "1975-5-1" (Y-n-j) will still be parsed correctly when "Y-m-d" is supplied as the format.
            // To ensure we're really testing against our desired format, perform an additional regex validation.

            // Preg quote, but remove escaped backslashes since we'll deal with escaped characters in the format string.
            $quotedFormat = str_replace('\\\\', '\\',
                preg_quote($format, '/'));

            // Build the regex string
            $regex = '';
            for ($i = 0; $i < strlen($quotedFormat); ++$i) {
                // Backslash – the next character does not represent a date token so add it on as-is and continue.
                // We're doing an extra ++$i here to increment the loop by 2.
                if ($quotedFormat[$i] === '\\') {
                    $regex .= '\\'.$quotedFormat[++$i];
                    continue;
                }

                $regex .= strtr($quotedFormat[$i], static::$regexFormats);
            }

            return (bool) preg_match('/^'.$regex.'$/', $date);
        } catch (InvalidArgumentException $e) {
        }

        return false;
    }
308720
public function validate(array $data)
    {
        $errors = [];

        if (empty($data['name'])) {
            $errors[] = ['path' => '/name', 'message' => 'name can not be empty.'];
        }

        if (empty($data['type'])) {
            $errors[] = ['path' => '/type', 'message' => 'type can not be empty.'];
        }

        if (empty($data['scheduledDate'])) {
            $errors[] = ['path' => '/scheduledDate', 'message' => 'scheduledDate can not be empty.'];
        }

        return $errors;
    }
1079042
public static function valid_date($date = null, $format = 'DD/MM/YYYY')
    {
        if ($date == null) {
            return false;
        }
        $lendate = strlen($date);
        if ($lendate > 10) {
            $date = substr($date, 0, 10);
            $lendate = strlen($date);
        }
        
        if ($lendate >= 8 && $lendate <= 10) {
            $regexp = $format;
            $regexp = str_replace('DD', '(?<D>\d{2})', $regexp);
            $regexp = str_replace('MM', '(?<M>\d{2})', $regexp);
            $regexp = str_replace('YYYY', '(?<Y>\d{4})', $regexp);
            
            if (preg_match("/$regexp/", $date, $aDate)) {
                $year = ($aDate['Y'] < 100) ? 2000 + $aDate['Y'] : $aDate['Y'];
                $month = $aDate['M'];
                $day = $aDate['D'];
                
                if (self::checkdate($month, $day, $year)) {
                    return mktime(0, 0, 0, $month, $day, $year);
                }
                else {
                    return false;
                }
            }
            else {
                return false;
            }
        }
        else {
            return false;
        }
    }
778433
private function _validateColumn(array $data)
    {
        if (!isset($data['label'])) {
            return 'Label key is required.';
        }
        if (!isset($data['index'])) {
            return 'Index key is required.';
        }

        if (isset($data['renderer']) && (!$data['renderer'] instanceof \Closure && !is_string($data['renderer']))) {
            return 'Renderer must be a string or instance of Closure object.';
        }

        if (isset($data['filter']) && !$data['filter'] instanceof \Closure) {
            return 'Filter function must be instance of Closure object.';
        }

        return true;
    }
902743
public function php($data)
    {
        $valid = true;
        $fields = $this->form->Fields();

        foreach ($fields as $field) {
            $valid = ($field->validate($this) && $valid);
        }

        if (!$this->required) {
            return $valid;
        }

        foreach ($this->required as $fieldName) {
            if (!$fieldName) {
                continue;
            }

            if ($fieldName instanceof FormField) {
                $formField = $fieldName;
                $fieldName = $fieldName->getName();
            } else {
                $formField = $fields->dataFieldByName($fieldName);
            }

            // submitted data for file upload fields come back as an array
            $value = isset($data[$fieldName]) ? $data[$fieldName] : null;

            if (is_array($value)) {
                if ($formField instanceof FileField && isset($value['error']) && $value['error']) {
                    $error = true;
                } else {
                    $error = (count($value)) ? false : true;
                }
            } else {
                // assume a string or integer
                $error = (strlen($value)) ? false : true;
            }

            if ($formField && $error) {
                $errorMessage = _t(
                    'SilverStripe\\Forms\\Form.FIELDISREQUIRED',
                    '{name} is required',
                    array(
                        'name' => strip_tags(
                            '"' . ($formField->Title() ? $formField->Title() : $fieldName) . '"'
                        )
                    )
                );

                if ($msg = $formField->getCustomValidationMessage()) {
                    $errorMessage = $msg;
                }

                $this->validationError(
                    $fieldName,
                    $errorMessage,
                    "required"
                );

                $valid = false;
            }
        }

        return $valid;
    }
969380
function validSamlDateFormat($samlDate) 
   {
      if ($samlDate == "") return false;
      $indexT = strpos($samlDate, 'T');
      $indexZ = strpos($samlDate, 'Z');
      if (($indexT != 10) || ($indexZ != 19)) {
         return false;
      }
      $dateString = substr($samlDate, 0, 10);
      $timeString = substr($samlDate, $indexT + 1, 8);
      list($year, $month, $day) = explode('-', $dateString);
      list($hour, $minute, $second) = explode(':', $timeString);
      $parsedDate = gmmktime($hour, $minute, $second, $month, $day, $year);
      if (($parsedDate === false) || ($parsedDate == -1)) return false;
      if (!checkdate($month, $day, $year)) return false;
      return $parsedDate;
   }
523241
public static function Validate($date, $format = DATEFORMAT::YMD, $separator = "/")
	{
		$timestamp = DateUtil::TimeStampFromStr($date, $format);
		$dateCheck = DateUtil::FormatDate($timestamp, $format, $separator, true);

		$date = $date . substr('--/--/---- 00:00:00', strlen($date));

		$timestamp2 = DateUtil::TimeStampFromStr($dateCheck, $format);
		return ($timestamp == $timestamp2) && ($date == $dateCheck);
	}
17430
public function validate($input)
    {
        switch (strlen($input)) {
            // YYMMDDAABB
            case 10:
                $parsed = $input;
                break;

            case 11: // YYMMDD[-+]AABB
                $parsed = str_replace(array('-', '+'), '', $input);
                break;

            // YYYYMMDDAABB
            case 12:
                $parsed = substr($input, 2);
                break;

            case 13: // YYYYYYMMDD[-+]AABB
                $parsed = substr($input, 2);
                $parsed = str_replace(array('-', '+'), '', $parsed);
                break;

            default:
                return false;
        }

        if (!preg_match('/^\d+$/', $parsed)) {
            return false;
        }

        // Use DateTime to parse the date and see if it changes
        $date     = substr($parsed, 0, 6);
        $dateTime = \DateTime::createFromFormat('ymd', $date);

        // If it changes, it wasn't a valid date (since 991232 will yield 000101)
        if ($dateTime->format('ymd') !== $date) {
            return false;
        }

        // Get check number
        $check  = intval(substr($parsed, -1, 1));
        $parsed = substr($parsed, 0, 9);

        $result = 0;

        // Calculate check number
        for ($i = 0; $i < strlen($parsed); $i++) {

            if (($i % 2) == 0) {
                $number = intval($parsed[$i] * 2);
            } else {
                $number = intval($parsed[$i]);
            }

            if ($number > 9) {
                $result += (1 + ($number % 10));
            } else {
                $result += $number;
            }
        }

        return ((($check + $result) % 10) == 0);
    }
1326518
protected function isValidConditionData($data)
    {
        if (!array_key_exists('field', $data)) {
            throw new MissingConditionAttributeException('field');
        }
        if (!array_key_exists('operator', $data)) {
            throw new MissingConditionAttributeException('operator');
        }
        if (!array_key_exists('type', $data)) {
            throw new MissingConditionAttributeException('type');
        }
        if (!array_key_exists('value', $data)) {
            throw new MissingConditionAttributeException('value');
        }

        return true;
    }