Comments (4)
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.
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.
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.
@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)
- afQuickField id field is overwritten - v7 HOT 9
- Array of objects set last object fields to null when remove unordered element HOT 6
- Cleanup issues, that are fixed by v7
- Problem with Meteor 1.11.1 Blazejs HOT 5
- Create a native 'autocomplete' type for `afQuickfield` in `AutoForm` that is Bootstrap themed. HOT 1
- Outdated links to SimpleSchema in README
- select-radio and select-radio-inline not working for update HOT 10
- minCount and maxCount undefined inside afEachArrayItem HOT 5
- quickForm and autoForm not displaying - no error messages. HOT 8
- Make moment a weak dependency
- Autoform hooks onSuccess not working HOT 13
- Demo link stale HOT 2
- Option list unwanted memory autosave form HOT 4
- Validation messages not showing - SimpleSchema 3.x HOT 4
- Why is there throttling on validateField HOT 2
- Remove global variables HOT 1
- Add coverage report to tests HOT 1
- Please add error/warning messages if no theme is installed HOT 4
- Cross - Posting: Incompatibility with Blaze-Async-PR HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from meteor-autoform.