javascript - Semantic UI: How to prevent form from being submitted when validation fails? - Stack Overflow

I have a basic login form and I've specified the form validations and API call in my javascript fi

I have a basic login form and I've specified the form validations and API call in my javascript file. The problem that I have is, when I click the login button and the form has errors, the invalid fields will be highlighted but the API call is still made, even though the form is invalid.

Here's a simplified example:

<form class="ui form">
  <div class="field">
    <input name="username" type="text" placeholder="Username" autofocus>
  </div>
  <div class="field">
    <input name="password" type="password" placeholder="Password">
  </div>
  <button type="submit" class="ui submit button">Login</button>
  <div class="ui error message"></div>
</form>

$(function() {

    $('form').form({
        username: {
            identifier: 'username',
            rules: [{
                type: 'empty',
                prompt: 'Please enter your username'
            }]
        },
        password: {
            identifier: 'password',
            rules: [{
                type: 'empty',
                prompt: 'Please enter your password'
            }]
        }
    }, {
        onFailure: function() {
            // prevent form submission (doesn't work)
            return false;
        }
    });

    // *** this API call should not be made when the form is invalid ***
    $('form .submit.button').api({
        action: 'login',
        method: 'post',
        serializeForm: true,
        dataType: 'text',
        onSuccess: function() {
            // todo
        },
        onError: function() {
            // todo
        }
    });

});

I also have a punkr here which demonstrates the issue I'm having.

Did I miss something? Are the .api() and .form() calls correct?

I have a basic login form and I've specified the form validations and API call in my javascript file. The problem that I have is, when I click the login button and the form has errors, the invalid fields will be highlighted but the API call is still made, even though the form is invalid.

Here's a simplified example:

<form class="ui form">
  <div class="field">
    <input name="username" type="text" placeholder="Username" autofocus>
  </div>
  <div class="field">
    <input name="password" type="password" placeholder="Password">
  </div>
  <button type="submit" class="ui submit button">Login</button>
  <div class="ui error message"></div>
</form>

$(function() {

    $('form').form({
        username: {
            identifier: 'username',
            rules: [{
                type: 'empty',
                prompt: 'Please enter your username'
            }]
        },
        password: {
            identifier: 'password',
            rules: [{
                type: 'empty',
                prompt: 'Please enter your password'
            }]
        }
    }, {
        onFailure: function() {
            // prevent form submission (doesn't work)
            return false;
        }
    });

    // *** this API call should not be made when the form is invalid ***
    $('form .submit.button').api({
        action: 'login',
        method: 'post',
        serializeForm: true,
        dataType: 'text',
        onSuccess: function() {
            // todo
        },
        onError: function() {
            // todo
        }
    });

});

I also have a punkr here which demonstrates the issue I'm having.

Did I miss something? Are the .api() and .form() calls correct?

Share Improve this question edited Dec 9, 2014 at 8:13 CodingIntrigue 78.7k32 gold badges176 silver badges177 bronze badges asked Dec 9, 2014 at 8:07 Decade MoonDecade Moon 34.3k8 gold badges82 silver badges104 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

Ok I figured it out. All I need to do is change

$('form .submit.button').api(...

to

$('form').api(...

I didn't realise that I could call .api() directly on the <form> element. Now the api call isn't made when the form is invalid because the form isn't submitted (previously I had the api call on the submit button which isn't cancelled when the form is invalid).

Use onSuccess event instead of api method.

$('form').form({
        username: {
            identifier: 'username',
            rules: [{
                type: 'empty',
                prompt: 'Please enter your username'
            }]
        },
        password: {
            identifier: 'password',
            rules: [{
                type: 'empty',
                prompt: 'Please enter your password'
            }]
        }
    }, {
        onFailure: function() {
            // prevent form submission (doesn't work)
            return false;
        },
         onSuccess:function(event,fields){
            // prevent form submission
            // api / ajax call
          return false;
        }
    });

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745368251a4624681.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信