Commit 03ebbc95 authored by Richard Mansfield's avatar Richard Mansfield
Browse files

Forms api creates js validation function for required rule, table renderer

parent 857c6390
// @todo: Pack it down.
// Expects strings array
function get_string(s) {
var args = flattenArguments(arguments).slice(1);
......@@ -35,27 +37,8 @@ function displayMessage(m, /* optional */ elemid) {
});
}
// Tests if elements with the 'required' class have content and
// displays the appropriate message.
// Uses the html output from form.php to find the title of required
// fields: <label for="elementid">Element title</label>
function testRequired(e,formid) {
if (hasElementClass(e,'required') && e.value == '') {
var labels = getElementsByTagAndClassName('label',null,formid);
for (var j = 0; j < labels.length; j++) {
if (getNodeAttribute(labels[j],'for') == e.name) {
displayMessage({'message':get_string('namedfieldempty',scrapeText(labels[j])),
'type':'error'});
return false;
}
}
displayMessage({'message':get_string('requiredfieldempty'),'type':'error'});
return false;
}
return true;
}
// The javascript form validating function should be available from
// the server as formname_validate().
// Gets form elements, submits them to a url via post, and waits for a
// JSON response containing the result of the submission.
......@@ -63,15 +46,13 @@ function submitForm(formid,url,callback) {
if (typeof(tinyMCE) != 'undefined') {
tinyMCE.triggerSave();
}
if (!eval(formid + '_validate()')) {
return false;
}
var formelements = getElementsByTagAndClassName(null,formid,formid);
var data = {};
for (var i = 0; i < formelements.length; i++) {
if (testRequired(formelements[i])) {
data[formelements[i].name] = formelements[i].value;
}
else {
return false;
}
data[formelements[i].name] = formelements[i].value;
}
var req = getXMLHttpRequest();
req.open('POST',url);
......
......@@ -409,7 +409,10 @@ class Form {
}
$result .= "</form>\n";
return $result;
$js_validator = '<script language="javascript" type="text/javascript">' . "\n"
. $this->validate_js() . "</script>\n";
return $result . $js_validator;
}
/**
......@@ -547,6 +550,42 @@ class Form {
}
}
/**
* Returns a js function to perform simple validation based off
* the definition array.
*/
private function validate_js() {
$result = 'function ' . $this->name . "_validate(){\nvar ok=true;\n";
foreach ($this->get_elements() as $element) {
if (isset($element['rules']) && is_array($element['rules'])) {
foreach ($element['rules'] as $rule => $data) {
// Get the rule
$function = 'form_rule_' . $rule . '_js';
if (!function_exists($function)) {
@include_once('form/rules/' . $rule . '.php');
}
if (function_exists($function)) {
$rdata = $function($element['name']);
$errmsgid = $element['name'] . '_errmsg';
$result .= 'if (!(' . $rdata->condition . ")){" ;
$result .= $this->name . '_set_error(\'' . $errmsgid . '\',\''
. $rdata->message . "');ok=false;}\n";
$result .= 'else{' . $this->name . '_rem_error(\'' . $errmsgid . "');}\n";
}
}
}
}
$result .= "return ok;\n}\n";
$js_error_function = 'form_renderer_' . $this->renderer . '_error_js';
if (!function_exists($js_error_function)) {
@include_once('form/renderers/' . $this->renderer . '.php');
if (!function_exists($js_error_function)) {
throw new FormException('No such renderer function "' . $js_error_function . '"');
}
}
return $result . $js_error_function($this->name);
}
/**
* Returns whether a field has an error marked on it.
*
......
......@@ -84,8 +84,11 @@ function form_renderer_table($builtelement, $rawelement) {
if (!empty($rawelement['error'])) {
$result .= "\t<tr>\n\t\t<td colspan=\"2\" class=\"errmsg\">";
$result .= hsc($rawelement['error']);
$result .= "</td>\n\t</tr>\n";
}
else {
$result .= "\t<tr style=\"display:none\" id=\"" . $rawelement['id'] . "_errmsg\"><td>";
}
$result .= "</td>\n\t</tr>\n";
return $result;
}
......@@ -98,4 +101,15 @@ function form_renderer_table_footer() {
return "</table>\n";
}
function form_renderer_table_error_js($id) {
$result = 'function ' . $id . '_set_error(id,m) {';
$result .= "swapDOM($(id),TR({'id':id},TD({'colspan':2,'class':'errmsg'},m)));";
$result .= "}\n";
$result .= 'function ' . $id . '_rem_error(id) {';
$result .= "swapDOM($(id),TR({'id':id,'style':'display:none;'},TD(null)));";
$result .= "}\n";
return $result;
}
?>
......@@ -39,4 +39,19 @@ function form_rule_required($field) {
}
}
/**
* Returns a javascript condition to check whether the field has been specified.
*
* @param string $id id of the field to check
* @return string js condition to check if the field is empty.
* string The error message, if the value is invalid.
*/
function form_rule_required_js($id) {
$r->condition = '$(\'' . $id . '\').value != \'\'';
$r->message = get_string('This field is required');
return $r;
}
?>
Markdown is supported
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