Validation.

@param array $data
@param array $files
@return array the errors that were found
163719
public function validation($data, $files) {
        global $DB;

        $errors = parent::validation($data, $files);

        if ($errorcode = course_validate_dates($data['defaults'])) {
            $errors['defaults[enddate]'] = get_string($errorcode, 'error');
        }

        return $errors;
    }
1019433
public function validation($data, $files) {
        $errors = parent::validation($data, $files);

        // check the phases borders are valid
        if ($data['submissionstart'] > 0 and $data['submissionend'] > 0 and $data['submissionstart'] >= $data['submissionend']) {
            $errors['submissionend'] = get_string('submissionendbeforestart', 'mod_workshop');
        }
        if ($data['assessmentstart'] > 0 and $data['assessmentend'] > 0 and $data['assessmentstart'] >= $data['assessmentend']) {
            $errors['assessmentend'] = get_string('assessmentendbeforestart', 'mod_workshop');
        }

        // check the phases do not overlap
        if (max($data['submissionstart'], $data['submissionend']) > 0 and max($data['assessmentstart'], $data['assessmentend']) > 0) {
            $phasesubmissionend = max($data['submissionstart'], $data['submissionend']);
            $phaseassessmentstart = min($data['assessmentstart'], $data['assessmentend']);
            if ($phaseassessmentstart == 0) {
                $phaseassessmentstart = max($data['assessmentstart'], $data['assessmentend']);
            }
            if ($phasesubmissionend > 0 and $phaseassessmentstart > 0 and $phaseassessmentstart < $phasesubmissionend) {
                foreach (array('submissionend', 'submissionstart', 'assessmentstart', 'assessmentend') as $f) {
                    if ($data[$f] > 0) {
                        $errors[$f] = get_string('phasesoverlap', 'mod_workshop');
                        break;
                    }
                }
            }
        }

        // Check that the submission grade pass is a valid number.
        if (!empty($data['submissiongradepass'])) {
            $submissiongradefloat = unformat_float($data['submissiongradepass'], true);
            if ($submissiongradefloat === false) {
                $errors['submissiongradepass'] = get_string('err_numeric', 'form');
            } else {
                if ($submissiongradefloat > $data['grade']) {
                    $errors['submissiongradepass'] = get_string('gradepassgreaterthangrade', 'grades', $data['grade']);
                }
            }
        }

        // Check that the grade pass is a valid number.
        if (!empty($data['gradinggradepass'])) {
            $gradepassfloat = unformat_float($data['gradinggradepass'], true);
            if ($gradepassfloat === false) {
                $errors['gradinggradepass'] = get_string('err_numeric', 'form');
            } else {
                if ($gradepassfloat > $data['gradinggrade']) {
                    $errors['gradinggradepass'] = get_string('gradepassgreaterthangrade', 'grades', $data['gradinggrade']);
                }
            }
        }

        if (!$data['submissiontypetextavailable'] && !$data['submissiontypefileavailable']) {
            // One submission type must be available.
            $errors['submissiontypes'] = get_string('nosubmissiontype', 'workshop');
        }

        return $errors;
    }
742773
function validation($data, $files) {
        global $COURSE, $DB;

        $errors = parent::validation($data, $files);

        $name = trim($data['name']);
        if (isset($data['idnumber'])) {
            $idnumber = trim($data['idnumber']);
        } else {
            $idnumber = '';
        }
        if ($data['id'] and $grouping = $DB->get_record('groupings', array('id'=>$data['id']))) {
            if (core_text::strtolower($grouping->name) != core_text::strtolower($name)) {
                if (groups_get_grouping_by_name($COURSE->id,  $name)) {
                    $errors['name'] = get_string('groupingnameexists', 'group', $name);
                }
            }
            if (!empty($idnumber) && $grouping->idnumber != $idnumber) {
                if (groups_get_grouping_by_idnumber($COURSE->id, $idnumber)) {
                    $errors['idnumber']= get_string('idnumbertaken');
                }
            }

        } else if (groups_get_grouping_by_name($COURSE->id, $name)) {
            $errors['name'] = get_string('groupingnameexists', 'group', $name);
        } else if (!empty($idnumber) && groups_get_grouping_by_idnumber($COURSE->id, $idnumber)) {
            $errors['idnumber']= get_string('idnumbertaken');
        }

        return $errors;
    }
1258273
function validation($data, $files) {
        global $USER;
        $errors = parent::validation($data, $files);
        $reason = null;

        // ignore submitted username
        if (!$user = authenticate_user_login($USER->username, $data['password'], true, $reason, false)) {
            $errors['password'] = get_string('invalidlogin');
            return $errors;
        }

        if ($data['newpassword1'] <> $data['newpassword2']) {
            $errors['newpassword1'] = get_string('passwordsdiffer');
            $errors['newpassword2'] = get_string('passwordsdiffer');
            return $errors;
        }

        if ($data['password'] == $data['newpassword1']){
            $errors['newpassword1'] = get_string('mustchangepassword');
            $errors['newpassword2'] = get_string('mustchangepassword');
            return $errors;
        }

        if (user_is_previously_used_password($USER->id, $data['newpassword1'])) {
            $errors['newpassword1'] = get_string('errorpasswordreused', 'core_auth');
            $errors['newpassword2'] = get_string('errorpasswordreused', 'core_auth');
        }

        $errmsg = '';//prevents eclipse warnings
        if (!check_password_policy($data['newpassword1'], $errmsg)) {
            $errors['newpassword1'] = $errmsg;
            $errors['newpassword2'] = $errmsg;
            return $errors;
        }

        return $errors;
    }
299190
public static function validateUploadedFiles(array $files)
    {
        foreach ($files as $file) {
            if (is_array($file)) {
                static::validateUploadedFiles($file);

                continue;
            }

            if (!$file instanceof UploadedFileInterface) {
                return false;
            }
        }

        return true;
    }
1008651
function validation($data, $files) {
        global $CFG, $COURSE;
        $errors = parent::validation($data, $files);

        if ($data['allocateby'] != 'no') {
            $source = array();
            if ($data['cohortid']) {
                $source['cohortid'] = $data['cohortid'];
            }
            if ($data['groupingid']) {
                $source['groupingid'] = $data['groupingid'];
            }
            if ($data['groupid']) {
                $source['groupid'] = $data['groupid'];
            }
            if (!$users = groups_get_potential_members($data['courseid'], $data['roleid'], $source)) {
                $errors['roleid'] = get_string('nousersinrole', 'group');
            }

           /// Check the number entered is sane
            if ($data['groupby'] == 'groups') {
                $usercnt = count($users);

                if ($data['number'] > $usercnt || $data['number'] < 1) {
                    $errors['number'] = get_string('toomanygroups', 'group', $usercnt);
                }
            }
        }

        //try to detect group name duplicates
        $name = groups_parse_name(trim($data['namingscheme']), 0);
        if (groups_get_group_by_name($COURSE->id, $name)) {
            $errors['namingscheme'] = get_string('groupnameexists', 'group', $name);
        }

        // check grouping name duplicates
        if ( isset($data['grouping']) && $data['grouping'] == '-1') {
            $name = trim($data['groupingname']);
            if (empty($name)) {
                $errors['groupingname'] = get_string('required');
            } else if (groups_get_grouping_by_name($COURSE->id, $name)) {
                $errors['groupingname'] = get_string('groupingnameexists', 'group', $name);
            }
        }

       /// Check the naming scheme
        if ($data['groupby'] == 'groups' and $data['number'] == 1) {
            // we can use the name as is because there will be only one group max
        } else {
            $matchcnt = preg_match_all('/[#@]{1,1}/', $data['namingscheme'], $matches);
            if ($matchcnt != 1) {
                $errors['namingscheme'] = get_string('badnamingscheme', 'group');
            }
        }

        return $errors;
    }
869
private function check_fields(array $data)
    {
        $ruleset = $this->validation_rules();
        $mismatch = array_diff_key($data, $ruleset);
        $fields = array_keys($mismatch);

        foreach ($fields as $field) {
            $this->errors[] = array(
                'field' => $field,
                'value' => $data[$field],
                'rule' => 'mismatch',
                'param' => null,
            );
        }
    }
400099
public function validation($data, $files) {
        $error = parent::validation($data, $files);
        $fields = array('minute', 'hour', 'day', 'month', 'dayofweek');
        foreach ($fields as $field) {
            if (!self::validate_fields($field, $data[$field])) {
                $error[$field] = get_string('invaliddata', 'core_error');
            }
        }
        return $error;
    }
518165
public static function validateFiled(array $data, string $field_value, array $field_rules, array $messages = [])
    {
        $errors = [];

        foreach ($field_rules as $check => $check_value) {

            switch ($check) {

                case 'required':
                    if (!$field_value) {
                        $errors[$check] = isset($messages['required']) ? w($messages['required']) : self::$messages['required'];
                    }

                    break;

                case 'min_length':

                    if (mb_strlen($field_value) < $check_value) {
                        $errors[$check] = sprintf(isset($messages['min_length']) ? w($messages['min_length']) : self::$messages['min_length'], $check_value);
                    }

                    break;

                case 'max_length':

                    if (mb_strlen($field_value) > $check_value) {
                        $errors[$check] = sprintf(isset($messages['max_length']) ? w($messages['max_length']) : self::$messages['max_length'], $check_value);
                    }

                    break;

                case 'min':

                    if ($field_value < $check_value) {
                        $errors[$check] = sprintf(isset($messages['min']) ? w($messages['min']) : self::$messages['min'], $check_value);
                    }

                    break;

                case 'max':

                    if ($field_value > $check_value) {
                        $errors[$check] = sprintf(isset($messages['max']) ? w($messages['max']) : self::$messages['max'], $check_value);
                    }

                    break;

                case 'pattern':

                    if (!preg_match($check_value, $field_value)) {
                        $errors[$check] = $check_value;
                    }

                    break;

                case 'min_checkboxes_checked':

                    break;

                case 'max_checkboxes_checked':

                    break;

                case 'equal_to_field':

                    if (!isset($data[$check_value]) || $field_value != $data[$check_value]) {
                        $errors[$check] = sprintf(isset($messages['max']) ? w($messages['max']) : self::$messages['max'], $check_value);
                    }

                    break;

                // Is used for invisible fields, substituting captcha: if filled, it is invalid
                case 'empty':

                    if (!empty($field_value)) {
                        $errors[$check] = isset($messages['empty']) ? w($messages['empty']) : self::$messages['empty'];
                    }

                    break;

                case 'type':

                    switch ($check_value) {

                        case 'email':

                            if (!Verify::email($field_value)) {
                                $errors[$check] = isset($messages['type']) ? w($messages['type']) : self::$messages['email'];
                            }

                            break;

                        case 'number':

                            if (!is_numeric($field_value)) {
                                $errors[$check] = isset($messages['type']) ? w($messages['type']) : self::$messages['number'];
                            }

                            break;

                        case 'integer':

                            if (!\is_numeric($field_value) || !filter_var($field_value, FILTER_VALIDATE_INT)) {
                                $errors[$check] = isset($messages['type']) ? w($messages['type']) : self::$messages['integer'];
                            }

                            break;

                        case 'digits':

                            if (!ctype_digit($field_value)) {
                                $errors[$check] = isset($messages['type']) ? w($messages['type']) : self::$messages['digits'];
                            }

                            break;

                        case 'alphanum':

                            if (!ctype_alnum($field_value)) {
                                $errors[$check] = isset($messages['type']) ? w($messages['type']) : self::$messages['aphanum'];
                            }

                            break;

                        case 'url':

                            if (!filter_var($field_value, FILTER_VALIDATE_URL)) {
                                $errors[$check] = isset($messages['url']) ? w($messages['url']) : self::$messages['url'];
                            }

                            break;
                    }

                    break;
            }
        }

        return empty($errors) ? true : $errors;
    }
940794
private function validateData($rules, $data) {
		$errors = [];
		foreach ($rules as $field => $rule) {
			$valid = $this->validateField($field, $data, $rule);
			if ($valid !== true) {
				$errors[ $field ] = $valid;
			}
		}
		if (!empty($errors)) {
			throw new ValidateException($errors);
		}

		return true;
	}
561681
public function validate(array $data) : bool
    {
        $passed = true;
        $this->invalidFields = [];
        $rules = $this->getRules();
        foreach ($rules as $validation => $fields) {
            foreach ($fields as $key => $value) {
                $field = $this->getFieldInfo($key, $value);
                $validationInstance = $this->getValidation($validation);
                $validationStatus = $validationInstance->run($field, $data);
                $passed = $passed && $validationStatus;
                $this->invalidFields = array_merge_recursive(
                        $this->invalidFields, $validationInstance->getMessages()
                );
            }
        }
        return $passed;
    }
266737
public static function getCreateErrors(array $data, array $errors = [])
    {
        if (!isset($data['singular']) || empty($data['singular'])) {
            $errors['singular'] = 'Singular is required';
        }

        if (!isset($data['plural']) || empty($data['plural'])) {
            $errors['plural'] = 'Plural is required';
        }

        if (!isset($data['name']) || empty($data['name'])) {
            $errors['name'] = 'Keyword is required';
        }

        if (!isset($data['fields']) || empty($data['fields'])) {
            $errors['fields'] = 'Fields is required';
        }

        if (isset($data['name'])) {
            $exists = false;
            try {
                $exists = Schema::i($data['name'])->getAll();
            } catch (Exception $e) {
            }

            if ($exists) {
                $errors['name'] = 'Schema already exists';
            }
        }

        return self::getOptionalErrors($data, $errors);
    }
586677
protected function validateUploads(array $files): void
    {
        foreach ($files as $file) {
            if (null === $file) {
                continue;
            }
            if ($file instanceof UploadedFileInterface) {
                $this->validateUpload($file);
            } else {
                $this->validateUploads($file);
            }
        }
    }
563326
protected function validate(array &$data)
    {
        $messages = [];
        if (isset($data['inserts']) && !empty($data['inserts'])) {
            array_push($messages, ['warning', sprintf('Found %s new files. New, not original files may increase the risk of instability.', count($data['inserts']))]);
        }
        if (isset($data['deletes']) && !empty($data['deletes'])) {
            array_push($messages, ['warning', sprintf('%s files has been deleted. Deleting system files may increase the risk of instability.', count($data['deletes']))]);
        }
        if (isset($data['updates']) && !empty($data['updates'])) {
            array_push($messages, ['warning', sprintf('%s files has been changed. File modifications are unacceptable and can increase the risk of instability.', count($data['updates']))]);
        }
        $data = array_merge($data, ['messages' => $messages]);
        return $data;
    }
539889
protected function validate(array &$data)
    {
        $this->validateVersion();
        $this->validateExtensions();
        $this->validateIni();
        $data = array_merge($data, ['messages' => $this->messages]);
        return $data;
    }
238970
protected function validate(array &$data)
    {
        $messages = [];
        if (!empty($data['errors']) && !empty($data['activity']) && !empty($data['requests'])) {
            array_push($messages, ['warning', trans('It is recommended to periodically clean system logs.')]);
        }
        $data = array_merge($data, ['messages' => $messages]);
        return $data;
    }
1209678
protected function validation_inner($data, $files) {

        $errors = array();

        // Iterate over all submitted dimensions (criteria).
        for ($i = 0; isset($data['dimensionid__idx_'.$i]); $i++) {

            $dimgrades = array();

            if (0 == strlen(trim($data['description__idx_'.$i.'_editor']['text']))) {
                // The description text is empty and this criterion will be deleted.
                continue;
            }

            // Make sure the levels grades are unique within the criterion.
            $atleastonelevel = false;
            for ($j = 0; isset($data['levelid__idx_'.$i.'__idy_'.$j]); $j++) {
                if (0 == strlen(trim($data['definition__idx_'.$i.'__idy_'.$j]))) {
                    // The level definition is empty and will not be saved.
                    continue;
                }
                $atleastonelevel = true;

                $levelgrade = $data['grade__idx_'.$i.'__idy_'.$j];

                if (isset($dimgrades[$levelgrade])) {
                    // This grade has already been set for another level.
                    $k = $dimgrades[$levelgrade];
                    $errors['level__idx_'.$i.'__idy_'.$j] = $errors['level__idx_'.$i.'__idy_'.$k] = get_string('mustbeunique',
                        'workshopform_rubric');
                } else {
                    $dimgrades[$levelgrade] = $j;
                }
            }
            if (!$atleastonelevel) {
                $errors['level__idx_'.$i.'__idy_0'] = get_string('mustdefinelevel', 'workshopform_rubric');
            }
        }

        return $errors;
    }