diff --git a/doc/MANUAL.md b/doc/MANUAL.md index 1d434e8..dad78cf 100644 --- a/doc/MANUAL.md +++ b/doc/MANUAL.md @@ -16,6 +16,7 @@ This is the manual for version %%VERSION%%. - [Float](#field_types_float) - [Date](#field_types_date) - [Radio](#field_types_radio) + - [Checkbox](#field_types_checkbox) - [Select](#field_types_select) - [Text](#field_types_text) - [Password](#field_types_password) @@ -278,6 +279,16 @@ The `date` field type supports the following additional options: ### Radio +### Checkbox + +The `checkbox` field type represents the user with a toggleble checkbox that +can be either 'on' or 'off'. + +If the checkbox was checked, the value '`on`' is passed to the script. +Otherwise, '`off`' is passed. Unlike HTML forms, which send no value to the +server if the checkbox was not checked, Scriptform always sends either 'on' or +'off'. + ### Select ### Text diff --git a/examples/megacorp_acc/job_restart_acc.sh b/examples/megacorp_acc/job_restart_acc.sh index 877b72f..ca87644 100755 --- a/examples/megacorp_acc/job_restart_acc.sh +++ b/examples/megacorp_acc/job_restart_acc.sh @@ -6,4 +6,9 @@ if [ "$passwd" != "123foobar" ]; then fi echo "RESTARTING" + +if [ $no_db = "on" ]; then + echo "NOT RESTARTING DATABASE" +fi + ls -l /home/fboender diff --git a/examples/megacorp_acc/megacorp_acc.json b/examples/megacorp_acc/megacorp_acc.json index f20b1bb..d0ec1c2 100644 --- a/examples/megacorp_acc/megacorp_acc.json +++ b/examples/megacorp_acc/megacorp_acc.json @@ -70,6 +70,11 @@ "title": "Karl Karlsön gave you a password. What is it?", "type": "password", "required": true + }, + { + "name": "no_db", + "title": "Do not restart the database", + "type": "checkbox" } ] }, diff --git a/src/scriptform.py b/src/scriptform.py index ba1d5f0..38f15ce 100755 --- a/src/scriptform.py +++ b/src/scriptform.py @@ -460,6 +460,13 @@ class FormDefinition: "Invalid value for dropdown: {0}".format(value)) return value + def validate_checkbox(self, field_def, form_values): + value = form_values.get(field_def['name'], 'off') + if not value in ['on', 'off']: + raise ValidationError( + "Invalid value for checkbox: {0}".format(value)) + return value + def validate_text(self, field_def, form_values): value = form_values[field_def['name']] minlen = field_def.get('minlen', None) @@ -662,6 +669,7 @@ class ScriptFormWebApp(WebAppHandler): "password": u'', "text": u'', "select": u'', + "checkbox": u'', "radio": u'{3}
', } @@ -693,6 +701,8 @@ class ScriptFormWebApp(WebAppHandler): radio_elems.append(tpl.format(checked, field['name'], option[0], option[1])) checked = u'' # Check first radio option input = u''.join(radio_elems) + elif field['type'] == 'checkbox': + input = tpl.format(required, field['name']) elif field['type'] == 'text': rows = field.get('rows', 5) cols = field.get('cols', 80)