Semantic Dynamic Form Validations
HTML
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.1.8/semantic.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.1.8/semantic.js"></script>
<br><br>
<form class="ui form">
<div class="fields two">
<div class="field">
<label>Empty</label>
<input name="emptyanything" type="text">
</div>
<div class="field">
<label>Dropdown</label>
<select class="ui dropdown" name="dropdown">
<option value="">Select</option>
<option value="male">Choice 1</option>
<option value="female">Choice 2</option>
</select>
</div>
</div>
<div class="ui submit button">Submit</div>
<div class="ui button add">Add</div>
<div class="ui error message"></div>
</form>
JavaScript
$(document).ready(function(){
//$('form').form('validate');
$('.submit').click(function(){
//alert("sdfg");
//$('form').form('validate .form');
console.log($('form'));
var $inputs = $('.form');
console.log($inputs[0].elements);
console.log($inputs.serializeArray());
var form_fields = {};
$all_form_fields = $inputs.serializeArray();
for(var cnt in $all_form_fields)
{
var single_field = {};
single_field.identifier = $all_form_fields[cnt].name ;
single_field.rules = [
{
type : 'empty',
prompt : 'Please enter a valid {name}'
}
] ;
form_fields[$all_form_fields[cnt].name] = single_field;
}
console.log(form_fields);
$('form').form( {
fields : form_fields,
inline : true
});
});
var count = 1;
$(document).on('click', '.ui.button.add', function() {
var template = '<div class="fields two"> \
<div class="field">\
<label>Empty</label>\
<input name="emptyanything['+count+']" type="text">\
</div>\
<div class="field">\
<label>Dropdown</label>\
<select class="ui dropdown" name="dropdown['+count+']">\
<option value="">Select</option>\
<option value="male">Choice 1</option>\
<option value="female">Choice 2</option>\
</select>\
</div>\
</div>' ;
// $('form.ui.form').append(template);
$(template).insertBefore('.ui.submit');
count++;
});
//------------------------Ignore below Code. This only for Understanding purpose--------------------//
/*var validationRules = {
emptyanything: {
identifier : 'emptyanything',
rules: [
{
type : 'empty',
prompt : 'Please enter a value'
}
]
},
dropdown: {
identifier : 'dropdown',
rules: [
{
type : 'empty',
prompt : 'Please select a {name}'
}
]
},
...