[Form Validation] Clear errors
Is there a way to clear all errors on a form? A use case would be a modal form that comes up several times over the lifetime of a single page.
There's no dedicated behavior for clearing all errors. I will mark this as an enhancement as the use-case seems essential.
Currently calling $('.form').form('validate form'); will clear errors if the form passes validation. You can also clear errors using $('.form .message').hide();
Unfortunately, this does not remove inline errors and input styles. Would it make sense to automatically clear errors on a form's "reset" event?
To my knowledge validate form will remove inline validations if they are valid.
It calls validate field which does that here
The problem with using $('.form .message').hide(); is after you try submit the form a second time, and form is invalid, the error messages would not show. What that hide() does is modify the inline css style which takes precedence and overrides the semantic css styling based on classes.
The way I went about this is removing the "error" class from the ui form and ui fields. Here's the jquery code:
var aFunction = function()
{
//#aBtn resets semantic UI modal form and then displays it
$("#aBtn").on("click", function(){
//Resets form input fields from data values
$('.ui.form').trigger("reset");
//Resets form error messages and field styles
$('.ui.form .field.error').removeClass( "error" );
$('.ui.form.error').removeClass( "error" );
$('.modal').modal('show');
});
}
$(document).ready( aFunction );
Any news on this one? I need to set values to a default set, which shouldn't trigger validation straight away (as it won't and doesn't have to pass it). Unfortunately "set values" does exactly that.
Not sure if this will help anyone out there but this is an example of something I had done with tabs:
I had three tabs representing one form. It was a ton of fields in the form so splitting it into three tabs seemed like a logical choice. Additionally I didn't have to submit each tab before going to the next tab. Back to the issue, I was experiencing the following:
- At initial load the page just did not hide the ui error message box.
- I could not validate the entire form but had to do it as the tab changed.
- Every time a tab changed I would reset the validation errors to the current tab loaded by calling the $('.ui.form').form('fields: {.....});
- However every time I went back to previous tab it would fire the validation for that tab.
- To solve the problem in both 1 and 4 I created a dummy hidden field and called validation on it when it was equal to a value (essentially it would always be valid). I then did an each on the .error.field and removed the class error.
That was the only way I could clear the errors and make sure when going backwards in tabs it did not validate and hide the error message on top of the page.
I hope this helps someone who maybe stuck as well, additionally anyone who has any suggestions on how I could have solved this problem differently would be helpful!
EDIT: I forgot to mention clearing the class error does not remove inline error messages. Since my form does not use them I didn't have to but I suspect a .remove() would remove the error inline as necessary.
+1 for adding .form('clear errors')
@princeandrew01 , thanks for $('.ui.form').form('fields: {}) :smile:
Also +1 for adding .form('clear errors'). Another use case would be SPA javascript applications where underlying data is changed but html form is reused.
+1 for adding this. My problem is that I have a button to revert the changes in a form if I want to go back to original pre-filled form. If error messages were there, they never disappear. Using 'reset' clears all the fields and the errors but then all fields are empty.
Hey all,
Finally I managed to make it work by using a part of semanticUI source code. I'm using Angular for the controller so I created a directive to add to the form.
Here is a code snippet that might help:
var clearErrors = function () {
var $field = $(formElement).find('input, textarea, select'),
$group = $(formElement).find('.field');
$field
.each(function () {
var
$field = $(this),
$fieldGroup = $field.closest($group),
$prompt = $fieldGroup.find('.prompt.label'),
isErrored = $fieldGroup.hasClass('error');
if(isErrored) {
$fieldGroup.removeClass('error');
$prompt.remove();
}
});
};
Basically, I just used some parts of the form.reset() function from semantic.js without the clear() part.
+1 seems like a primitive utility
It works fine for me.
$(formElement).find('.error').removeClass('error');
+1 for a built-in behavior/method. I think this is important for client side apps where state is kept in JavaScript.
+1 I need this once again
+1
+1
The following was my fix (obviously absent any actual val sets):
$selector.form({fields:{}});
$selector.form('validate form');
+1 for $(form).form('clear errors')
I'm using on a little project. maybe can help!
// remove list of errors
$('form-id').find('.ui.error.message ul').remove();
remove prompt
$('form-id').find('.error').removeClass('error').find('.prompt').remove();
I' using this $('.ui.error.message').empty();