Semantic-UI icon indicating copy to clipboard operation
Semantic-UI copied to clipboard

[Form Validation] Clear errors

Open ShimShamSam opened this issue 12 years ago • 35 comments

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.

ShimShamSam avatar Mar 10 '14 05:03 ShimShamSam

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();

jlukic avatar Mar 11 '14 02:03 jlukic

Unfortunately, this does not remove inline errors and input styles. Would it make sense to automatically clear errors on a form's "reset" event?

ShimShamSam avatar Mar 11 '14 02:03 ShimShamSam

To my knowledge validate form will remove inline validations if they are valid.

It calls validate field which does that here

jlukic avatar Mar 11 '14 13:03 jlukic

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 );

deltoss avatar Dec 15 '14 06:12 deltoss

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.

hyperborea avatar Mar 17 '15 22:03 hyperborea

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:

  1. At initial load the page just did not hide the ui error message box.
  2. I could not validate the entire form but had to do it as the tab changed.
  3. Every time a tab changed I would reset the validation errors to the current tab loaded by calling the $('.ui.form').form('fields: {.....});
  4. However every time I went back to previous tab it would fire the validation for that tab.
  5. 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.

princeandrew01 avatar Jul 10 '15 14:07 princeandrew01

+1 for adding .form('clear errors')

avalanche1 avatar Oct 16 '15 11:10 avalanche1

@princeandrew01 , thanks for $('.ui.form').form('fields: {}) :smile:

avalanche1 avatar Oct 16 '15 12:10 avalanche1

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.

imilina avatar Nov 04 '15 06:11 imilina

+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.

LeMiniMoine avatar Nov 04 '15 09:11 LeMiniMoine

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.

LeMiniMoine avatar Nov 04 '15 16:11 LeMiniMoine

+1 seems like a primitive utility

talha-asad avatar Nov 30 '15 19:11 talha-asad

It works fine for me.

$(formElement).find('.error').removeClass('error');

craftzdog avatar Dec 05 '15 07:12 craftzdog

+1 for a built-in behavior/method. I think this is important for client side apps where state is kept in JavaScript.

Panman82 avatar Jan 11 '16 18:01 Panman82

+1 I need this once again

avalanche1 avatar Jan 19 '16 17:01 avalanche1

+1

ynnk avatar Feb 19 '16 05:02 ynnk

+1

The following was my fix (obviously absent any actual val sets):

$selector.form({fields:{}});
$selector.form('validate form');

rpsirois avatar Feb 20 '16 18:02 rpsirois

+1 for $(form).form('clear errors')

matantsu avatar Apr 14 '16 13:04 matantsu

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();

thiagoaag avatar Apr 18 '16 17:04 thiagoaag

I' using this $('.ui.error.message').empty();

d31m07y1988 avatar Mar 21 '17 19:03 d31m07y1988