Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

form submit with validate by default #207

Open
belozer opened this issue Aug 18, 2016 · 1 comment
Open

form submit with validate by default #207

belozer opened this issue Aug 18, 2016 · 1 comment

Comments

@belozer
Copy link
Member

belozer commented Aug 18, 2016

Now:

    _onSubmit : function(e) {
        e.preventDefault();
        this._emit('submit', this.getVal());
    },

With validate by default:

    _onSubmit: function(e, data) {
      e.preventDefault();
      this.validate().then(fieldsStatuses => {
        if (this.checkFields(fieldsStatuses)) {
          this._emit('submit', this.getVal());
        } else {
          this.getInvalidFields().then(invalidFields => {
            invalidFields[0].getControl().setMod('focused');
          });
        }
      });
    }

or add new event success, or data

@belozer
Copy link
Member Author

belozer commented Aug 18, 2016

in form.browser.js

    _onSubmit: function(e, data) {
      e.preventDefault();
      this._emit('submit', this.getVal());
      this.validate().then(fieldsStatuses => {
        if (this.checkFields(fieldsStatuses)) {
          this._emit('success', this.getVal());
        } else {
          this.getInvalidFields().then(invalidFields => {
            this._emit('error', this.getVal());
            invalidFields[0].getControl().setMod('focused');
          });
        }
      });
    }

in user block:

modules.define('form-contacts',
['i-bem-dom', 'form', 'store-auth'],
function(provide, bemDom, Form, auth) {
  provide(bemDom.declBlock(this.name, /** @lends app.prototype */{
    onSetMod: {
      js: {
        inited: function() {
          this._form = this.findChildBlock(Form);
          this._form._events().on('success', this._onFormSuccess, this);
        }
      }
    },

    _onFormSuccess: function(e, data) {
      console.log(data);
    }
  }));
});

or

/**
 * @module app
 */
modules.define('form-login',
  ['i-bem-dom', 'form', 'jquery', 'store-auth'],
  function(provide, bemDom, Form, $, auth) {
    provide(bemDom.declBlock(this.name, /** @lends app.prototype */{
      onSetMod: {
        js: {
          inited: function() {
            this._form = this.findChildBlock(Form);
            this._form._events().on('success', (e, data) => auth.login(data));
          }
        }
      }
    }));
  });

Very simple in use 👍

@belozer belozer changed the title form submit and validate by default form submit with validate by default Aug 18, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant