Validate the answers.
@param array $data the submitted data.
@param array $errors the errors array to add to.
@return array the updated errors array.
17066
protected function validate_numerical_options($data, $errors) {
        if ($data['unitrole'] != qtype_numerical::UNITNONE && trim($data['unit'][0]) == '') {
            $errors['units[0]'] = get_string('unitonerequired', 'qtype_numerical');
        }

        if (empty($data['unit']) || $data['unitrole'] == qtype_numerical::UNITNONE) {
            return $errors;
        }

        // Basic unit validation.
        foreach ($data['unit'] as $key => $unit) {
            if (is_numeric($unit)) {
                $errors['units[' . $key . ']'] =
                        get_string('xmustnotbenumeric', 'qtype_numerical',
                            get_string('unit', 'qtype_numerical'));
            }

            $trimmedunit = trim($unit);
            if (empty($trimmedunit)) {
                continue;
            }

            $trimmedmultiplier = trim($data['multiplier'][$key]);
            if (empty($trimmedmultiplier)) {
                $errors['units[' . $key . ']'] =
                        get_string('youmustenteramultiplierhere', 'qtype_numerical');
            } else if ($trimmedmultiplier === false) {
                $errors['units[' . $key . ']'] =
                        get_string('xmustbenumeric', 'qtype_numerical',
                            get_string('multiplier', 'qtype_numerical'));
            }
        }

        // Check for repeated units.
        $alreadyseenunits = array();
        foreach ($data['unit'] as $key => $unit) {
            $trimmedunit = trim($unit);
            if ($trimmedunit == '') {
                continue;
            }

            if (in_array($trimmedunit, $alreadyseenunits)) {
                $errors['units[' . $key . ']'] =
                        get_string('errorrepeatedunit', 'qtype_numerical');
            } else {
                $alreadyseenunits[] = $trimmedunit;
            }
        }

        return $errors;
    }
651338
protected function validate($data, $rules)
    {
        $data = array_filter($data);

        $validator = $this->validator->make($data, $rules);

        if ($validator->fails()) {
            throw new PagSeguroException($validator->messages()->first(), 1003);
        }
    }
205743
public function validate( array $data ) {
		// Validate use Validator.
		try {
			$this->validate_use_validator( $data, $this->get_validate_rules() );
		} catch ( \Exception $e ) {
			trigger_error( $e->getMessage(), E_USER_WARNING ); // @WPCS: XSS OK.
		}

		// Validate use custom callback.
		$callbacks = $this->fields->pluck( 'validate_cb', 'id' )->filter( 'is_callable' );

		if ( $callbacks->isNotEmpty() ) {
			$this->validate_by_callbacks( $data, $callbacks->all() );
		}
	}
1203984
protected function validate_use_validator( array $data, array $rules ) {
		if ( empty( $rules ) ) {
			return;
		}

		$validator = new Validator( $data, $rules );

		$validator->labels(
			$this->fields->pluck( 'name', 'id' )->all()
		);

		if ( $validator->fails() ) {
			foreach ( $rules as $key => $rule ) {
				$this->add_error( $key, $validator->errors( $key ) );
			}
		}
	}
858519
public final function validation($data, $files) {
        $errors = parent::validation($data, $files);
        $data = $this->get_submitted_data();

        // Only validate compatible fields.
        $persistentdata = $this->filter_data_for_persistent($data);
        $persistent = $this->get_persistent();
        $persistent->from_record((object) $persistentdata);
        $errors = array_merge($errors, $persistent->get_errors());

        // Apply extra validation.
        $extraerrors = $this->extra_validation($data, $files, $errors);
        $errors = array_merge($errors, (array) $extraerrors);

        return $errors;
    }
115248
public static function report_validation_errors(array $data, array &$errors) {
        // Empty value is allowed!
        if ($data['availabilityconditionsjson'] === '') {
            return;
        }

        // Decode value.
        $decoded = json_decode($data['availabilityconditionsjson']);
        if (!$decoded) {
            // This shouldn't be possible.
            throw new \coding_exception('Invalid JSON from availabilityconditionsjson field');
        }
        if (!empty($decoded->errors)) {
            $error = '';
            foreach ($decoded->errors as $stringinfo) {
                list ($component, $stringname) = explode(':', $stringinfo);
                if ($error !== '') {
                    $error .= ' ';
                }
                $error .= get_string($stringname, $component);
            }
            $errors['availabilityconditionsjson'] = $error;
        }
    }
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;
    }
708332
public function validate(array $data)
    {
        $rules = $this->getRules();
        foreach ($rules as $field => $rule) {
            if ((isset($rule['required']) && $rule['required'] === true && isset($data[$field]))
                || ((!isset($rule['required']) || !$rule['required'])) && isset($data[$field])) {
                foreach ($rule as $key => $value) {
                    if ($key !== 'required') {
                        foreach ($value as $validator) {
                            if (!is_array($validator)) {
                                $validator = [$validator];
                            }
                            if (!Validator::validate($validator[0], $field,
                                    $data, $validator)) {
                                $this->addError($field,
                                    isset($rule['errorMessage']) ? $rule['errorMessage']
                                            : false);
                            }
                        }
                    }
                }
            } elseif (isset($rule['required']) && $rule['required'] === true) {
                $this->addError($field,
                    isset($rule['errorMessage']) ? $rule['errorMessage'] : false);
            }
        }
        return !$this->hasErrors();
    }
333671
protected static function checkNew(array $data)
    {
        $errors = [];
        if ($missing = array_diff(static::getRequired(), array_keys($data))) {
            $errors[] = 'Missing: ' . implode(', ', $missing);
        }
        foreach ($data as $key => $value) {
            $errors += static::checkProperty($key, $value);
        }
        return $errors;
    }
699183
public function validate(array $data = [], array $rules = [], array $messages = [])
    {
        $this->prepareRecord($data, $rules, $messages);

        return $this->validateRecord();
    }
695265
public function validation($data, $files) {
        $errors = [];
        if (isset($data['purgeselectedcaches']) && empty(array_filter($data['purgeselectedoptions']))) {
            $errors['purgeselectedoptions'] = get_string('purgecachesnoneselected', 'admin');
        }
        return $errors;
    }
260364
protected function afterValidation($data)
  {
    if ($this->after_validation) {
      $data = call_user_func_array($this->after_validation, array($data, $this));
    }

    return $data;
  }
1020648
public function validate(array $data)
    {
        $validator = $this->getValidator();
        if (!$validator->count()) {
            //@codingStandardsIgnoreStart
            $validator = @$this->validator();
        }
        $this->_errors = $validator->errors($data);

        return count($this->_errors) === 0;
    }
1122500
public function validation($data, $files) {
        $errors = array();
        $draftitemid = $data['files_filemanager'];
        if (file_is_draft_area_limit_reached($draftitemid, $this->_customdata['options']['areamaxbytes'])) {
            $errors['files_filemanager'] = get_string('userquotalimit', 'error');
        }

        return $errors;
    }
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;
	}
509755
private function tryValidateData(
        array $data,
        array $rules,
        Closure $validationFailed,
        array $messages = [],
        array $customAttributes = []
    ) {
        $validator = $this->createValidator($data, $rules, $messages, $customAttributes);

        if ($validator->fails()) {
            call_user_func($validationFailed, $validator->errors()->getMessages());
        }
    }
599710
protected function validate_uploaded_file($data, $errors) {
        global $CFG;

        if (empty($data['questionfile'])) {
            $errors['questionfile'] = get_string('required');
            return $errors;
        }

        $files = $this->get_draft_files('questionfile');
        if (count($files) < 1) {
            $errors['questionfile'] = get_string('required');
            return $errors;
        }

        $formatfile = $CFG->dirroot.'/question/format/'.$data['format'].'/format.php';
        if (!is_readable($formatfile)) {
            throw new moodle_exception('formatnotfound', 'lesson', '', $data['format']);
        }

        require_once($formatfile);

        $classname = 'qformat_' . $data['format'];
        $qformat = new $classname();

        return $errors;
    }