Giter Club home page Giter Club logo

Comments (4)

jankapunkt avatar jankapunkt commented on May 25, 2024

Hi @crimsonrocks could you please provide a minimal example that I can reproduce? From there we can Talk about your issue and whether it can be solved using a different Code/API usage or by implementing a new behavior.

from meteor-autoform.

crimsonrocks avatar crimsonrocks commented on May 25, 2024
AutoForm.hooks({
    SomeForm: {
        before : {
            "method" : function(insertDoc){
                let determiner;
                Meteor.call("some.method",someData,function(err,result){
                    if(err){
                        determiner = false;
                        console.log(err.reason);
                    }else{
                       determiner = true;
                       // Do some logic here, such as updating the form doc
                    }
                });
                return ((determiner) ? insertDoc : false);
            },
        },
        // Other hooks
},

});

I'm playing around with a payments system, and one of the features I was thinking about was something like a 4 digit pin code that a user could add to their account if they wanted to. Anytime they're about to pay for something the keypad pops up and asks them to input their code, and hits up a method call to validate it. If validation succeeds the form is submitted, otherwise it fails.

I haven't come up with a better way of doing this other than running a method call in the before hook and trying to get it to wait for the result of that call before continuing. I know methods on the client are asynchronous, but there must be some way to achieve this with AutoForm I think.

from meteor-autoform.

jankapunkt avatar jankapunkt commented on May 25, 2024

You could use a "normal" form and to the validation and submission manually:

{{> quickForm id="testForm" type="normal" schema=testSchema}}
Template.myTemplate.events({
  'submit #testForm' (event, templateInstance) {
    // prevent auto submittion:
    event.preventDefault()

    // get form values
    const { insertDoc } = AutoForm.getFormValues('testForm')

    // validate form data but do not validate the pin here, 
    // just it's type (String or Number; length etc.)
    const ctx = testSchema.newContext()
    ctx.validate(insertDoc, { modifier: false }) // true if updateDoc
    const errors = context.validationErrors()

    // if we have already errors at this stage, add them to form and skip the rest!
    if (errors && errors.length > 0) {
      errors.forEach(err => AutoForm.addStickyValidationError('testForm', err.key, err.type, err.value))
      return
    }
    
    // now let's validate the PIN
    const { pin } = insertDoc
    Meteor.call('validatePinMethod', { pin }, err => {
      // if the server threw an error, add the error to the form and skip
      // see https://github.com/longshotlabs/simpl-schema/blob/main/package/lib/SimpleSchema.js#L906
      if (err) {
        return AutoForm.addStickyValidationError('testForm', 'pin', 'notAllowed', [pin])
      }
      
      // do not forget to remove the error if it passed
      else {
        AutoForm.removeStickyValidationError('testForm', 'pin')
      }
      
      // otherwise submit data to server
      Meteor.call('submitForm', insertDoc, (submissionError, res) => { ... })
    })
  }
})

Note: I would keep the pin validation separate from data submission in case you need to validate it anywhere else.
Would that solve your problem?

from meteor-autoform.

crimsonrocks avatar crimsonrocks commented on May 25, 2024

@jankapunkt that completely solves my problem! I honestly forgot about 'normal' forms. Thanks for taking the time to write that out.

from meteor-autoform.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.