Skip to content

Commit

Permalink
MDL-51108 mod_data: Add validation for Lat. and Long.
Browse files Browse the repository at this point in the history
Added a check to see that both the Lat. and Long. values
are entered when filling in database module entries.
  • Loading branch information
abgreeve committed Sep 17, 2015
1 parent 2dd2288 commit b44fd91
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
23 changes: 23 additions & 0 deletions mod/data/field/latlong/field.class.php
Expand Up @@ -253,4 +253,27 @@ function notemptyfield($value, $name) {
return isset($value) && !($value == '');
}

/**
* Validate values for this field.
* Both the Latitude and the Longitude fields need to be filled in.
*
* @since Moodle 2.9.2
* @param array $values The entered values for the lat. and long.
* @return string|bool Error message or false.
*/
public function field_validation($values) {
$valuecount = 0;
// The lat long class has two values that need to be checked.
foreach ($values as $value) {
if (isset($value->value) && !($value->value == '')) {
$valuecount++;
}
}
// If we have nothing filled in or both filled in then everything is okay.
if ($valuecount == 0 || $valuecount == 2) {
return false;
}
// If we get here then only one field has been filled in.
return get_string('latlongboth', 'data');
}
}
1 change: 1 addition & 0 deletions mod/data/lang/en/data.php
Expand Up @@ -207,6 +207,7 @@
$string['jstemplate'] = 'Javascript template';
$string['latitude'] = 'Latitude';
$string['latlong'] = 'Latitude/longitude';
$string['latlongboth'] = 'Both the Latitude and the Longitude must be filled in.';
$string['latlongdownloadallhint'] = 'Download link for all entries as KML';
$string['latlongkmllabelling'] = 'How to label items in KML files (Google Earth)';
$string['latlonglinkservicesdisplayed'] = 'Link-out services to display';
Expand Down
11 changes: 10 additions & 1 deletion mod/data/lib.php
Expand Up @@ -3829,6 +3829,7 @@ function data_process_submission(stdClass $mod, $fields, stdClass $datarecord) {
// Empty form checking - you can't submit an empty form.
$emptyform = true;
$requiredfieldsfilled = true;
$fieldsvalidated = true;

// Store the notifications.
$result->generalnotifications = array();
Expand Down Expand Up @@ -3866,6 +3867,14 @@ function data_process_submission(stdClass $mod, $fields, stdClass $datarecord) {

$field = data_get_field($fieldrecord, $mod);
if (isset($submitteddata[$fieldrecord->id])) {
// Field validation check.
if (method_exists($field, 'field_validation')) {
$errormessage = $field->field_validation($submitteddata[$fieldrecord->id]);
if ($errormessage) {
$result->fieldnotifications[$field->field->name][] = $errormessage;
$fieldsvalidated = false;
}
}
foreach ($submitteddata[$fieldrecord->id] as $fieldname => $value) {
if ($field->notemptyfield($value->value, $value->fieldname)) {
// The field has content and the form is not empty.
Expand Down Expand Up @@ -3897,7 +3906,7 @@ function data_process_submission(stdClass $mod, $fields, stdClass $datarecord) {
$result->generalnotifications[] = get_string('emptyaddform', 'data');
}

$result->validated = $requiredfieldsfilled && !$emptyform;
$result->validated = $requiredfieldsfilled && !$emptyform && $fieldsvalidated;

return $result;
}

0 comments on commit b44fd91

Please sign in to comment.