Using Semantic UI form, modal and validation, I have a form in a modal and I do not want the modal to close if the form is not valid (which happens when submitted).
The only way I can stop the modal from closing is to add a disabled
class to the submit button.
I want to listen to the form (in real time) and toggle the disabled
class on and off depending on the current form validation state.
I can only get it to work once the form is submitted but not in real time
$('#myForm')
.form({
title: {
identifier : 'title',
rules: [
{
type : 'empty',
prompt : 'Please enter a title'
},
{
type : 'length[2]',
prompt : 'At least 6 characters'
}
]
}
},
{
onSuccess: function() {
$('#submit').removeClass('disabled');
},
onFailure: function() {
$('#submit').addClass('disabled');
}
}
);
Instead of using disabled for submit, make sure modal stays opened even with clicking on the submit (Modal approve) button, and pass the decision to close modal to semantic UI form validation events (i.e. onSuccess).
So something similar to this:
$('.ui.modal').modal({
onApprove : function() {
//Submits the semantic ui form
//And pass the handling responsibilities to the form handlers,
// e.g. on form validation success
$('.ui.form').submit();
//Return false as to not close modal dialog
return false;
}
});
var formValidationRules =
{
title: {
identifier : 'title',
rules: [
{
type : 'empty',
prompt : 'Please enter a title'
},
{
type : 'length[2]',
prompt : 'At least 6 characters'
}
]
}
}
var formSettings =
{
onSuccess : function()
{
//Hides modal on validation success
alert("Valid Submission, modal will close.");
$('.modal').modal('hide');
}
}
$('.ui.form').form(formValidationRules, formSettings);
Note that "OnApprove" event only fires when you click on the modal button with the class "ok". So you need a modal button like so:
<div class="ui ok green basic inverted button">
<i class="checkmark icon"></i>
Submit
</div>
Here's a extended working plunker code I created to demonstrate this: http://plnkr.co/edit/5fK7UuJIm7QTJGiu23NL?p=preview
I've faced the same problem, and contacted with Semantic developer. Take notice that they are going to revisit some additional, obvious, missing form behaviors after 2.0 launch. Now you can take a look at this branch on their github repo (the form.js file): https://github.com/Semantic-Org/Semantic-UI/tree/next
Make use a stopgap function is valid
which returns true/false
if form validates.
e.g.
if ( $('form').form('is valid') ) {
// do something
}
This will be in 2.0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With