Commit 5e405159 authored by Nigel McNie's avatar Nigel McNie Committed by Nigel McNie
Browse files

Provide JS for getting the radio value when doing an AJAX request

parent f078e29b
......@@ -44,4 +44,10 @@ function form_render_cancel($element, Form $form) {
. ' value="' . hsc($form->get_value($element)) . '">';
}
function form_get_value_js_cancel($element, $form) {
$formname = $form->get_name();
$name = $element['name'];
return " data['$name'] = document.forms['$formname'].elements['{$name}_cancel'].value;\n";
}
?>
......@@ -35,7 +35,7 @@ defined('INTERNAL') || die();
*/
function form_render_radio($element, Form $form) {
if (!isset($element['options']) || !is_array($element['options']) || count($element['options']) < 1) {
log_warn('Radio elements should have at least one option');
throw new FormException('Radio elements should have at least one option');
}
$result = '';
......@@ -61,6 +61,29 @@ function form_render_radio($element, Form $form) {
return $result;
}
/**
* radio doesn't need a function to get a value from phpland because it comes
* through correctly from the request... however in javascript land things are
* harder.
*
* @todo maybe later: make the get_value_js functions return a javascript function,
* to keep their stuff in its own scope. Maybe. If js scoping rules mean this will help.
*/
function form_get_value_js_radio($element, Form $form) {
$formname = $form->get_name();
$name = $element['name'];
return <<<EOF
var radio = document.forms['$formname'].elements['$name'];
for (var i = 0; i < radio.length; i++) {
if (radio[i].checked) {
data['$name'] = radio[i].value;
break;
}
}
EOF;
}
function form_render_radio_set_attributes($element) {
$element['nolabel'] = true;
$element['rules']['validateoptions'] = true;
......
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