Validates this form.

@param array $data
@param array $files
@return array
515364
final public function validation($data, $files) {
        $errors = parent::validation($data, $files);
        if (!isset($errors['name'])) {
            $config = cache_config::instance();
            if (in_array($data['name'], array_keys($config->get_locks()))) {
                $errors['name'] = get_string('locknamenotunique', 'cache');
            }
        }
        $errors = $this->plugin_validation($data, $files, $errors);
        return $errors;
    }
104360
public function validation($data, $files) {
        global $CFG, $DB, $USER;

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

        // Validate course association.
        if (!empty($data['courseassoc'])) {
            $coursecontext = context::instance_by_id($data['courseassoc']);

            if ($coursecontext->contextlevel != CONTEXT_COURSE) {
                $errors['courseassoc'] = get_string('error');
            }
        }

        // Validate mod association.
        if (!empty($data['modassoc'])) {
            $modcontextid = $data['modassoc'];
            $modcontext = context::instance_by_id($modcontextid);

            if ($modcontext->contextlevel == CONTEXT_MODULE) {
                // Get context of the mod's course.
                $coursecontext = $modcontext->get_course_context(true);

                // Ensure only one course is associated.
                if (!empty($data['courseassoc'])) {
                    if ($data['courseassoc'] != $coursecontext->id) {
                        $errors['modassoc'] = get_string('onlyassociateonecourse', 'blog');
                    }
                } else {
                    $data['courseassoc'] = $coursecontext->id;
                }
            } else {
                $errors['modassoc'] = get_string('error');
            }
        }

        if ($errors) {
            return $errors;
        }
        return true;
    }
508421
public function validateData(array $data, array $rules, array $messages)
    {
        if ($rules) {
            $this->customValidator->setData($data);
            $this->customValidator->setRules($rules);
            $this->customValidator->setCustomMessages($messages);

            //if the validator fails, kick back the errors
            if ($this->customValidator->fails()) {
                return implode('. ', $this->customValidator->messages()->all());
            }
        }

        return true;
    }
986992
public function config_form_validation(array $data, $files = array()) : array {
        $errors = parent::config_form_validation($data, $files);

        if ($data['configdata']['uniquevalues']) {
            $errors['configdata[uniquevalues]'] = get_string('errorconfigunique', 'customfield_checkbox');
        }

        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;
    }
191157
public function validation($data, $files) {
        $errors = parent::validation($data, $files);

        if ($files = $this->get_draft_files('rolepreset')) {
            /** @var stored_file $file */
            $file = reset($files);
            $xml = $file->get_content();
            if (!core_role_preset::is_valid_preset($xml)) {
                $errors['rolepreset'] = get_string('invalidpresetfile', 'core_role');
            }
        }

        return $errors;
    }
1208394
protected function validate_by_callbacks( array $data, array $callbacks ) {
		foreach ( $callbacks as $key => $callback ) {
			$validity = new WP_Error;

			$value = array_key_exists( $key, $data ) ? $data[ $key ] : null;
			$callback( $validity, $value );

			if ( is_wp_error( $validity ) && count( $validity->errors ) > 0 ) {
				$this->add_error( $key, $validity->get_error_messages() );
			}
		}
	}
1158351
private static function validateStore($data)
    {
        // custom messages for validation
        $validationMessagesTemplate = array(
          'required'=> "{{field}} can't be empty",
          'email'=>"{{field}} must be an email",
          'integer'=>"{{field}} must be a number",
        );
        $validationMessages = array();
        $validationRules = array();
        $validationValues= array();

        //setup title validation
        $isOneTitleFilled = false;
        foreach ($data['title'] as $title){
            if($title !== '') {
                $isOneTitleFilled = true;
            }
        }
        if (!$isOneTitleFilled) {
            foreach ($data['languages'] as $lang) {
                $validationMessages["title_" . $lang['slug'].".required"] = str_replace("{{field}}", "Title", $validationMessagesTemplate["required"]);
                $validationRules["title_" . $lang['slug']] = 'required';
                $validationValues["title_" . $lang['slug']] = $data['title'][$lang['slug']];
            }
        }

        // Custom fields validation
        foreach($data['formData'] as $formData){ // loop throw form data
            if ($formData['required']) { // check if input is required
                if(!$formData['translatable']) { // if input is not multilanguage
                    foreach ($data['languages'] as $lang){
                        // if it is not a file return required error
                        if($formData['type']['inputType'] != "image" && $formData['type']['inputType'] != "file") {
                            if(isset($data['status']) && $data['status'][$lang['slug']] != 'draft') {
                                $validationMessages[$formData['slug'] . "_" . $lang['slug'].".required"] = str_replace("{{field}}", $formData['name'], $validationMessagesTemplate["required"]);
                                $validationRules[$formData['slug'] . "_" . $lang['slug']] = 'required';
                                $validationValues[$formData['slug']. "_" . $lang['slug']] = $formData['value'];
                            }
                        }else{
                            if($formData['translatable']) {
                                $imageKey = $formData['slug']."_".$lang['slug'];
                            }else{
                                $imageKey = $formData['slug'];
                            }

                            if(isset($data['status']) && $data['status'][$lang['slug']] != 'draft') {
                                $validationMessages[$formData['slug'] . "_" . $lang['slug'].".required"] = str_replace("{{field}}", $formData['name'], $validationMessagesTemplate["required"]);
                                $validationRules[$formData['slug'] . "_" . $lang['slug']] = 'required';
                            }

                            if(!isset($data['files'][$imageKey])) {  // errors if images or files are not set and the language is status
                                if(isset($data['status']) && $data['status'][$lang['slug']] != 'draft') {
                                    $validationValues[$formData['slug']. "_" . $lang['slug']] = "";
                                }
                            }else{
                                $validationValues[$formData['slug']. "_" . $lang['slug']] = $data['files'][$imageKey];
                            }
                        }
                    }

                }else{ // if input is multilanguage
                    if ($formData['value']) {
                        foreach($formData['value'] as $langSlug => $value){ // loop throw the values for each language
                            if(isset($data['status']) && !is_integer($langSlug) && $data['status'][$langSlug] != 'draft') {
                                $validationMessages[$formData['slug'] . "_" . $langSlug.".required"] = str_replace("{{field}}", $formData['name'], $validationMessagesTemplate["required"]);
                                $validationRules[$formData['slug'] . "_" . $langSlug] = 'required';
                            }

                            //if is dropdown or checbox
                            if(is_array($value)) {
                                if(isset($data['status']) && !is_integer($langSlug) && $data['status'][$langSlug] != 'draft') {
                                    $validationValues[$formData['slug']. "_" . $langSlug] = $value;
                                }
                            }else {
                                if(isset($data['status']) && !is_integer($langSlug) && $data['status'][$langSlug] != 'draft') {
                                    $validationValues[$formData['slug']. "_" . $langSlug] = $value;
                                }
                            }
                        }
                    }
                }
            }

            /*
             * CUSTOM validations like email, number etc
             * */
            switch ($formData['type']['inputType']){
            case "email":
                foreach ($data['languages'] as $lang){
                    if(isset($data['status']) && $data['status'][$lang['slug']] != 'draft') {
                        $validationMessages[$formData['slug'] . "_" . $lang['slug'] . ".email"] = str_replace("{{field}}", $formData['name'], $validationMessagesTemplate["email"]);

                        //set email rule
                        if(isset($validationRules[$formData['slug'] . "_" . $lang['slug']])) {
                            $validationRules[$formData['slug'] . "_" . $lang['slug']] .= '|email';
                        }else{
                            $validationRules[$formData['slug'] . "_" . $lang['slug']] = 'email';
                        }
                    }
                }
                break;
            case "number":
                foreach ($data['languages'] as $lang){
                    if(isset($data['status']) && $data['status'][$lang['slug']] != 'draft') {
                        $validationMessages[$formData['slug'] . "_" . $lang['slug'] . ".integer"] = str_replace("{{field}}", $formData['name'], $validationMessagesTemplate["integer"]);

                        //set email rule
                        if(isset($validationRules[$formData['slug'] . "_" . $lang['slug']])) {
                            $validationRules[$formData['slug'] . "_" . $lang['slug']] .= '|integer';
                        }else{
                            $validationRules[$formData['slug'] . "_" . $lang['slug']] = 'integer';
                        }
                    }
                }
                break;
            }
        }

        // Validate tags
        if($data['isFeaturedImageRequired']) {
            if(!isset($data['files']) || !isset($data['files']['featuredImage']) || (isset($data['files']['featuredImage']) && !count($data['files']['featuredImage']))) {
                $validationMessages["files_featuredImage.required"] = str_replace("{{field}}", "Featured Image ", $validationMessagesTemplate["required"]);
                $validationRules["files_featuredImage"] = 'required';
                $validationValues["featuredImage"] = "NONE";
            }
        }

        // Validatate categories
        if($data['isCategoryRequired']) {
            $validationMessages["categories.required"] = str_replace("{{field}}", "Categories", $validationMessagesTemplate["required"]);
            $validationRules["categories"] = 'required';
            $validationValues["categories"] = $data['selectedCategories'];
        }

        // Validate tags
        if($data['isTagRequired']) {
            foreach($data['selectedTags'] as $langKey => $tags){
                if(isset($data['status']) && $data['status'][$langKey] != 'draft') {
                    $validationMessages["tags_" . $langKey .".required"] = str_replace("{{field}}", "Tags", $validationMessagesTemplate["required"]);
                    $validationRules["tags_" . $langKey] = 'required';
                    $validationValues["tags_" . $langKey] = $data['selectedTags'][$langKey];
                }
            }
        }

        $validator = \Illuminate\Support\Facades\Validator::make($validationValues, $validationRules, $validationMessages);
        return $validator->messages();
    }
1013579
public function validateAsSubForm(Dictionary $arguments, Dictionary $files)
    {
        $name = $this->name;
        if (!$arguments->has($name, Type::ARRAY))
        {
            $this->errors = [];
            if ($this->isRequired())
            {
                $this->errors = ['' => ['msg' => "{name} is required", 'context' => ['name' => $name]]];
                $this->complete = false;
            }
            return $this->complete;
        }

        $sub = $arguments->get($name);
        $subfiles = $files->has($name, Type::ARRAY) ? $files[$name] : new Dictionary;

        if (!$this->repeatable)
            return $this->validate($sub, $subfiles);

        // Repeatable forms should be an array of arrays - each containing
        // their own set of the values for the form elements. We need to
        // collect them all here.
        $value = [];
        $errors = [];

        // Only numeric keys are allowed for repeatable forms.
        if (!WF::is_numeric_array($sub))
        {
            $this->errors = ['' => ['msg' => "{name} should be an array", 'context' => ['name' => $name]]];
            $this->complete = false;
            return $this->complete;
        }

        // Validate each set of submitted data
        $valid = true;
        foreach ($sub as $idx => $subsub)
        {
            $subsubfiles = $subfiles->has($idx, Type::ARRAY) ? $subfiles[$idx] : new Dictionary;
            $valid = $valid && $this->validate($subsub, $subsubfiles);
            $subvalue = $this->getValue();
            $value[$idx] = $subvalue;

            if (count($this->errors))
                $errors[$idx] = $this->errors;
        }

        // Store the value and the errors for the form as a whole.
        $this->value = $value;
        $this->errors = $errors;
        return $valid;
    }
1322612
public function validation($data, $files) {
        $errors = parent::validation($data, $files);
        if (empty($data['fulldelete']) && empty($data['newparent'])) {
            // When they have chosen the move option, they must specify a destination.
            $errors['newparent'] = get_string('required');
        }

        if ($data['sure'] !== md5(serialize($this->coursecat))) {
            $errors['categorylabel'] = get_string('categorymodifiedcancel');
        }

        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;
    }
593293
public function instance_form_validation(array $data, array $files) : array {
        $errors = parent::instance_form_validation($data, $files);
        if ($this->get_field()->get_configdata_property('required')) {
            // Standard required rule does not work on select element.
            $elementname = $this->get_form_element_name();
            if (empty($data[$elementname])) {
                $errors[$elementname] = get_string('err_required', 'form');
            }
        }
        return $errors;
    }
985
function validation($data, $files) {
        global $COURSE;

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

        if (array_key_exists('idnumber', $data)) {
            if ($data['id']) {
                $grade_item = new grade_item(array('id'=>$data['id'], 'courseid'=>$data['courseid']));
            } else {
                $grade_item = null;
            }
            if (!grade_verify_idnumber($data['idnumber'], $COURSE->id, $grade_item, null)) {
                $errors['idnumber'] = get_string('idnumbertaken');
            }
        }

        return $errors;
    }
1093614
public function validate(Dictionary $arguments, Dictionary $files)
    {
        $source = $this->isFile() ? $files : $arguments;
        $value = $this->extractValue($source);
        $this->setValue($value, true);
        $value = $this->value;
        $this->errors = [];
        if ($this->is_array)
        {
            $name = $this->getName(true);
            $value = $this->isFile() ? $files[$name] : $arguments[$name];
            $this->setValue($value);

            // Validate arrays element by element
            if (WF::is_array_like($value) && !($value instanceof Dictionary))
                $value = new Dictionary($value);

            if (!$value instanceof Dictionary)
            {
                $this->errors[''][] = [
                    'msg' => 'Invalid value for {name}: {value}', 
                    'context' => ['name' => $this->name, 'value' => (string)$value]
                ];
                return false;
            }

            // Value should be a shallow array
            if (!$value->isShallow())
            {
                $this->errors[''][] = [
                    'msg' => 'Field {name} should not nest',
                    'context' => ['name' => $this->name]
                ];
            }

            // Empty depends on the value of nullable
            if (count($value) === 0)
            {
                $nullable = true;
                foreach ($this->validators as $validator)
                    $nullable = $nullable && $validator->isNullable();

                if (!$nullable)
                {
                    $this->errors[''][] = [
                        'msg' => 'Required field: {name}',
                        'context' => ['name' => $this->name]
                    ];
                }
            }

            // Validate each value of the array
            foreach ($value as $key => $v)
            {
                foreach ($this->validators as $validator)
                {
                    if (!$validator->validate($v))
                    {
                        $this->errors[$key][] = $validator->getErrorMessage($v);
                    }
                }
            }

            // Check if any errors were produced
            return count($this->errors) === 0;
        }

        foreach ($this->validators as $validator)
        {
            $result = $validator->validate($this->value, $value);
            if (!$result)
            {
                $this->errors[] = $validator->getErrorMessage($value);
            }
            else
            {
                $this->value = $value;
            }
        }

        return count($this->errors) === 0;
    }
358136
public function update(array $data, array $files = [])
    {
        if ($data) {
            $blueprint = $this->getBlueprint();

            // Process updated data through the object filters.
            $this->filterElements($data);

            // Get currently stored data.
            $elements = $this->getElements();

            // Merge existing object to the test data to be validated.
            $test = $blueprint->mergeData($elements, $data);

            // Validate and filter elements and throw an error if any issues were found.
            $blueprint->validate($test + ['storage_key' => $this->getStorageKey(), 'timestamp' => $this->getTimestamp()]);
            $data = $blueprint->filter($data, false, true);

            // Finally update the object.
            foreach ($blueprint->flattenData($data) as $key => $value) {
                if ($value === null) {
                    $this->unsetNestedProperty($key);
                } else {
                    $this->setNestedProperty($key, $value);
                }
            }

            // Store the changes
            $this->_changes = Utils::arrayDiffMultidimensional($this->getElements(), $elements);
        }

        if ($files && method_exists($this, 'setUpdatedMedia')) {
            $this->setUpdatedMedia($files);
        }

        return $this;
    }
416585
protected final function validateNewData(array $data = null) {
		if ($data === null && isset($this->_formData)) {
			$data = $this->_formData;
		}
		if ($this->rules) {
			return $this->validateData($this->rules, $data);
		}

		return true;
	}
1347317
function validation($data, $files) {
        global $DB;

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

        // Add field validation check for duplicate shortname.
        if ($course = $DB->get_record('course', array('shortname' => $data['shortname']), '*', IGNORE_MULTIPLE)) {
            if (empty($data['id']) || $course->id != $data['id']) {
                $errors['shortname'] = get_string('shortnametaken', '', $course->fullname);
            }
        }

        // Add field validation check for duplicate idnumber.
        if (!empty($data['idnumber']) && (empty($data['id']) || $this->course->idnumber != $data['idnumber'])) {
            if ($course = $DB->get_record('course', array('idnumber' => $data['idnumber']), '*', IGNORE_MULTIPLE)) {
                if (empty($data['id']) || $course->id != $data['id']) {
                    $errors['idnumber'] = get_string('courseidnumbertaken', 'error', $course->fullname);
                }
            }
        }

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

        $errors = array_merge($errors, enrol_course_edit_validation($data, $this->context));

        $courseformat = course_get_format((object)array('format' => $data['format']));
        $formaterrors = $courseformat->edit_form_validation($data, $files, $errors);
        if (!empty($formaterrors) && is_array($formaterrors)) {
            $errors = array_merge($errors, $formaterrors);
        }

        // Add the custom fields validation.
        $handler = core_course\customfield\course_handler::create();
        $errors  = array_merge($errors, $handler->instance_form_validation($data, $files));

        return $errors;
    }