Commit a78890d5 authored by skodak's avatar skodak
Browse files

MDL-12133 validate() method tidying up; merged from MOODLE_19_STABLE

parent 010aa4da
......@@ -269,8 +269,8 @@ class admin_uploaduser_form2 extends moodleform {
/**
* Server side validation.
*/
function validation($data) {
$errors = array();
function validation($data, $files) {
$errors = parent::validation($data, $files);
$columns =& $this->_customdata;
$optype = $data['uutype'];
......@@ -325,11 +325,7 @@ class admin_uploaduser_form2 extends moodleform {
}
}
if (0 == count($errors)){
return true;
} else {
return $errors;
}
return $errors;
}
}
?>
......@@ -405,8 +405,8 @@ class course_edit_form extends moodleform {
/// perform some extra moodle validation
function validation($data){
$errors= array();
function validation($data, $files) {
$errors = parent::validation($data, $files);
if ($foundcourses = get_records('course', 'shortname', $data['shortname'])) {
if (!empty($data['id'])) {
unset($foundcourses[$data['id']]);
......@@ -426,11 +426,7 @@ class course_edit_form extends moodleform {
}
}
if (0 == count($errors)){
return true;
} else {
return $errors;
}
return $errors;
}
}
?>
......@@ -25,8 +25,8 @@ class course_import_activities_form_1 extends moodleform {
}
function validation($data) {
return true;
function validation($data, $files) {
return parent::validation($data, $files);
}
}
......@@ -52,7 +52,7 @@ class course_import_activities_form_2 extends moodleform {
}
function validation($data) {
function validation($data, $files) {
return true;
}
......
......@@ -65,8 +65,8 @@ class moodleform_mod extends moodleform {
}
}
if ($mform->elementExists('groupmode')) {
if ($COURSE->groupmodeforce) {
if ($COURSE->groupmodeforce) {
if ($mform->elementExists('groupmode')) {
$mform->hardFreeze('groupmode'); // groupmode can not be changed if forced from course settings
}
}
......@@ -86,8 +86,9 @@ class moodleform_mod extends moodleform {
}
// form verification
function validation($data) {
function validation($data, $files) {
global $COURSE;
$errors = parent::validation($data, $files);
$mform =& $this->_form;
......@@ -115,11 +116,7 @@ class moodleform_mod extends moodleform {
}
}
if (count($errors) == 0) {
return true;
} else {
return $errors;
}
return $errors;
}
/**
......@@ -181,8 +178,12 @@ class moodleform_mod extends moodleform {
}
$mform->addElement('header', 'modstandardelshdr', get_string('modstandardels', 'form'));
if ($features->groups){
$mform->addElement('modgroupmode', 'groupmode', get_string('groupmode'));
if ($features->groups) {
$options = array(NOGROUPS => get_string('groupsnone'),
SEPARATEGROUPS => get_string('groupsseparate'),
VISIBLEGROUPS => get_string('groupsvisible'));
$mform->addElement('select', 'groupmode', get_string('groupmode'), $options, NOGROUPS);
$mform->setHelpButton('groupmode', array('groupmode', get_string('groupmode')));
}
if (!empty($CFG->enablegroupings)) {
......
......@@ -31,8 +31,8 @@ class course_request_form extends moodleform {
$this->add_action_buttons();
}
function validation($data) {
$errors = array();
function validation($data, $files) {
$errors = parent::validation($data, $files);
$foundcourses = null;
$foundreqcourses = null;
......@@ -67,12 +67,8 @@ class course_request_form extends moodleform {
}
}
}
if (0 == count($errors)){
return true;
} else {
return $errors;
}
return $errors;
}
}
......
......@@ -171,11 +171,10 @@ class enrol_authorize_form extends moodleform
$this->add_action_buttons(false, get_string('sendpaymentbutton', 'enrol_authorize'));
}
function validation($data)
function validation($data, $files)
{
global $CFG;
$errors = array();
$errors = parent::validation($data, $files);
if (AN_METHOD_CC == $data['paymentmethod'])
{
......@@ -209,7 +208,7 @@ class enrol_authorize_form extends moodleform
}
}
return (empty($errors) ? true : $errors);
return $errors;
}
private function other_method_available($currentmethod)
......
......@@ -135,8 +135,8 @@ class edit_outcome_form extends moodleform {
}
/// perform extra validation before submission
function validation($data){
$errors = array();
function validation($data, $files) {
$errors = parent::validation($data, $files);
if ($data['scaleid'] < 1) {
$errors['scaleid'] = get_string('required');
......@@ -149,11 +149,7 @@ class edit_outcome_form extends moodleform {
}
}
if (0 == count($errors)){
return true;
} else {
return $errors;
}
return $errors;
}
......
......@@ -107,10 +107,10 @@ class edit_scale_form extends moodleform {
}
/// perform extra validation before submission
function validation($data){
function validation($data, $files) {
global $CFG, $COURSE;
$errors = array();
$errors = parent::validation($data, $files);
// we can not allow 2 scales with the same exact scale as this creates
// problems for backup/restore
......@@ -148,11 +148,7 @@ class edit_scale_form extends moodleform {
}
}
if (0 == count($errors)){
return true;
} else {
return $errors;
}
return $errors;
}
}
......
......@@ -82,8 +82,8 @@ class edit_calculation_form extends moodleform {
}
/// perform extra validation before submission
function validation($data){
$errors = array();
function validation($data, $files) {
$errors = parent::validation($data, $files);
$mform =& $this->_form;
......@@ -97,11 +97,7 @@ class edit_calculation_form extends moodleform {
}
}
if (0 == count($errors)){
return true;
} else {
return $errors;
}
return $errors;
}
}
......
......@@ -318,8 +318,8 @@ class edit_item_form extends moodleform {
/// perform extra validation before submission
function validation($data){
$errors = array();
function validation($data, $files) {
$errors = parent::validation($data, $files);
if (array_key_exists('idnumber', $data)) {
if ($data['id']) {
......@@ -351,11 +351,7 @@ class edit_item_form extends moodleform {
}
}
if (0 == count($errors)){
return true;
} else {
return $errors;
}
return $errors;
}
}
......
......@@ -216,8 +216,8 @@ class edit_outcomeitem_form extends moodleform {
/// perform extra validation before submission
function validation($data){
$errors= array();
function validation($data, $files) {
$errors = parent::validation($data, $files);
if (array_key_exists('idnumber', $data)) {
if ($data['id']) {
......@@ -230,11 +230,7 @@ class edit_outcomeitem_form extends moodleform {
}
}
if (0 == count($errors)){
return true;
} else {
return $errors;
}
return $errors;
}
}
......
......@@ -84,7 +84,7 @@ class grade_import_form extends moodleform {
}
function validation($data, $files) {
$err = array();
$err = parent::validation($data, $files);
if (empty($data['url']) and empty($files['userfile'])) {
if (array_key_exists('url', $data)) {
$err['url'] = get_string('required');
......@@ -97,11 +97,7 @@ class grade_import_form extends moodleform {
$err['url'] = get_string('error');
}
if (count($err) == 0){
return true;
} else {
return $err;
}
return $err;
}
}
?>
......@@ -191,13 +191,8 @@ class grader_report_preferences_form extends moodleform {
}
/// perform some extra moodle validation
function validation($data){
$errors= array();
if (0 == count($errors)){
return true;
} else {
return $errors;
}
function validation($data, $files) {
return parent::validation($data, $files);
}
}
?>
......@@ -91,7 +91,7 @@ class autogroup_form extends moodleform {
function validation($data, $files) {
global $CFG, $COURSE;
$errors = array();
$errors = parent::validation($data, $files);
if ($data['allocateby'] != 'no') {
if (!$users = groups_get_potential_members($data['courseid'], $data['roleid'])) {
......
......@@ -42,10 +42,10 @@ class group_form extends moodleform {
$this->add_action_buttons();
}
function validation($data) {
function validation($data, $files) {
global $COURSE;
$errors = array();
$errors = parent::validation($data, $files);
$name = trim(stripslashes($data['name']));
if ($data['id'] and $group = get_record('groups', 'id', $data['id'])) {
......@@ -59,11 +59,7 @@ class group_form extends moodleform {
$errors['name'] = get_string('groupnameexists', 'group', $name);
}
if (count($errors) > 0) {
return $errors;
} else {
return true;
}
return $errors;
}
function get_um() {
......
......@@ -27,10 +27,10 @@ class grouping_form extends moodleform {
$this->add_action_buttons();
}
function validation($data) {
function validation($data, $files) {
global $COURSE;
$errors = array();
$errors = parent::validation($data, $files);
$name = trim(stripslashes($data['name']));
if ($data['id'] and $grouping = get_record('groupings', 'id', $data['id'])) {
......@@ -44,11 +44,7 @@ class grouping_form extends moodleform {
$errors['name'] = get_string('groupingnameexists', 'group', $name);
}
if (count($errors) > 0) {
return $errors;
} else {
return true;
}
return $errors;
}
}
......
......@@ -335,15 +335,16 @@ class moodleform {
$data = $mform->exportValues(null, true);
$moodle_val = $this->validation($data, $files);
if ($moodle_val !== true) {
if ((is_array($moodle_val) && count($moodle_val)!==0)) {
foreach ($moodle_val as $element=>$msg) {
$mform->setElementError($element, $msg);
}
$moodle_val = false;
} else {
$moodle_val = true;
if ((is_array($moodle_val) && count($moodle_val)!==0)) {
// non-empty array means errors
foreach ($moodle_val as $element=>$msg) {
$mform->setElementError($element, $msg);
}
$moodle_val = false;
} else {
// anything else means validation ok
$moodle_val = true;
}
$validated = ($internal_val and $moodle_val and $file_val);
......@@ -511,8 +512,8 @@ class moodleform {
*
* @param array $data array of ("fieldname"=>value) of submitted data
* @param array $files array of uploaded files "element_name"=>tmp_file_path
* @return mixed an array of "element_name"=>"error_description" if there are errors.
* true or an empty array if everything is OK.
* @return array of "element_name"=>"error_description" if there are errors,
* or an empty array if everything is OK (true allowed for backwards compatibility too).
*/
function validation($data, $files) {
return array();
......
......@@ -40,9 +40,9 @@ class login_change_password_form extends moodleform {
}
/// perform extra password change validation
function validation($data){
function validation($data, $files) {
global $USER;
$errors = array();
$errors = parent::validation($data, $files);
update_login_count();
......@@ -73,7 +73,7 @@ class login_change_password_form extends moodleform {
return $errors;
}
return true;
return $errors;
}
}
?>
......@@ -19,10 +19,10 @@ class login_forgot_password_form extends moodleform {
$this->add_action_buttons(true, get_string('ok'));
}
function validation($data) {
function validation($data, $files) {
global $CFG;
$errors = array();
$errors = parent::validation($data, $files);
if ((!empty($data['username']) and !empty($data['email'])) or (empty($data['username']) and empty($data['email']))) {
$errors['username'] = get_string('usernameoremail');
......@@ -57,11 +57,7 @@ class login_forgot_password_form extends moodleform {
}
}
if (0 == count($errors)){
return true;
} else {
return $errors;
}
return $errors;
}
}
......
......@@ -79,9 +79,9 @@ class login_signup_form extends moodleform {
$mform->applyFilter('username', 'trim');
}
function validation($data) {
function validation($data, $files) {
global $CFG;
$errors = array();
$errors = parent::validation($data, $files);
$authplugin = get_auth_plugin($CFG->registerauth);
......@@ -122,15 +122,12 @@ class login_signup_form extends moodleform {
}
$errmsg = '';
if (!check_password_policy($data['password'], $errmsg)) {
$errors['password'] = $errmsg;
}
if (0 == count($errors)){
return true;
} else {
return $errors;
}
return $errors;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment