Difference between revisions of "MediaWiki:Form.rythm"
Jump to navigation
Jump to search
Line 3: | Line 3: | ||
@// field definitions | @// field definitions | ||
@def static { | @def static { | ||
+ | // a form with fields | ||
class Form { | class Form { | ||
− | + | Map<String,Field> fieldMap=new HashMap<String,Field>(); | |
− | + | String title; | |
− | + | String title_de; | |
− | + | ||
− | + | public Form(Field[] fields, String title, String title_de) { | |
− | + | this.title=title; | |
− | + | this.title_de=title_de; | |
− | + | for (Field field:fields) { | |
+ | this.addField(field); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | public Collection<Field> getFields() { | ||
+ | return fieldMap.values(); | ||
+ | } | ||
+ | |||
+ | public void addField(Field field) { | ||
+ | fieldMap.put(field.name,field); | ||
+ | } | ||
+ | |||
+ | public Field getField(String fieldName) { | ||
+ | Field field=fieldMap.get(fieldName); | ||
+ | return field; | ||
+ | } | ||
} | } | ||
+ | // a field | ||
class Field { | class Field { | ||
String name; | String name; | ||
Line 64: | Line 82: | ||
} | } | ||
@// formvalidation | @// formvalidation | ||
− | @def formvalidate(boolean de, | + | @def formvalidate(boolean de,Form form) { |
<style> | <style> | ||
#success_message{ display: none;} | #success_message{ display: none;} | ||
Line 79: | Line 97: | ||
fields: @("{") | fields: @("{") | ||
@{ String delim="";} | @{ String delim="";} | ||
− | @for (Field field: | + | @for (Field field:form.getFields()) { |
@(delim)@(fieldvalidate(de,field)) | @(delim)@(fieldvalidate(de,field)) | ||
@{ delim=",";} | @{ delim=",";} | ||
Line 96: | Line 114: | ||
} | } | ||
@// show form | @// show form | ||
− | @def showform(boolean de, | + | @def showform(boolean de,Form form){ |
<form class="well form-horizontal" action=" " method="post" id="contact_form"> | <form class="well form-horizontal" action=" " method="post" id="contact_form"> | ||
<fieldset> | <fieldset> | ||
<!-- Form Name --> | <!-- Form Name --> | ||
− | <legend>@(de?title_de:title)</legend> | + | <legend>@(de?form.title_de:form.title)</legend> |
− | @for (Field field: | + | @for (Field field:form.getFields()) { |
@field(de,field) | @field(de,field) | ||
} | } |
Revision as of 12:10, 6 November 2017
Rythm template source
@// field definitions
@def static {
// a form with fields
class Form {
Map<String,Field> fieldMap=new HashMap<String,Field>();
String title;
String title_de;
public Form(Field[] fields, String title, String title_de) {
this.title=title;
this.title_de=title_de;
for (Field field:fields) {
this.addField(field);
}
}
public Collection<Field> getFields() {
return fieldMap.values();
}
public void addField(Field field) {
fieldMap.put(field.name,field);
}
public Field getField(String fieldName) {
Field field=fieldMap.get(fieldName);
return field;
}
}
// a field
class Field {
String name;
String label_en;
String label_de;
String placeholder_en;
String placeholder_de;
String icon;
int min;
public Field(String name,String label_en, String label_de, String placeholder_en,String placeholder_de, String icon, int min) {
this.name=name;
this.label_en=label_en;
this.label_de=label_de;
this.placeholder_de=placeholder_de;
this.placeholder_en=placeholder_en;
this.icon=icon;
this.min=min;
}
public Field(String name,String label_en, String label_de, String icon, int min) {
this(name,label_en,label_de,label_en,label_de,icon,min);
}
}
}
@// add a bootstrap field validation
@def fieldvalidate(boolean de,Field field) {
@(field.name): @("{")
validators: @("{")
stringLength: @("{")
min: @(field.min),
@("}"),
notEmpty: @("{")
message: '@(de?"Bitte "+field.label_de+" eingeben":"Please supply your "+field.label_en)'
@("}")
@("}")
@("}")
}
@// add a form field
@def field(boolean de,Field field) {
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label">@(de?field.label_de:field.label_en)</label>
<div class="col-md-4 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-@(field.icon)"></i></span>
<input name="@(field.name)" placeholder="@(de?field.placeholder_de:field.placeholder_en)" class="form-control" type="text">
</div>
</div>
</div>
}
@// formvalidation
@def formvalidate(boolean de,Form form) {
<style>
#success_message{ display: none;}
</style>
<script>
$(document).ready(function() @("{")
$('#contact_form').bootstrapValidator(@("{")
// To use feedback icons, ensure that you use Bootstrap v3.1.0 or later
feedbackIcons: @("{")
valid: 'glyphicon glyphicon-ok',
invalid: 'glyphicon glyphicon-remove',
validating: 'glyphicon glyphicon-refresh'
@("}"),
fields: @("{")
@{ String delim="";}
@for (Field field:form.getFields()) {
@(delim)@(fieldvalidate(de,field))
@{ delim=",";}
}
@("}")
@("}"))
.on('success.form.bv', function(e) @("{")
$('#success_message').slideDown(@("{") opacity: "show" @("}"), "slow") // Do something ...
$('#contact_form').data('bootstrapValidator').resetForm();
// Prevent form submission
e.preventDefault();
@("}"));
@("}"));
</script>
}
@// show form
@def showform(boolean de,Form form){
<form class="well form-horizontal" action=" " method="post" id="contact_form">
<fieldset>
<!-- Form Name -->
<legend>@(de?form.title_de:form.title)</legend>
@for (Field field:form.getFields()) {
@field(de,field)
}
<!-- Success message -->
<div class="alert alert-success" role="alert" id="success_message">Success <i class="glyphicon glyphicon-thumbs-up"></i> Thanks for contacting us, we will get back to you shortly.</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label"></label>
<div class="col-md-4">
<button type="submit" class="btn btn-warning" >Send <span class="glyphicon glyphicon-send"></span></button>
</div>
</div>
</fieldset>
</form>
}