Skip to content

Commit 3f219ad

Browse files
committed
rearrange and fix #831
1 parent 2520f04 commit 3f219ad

File tree

7 files changed

+171
-110
lines changed

7 files changed

+171
-110
lines changed

api.md

Lines changed: 54 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ manner:
221221

222222
-
223223

224-
### <a name="AutoForm.getFormValues"></a>*AutoForm*.getFormValues(formId, [template], [ss])&nbsp;&nbsp;<sub><i>Client</i></sub> ###
224+
### <a name="AutoForm.getFormValues"></a>*AutoForm*.getFormValues(formId, [template], [ss], [getModifier])&nbsp;&nbsp;<sub><i>Client</i></sub> ###
225225

226226
*This method __getFormValues__ is defined in `AutoForm`*
227227

@@ -239,15 +239,18 @@ __Arguments__
239239

240240
The SimpleSchema instance, if already known, as a performance optimization.
241241

242+
* __getModifier__ *{Boolean}* (Optional)
243+
244+
Set to `true` to return a modifier object or `false` to return a normal object. For backwards compatibility, and object containing both is returned if this is undefined.
245+
242246

243247
__Returns__ *{Object}*
244248

245249

246250
Returns an object representing the current values of all schema-based fields in the form.
247-
The returned object contains two properties, "insertDoc" and "updateDoc", which represent
248-
the field values as a normal object and as a MongoDB modifier, respectively.
251+
The returned object is either a normal object or a MongoDB modifier, based on the `getModifier` argument.
249252

250-
> ```AutoForm.getFormValues = function autoFormGetFormValues(formId, template, ss) { ...``` [autoform-api.js:266](autoform-api.js#L266)
253+
> ```AutoForm.getFormValues = function autoFormGetFormValues(formId, template, ss, getModifier) { ...``` [autoform-api.js:266](autoform-api.js#L266)
251254
252255

253256
-
@@ -273,7 +276,7 @@ __Returns__ *{Any}*
273276
Returns the value of the field (the value that would be used if the form were submitted right now).
274277
This is a reactive method that will rerun whenever the current value of the requested field changes.
275278

276-
> ```AutoForm.getFieldValue = function autoFormGetFieldValue(fieldName, formId) { ...``` [autoform-api.js:386](autoform-api.js#L386)
279+
> ```AutoForm.getFieldValue = function autoFormGetFieldValue(fieldName, formId) { ...``` [autoform-api.js:409](autoform-api.js#L409)
277280
278281

279282
-
@@ -294,7 +297,7 @@ __Returns__ *{String}*
294297

295298
Returns the name of the template used to render the element.
296299

297-
> ```AutoForm.getInputTypeTemplateNameForElement = function autoFormGetInputTypeTemplateNameForElement(element) { ...``` [autoform-api.js:413](autoform-api.js#L413)
300+
> ```AutoForm.getInputTypeTemplateNameForElement = function autoFormGetInputTypeTemplateNameForElement(element) { ...``` [autoform-api.js:437](autoform-api.js#L437)
298301
299302

300303
-
@@ -320,7 +323,7 @@ __Returns__ *{Any}*
320323
Returns the value of the field (the value that would be used if the form were submitted right now).
321324
Unlike `AutoForm.getFieldValue`, this function is not reactive.
322325

323-
> ```AutoForm.getInputValue = function autoFormGetInputValue(element, ss) { ...``` [autoform-api.js:441](autoform-api.js#L441)
326+
> ```AutoForm.getInputValue = function autoFormGetInputValue(element, ss) { ...``` [autoform-api.js:465](autoform-api.js#L465)
324327
325328

326329
-
@@ -349,7 +352,7 @@ __Returns__ *{undefined}*
349352

350353
Use this method to add custom input components.
351354

352-
> ```AutoForm.addInputType = function afAddInputType(name, definition) { ...``` [autoform-api.js:527](autoform-api.js#L527)
355+
> ```AutoForm.addInputType = function afAddInputType(name, definition) { ...``` [autoform-api.js:555](autoform-api.js#L555)
353356
354357

355358
-
@@ -398,7 +401,7 @@ __Returns__ *{undefined}*
398401

399402
Use this method to add custom form types.
400403

401-
> ```AutoForm.addFormType = function afAddFormType(name, definition) { ...``` [autoform-api.js:548](autoform-api.js#L548)
404+
> ```AutoForm.addFormType = function afAddFormType(name, definition) { ...``` [autoform-api.js:576](autoform-api.js#L576)
402405
403406

404407
-
@@ -429,7 +432,7 @@ Is it valid?
429432
In addition to returning a boolean that indicates whether the field is currently valid,
430433
this method causes the reactive validation messages to appear.
431434

432-
> ```AutoForm.validateField = function autoFormValidateField(formId, fieldName, skipEmpty) { ...``` [autoform-api.js:565](autoform-api.js#L565)
435+
> ```AutoForm.validateField = function autoFormValidateField(formId, fieldName, skipEmpty) { ...``` [autoform-api.js:593](autoform-api.js#L593)
433436
434437

435438
-
@@ -452,7 +455,7 @@ Is it valid?
452455
In addition to returning a boolean that indicates whether the form is currently valid,
453456
this method causes the reactive validation messages to appear.
454457

455-
> ```AutoForm.validateForm = function autoFormValidateForm(formId) { ...``` [autoform-api.js:583](autoform-api.js#L583)
458+
> ```AutoForm.validateForm = function autoFormValidateForm(formId) { ...``` [autoform-api.js:611](autoform-api.js#L611)
456459
457460

458461
-
@@ -475,7 +478,7 @@ The SimpleSchema validation context object.
475478
Use this method to get the validation context, which can be used to check
476479
the current invalid fields, manually invalidate fields, etc.
477480

478-
> ```AutoForm.getValidationContext = function autoFormGetValidationContext(formId) { ...``` [autoform-api.js:599](autoform-api.js#L599)
481+
> ```AutoForm.getValidationContext = function autoFormGetValidationContext(formId) { ...``` [autoform-api.js:645](autoform-api.js#L645)
479482
480483

481484
-
@@ -497,7 +500,7 @@ Searches for the given attribute, looking up the parent context tree until the c
497500

498501
Call this method from a UI helper. Might return undefined.
499502

500-
> ```AutoForm.findAttribute = function autoFormFindAttribute(attrName) { ...``` [autoform-api.js:612](autoform-api.js#L612)
503+
> ```AutoForm.findAttribute = function autoFormFindAttribute(attrName) { ...``` [autoform-api.js:658](autoform-api.js#L658)
501504
502505

503506
-
@@ -519,7 +522,7 @@ An object containing all of the found attributes and their values, with the pref
519522

520523
Call this method from a UI helper. Searches for attributes that start with the given prefix, looking up the parent context tree until the closest autoform is reached.
521524

522-
> ```AutoForm.findAttributesWithPrefix = function autoFormFindAttributesWithPrefix(prefix) { ...``` [autoform-api.js:658](autoform-api.js#L658)
525+
> ```AutoForm.findAttributesWithPrefix = function autoFormFindAttributesWithPrefix(prefix) { ...``` [autoform-api.js:699](autoform-api.js#L699)
523526
524527

525528
-
@@ -532,7 +535,7 @@ Call this method from a UI helper. Searches for attributes that start with the g
532535
Call this method in client code while developing to turn on extra logging.
533536
You need to call it just one time, usually in top level client code.
534537

535-
> ```AutoForm.debug = function autoFormDebug() { ...``` [autoform-api.js:707](autoform-api.js#L707)
538+
> ```AutoForm.debug = function autoFormDebug() { ...``` [autoform-api.js:743](autoform-api.js#L743)
536539
537540

538541
-
@@ -542,7 +545,7 @@ You need to call it just one time, usually in top level client code.
542545
*This property __arrayTracker__ is defined in `AutoForm`*
543546

544547

545-
> ```AutoForm.arrayTracker = arrayTracker;``` [autoform-api.js:721](autoform-api.js#L721)
548+
> ```AutoForm.arrayTracker = arrayTracker;``` [autoform-api.js:757](autoform-api.js#L757)
546549
547550

548551
-
@@ -564,7 +567,7 @@ The input type. Most are the same as the `type` attributes for HTML input elemen
564567

565568
Call this method from a UI helper to get the type string for the input control.
566569

567-
> ```AutoForm.getInputType = function getInputType(atts) { ...``` [autoform-api.js:731](autoform-api.js#L731)
570+
> ```AutoForm.getInputType = function getInputType(atts) { ...``` [autoform-api.js:767](autoform-api.js#L767)
568571
569572

570573
-
@@ -586,7 +589,7 @@ __Returns__ *{Object}*
586589
Call this method from a UI helper to get the field definitions based on the schema used by the closest containing autoForm.
587590
Always throws an error or returns the schema object.
588591

589-
> ```AutoForm.getSchemaForField = function autoFormGetSchemaForField(name) { ...``` [autoform-api.js:818](autoform-api.js#L818)
592+
> ```AutoForm.getSchemaForField = function autoFormGetSchemaForField(name) { ...``` [autoform-api.js:854](autoform-api.js#L854)
590593
591594

592595
-
@@ -607,7 +610,7 @@ __Returns__ *{Array(Object)|String|undefined}*
607610

608611
Call this method from a UI helper to get the select options for the field. Might return the string "allowed".
609612

610-
> ```AutoForm._getOptionsForField = function autoFormGetOptionsForField(name) { ...``` [autoform-api.js:831](autoform-api.js#L831)
613+
> ```AutoForm._getOptionsForField = function autoFormGetOptionsForField(name) { ...``` [autoform-api.js:867](autoform-api.js#L867)
611614
612615

613616
-
@@ -629,7 +632,7 @@ __Returns__ *{Object}*
629632
Call this method from a UI helper to get the field definitions based on the schema used by the closest containing autoForm.
630633
Always throws an error or returns the schema object.
631634

632-
> ```AutoForm.getLabelForField = function autoFormGetSchemaForField(name) { ...``` [autoform-api.js:872](autoform-api.js#L872)
635+
> ```AutoForm.getLabelForField = function autoFormGetSchemaForField(name) { ...``` [autoform-api.js:908](autoform-api.js#L908)
633636
634637

635638
-
@@ -651,7 +654,29 @@ The template instance.
651654

652655
Gets the template instance for the form with formId or the closest form to the current context.
653656

654-
> ```AutoForm.templateInstanceForForm = function (formId) { ...``` [autoform-api.js:891](autoform-api.js#L891)
657+
> ```AutoForm.templateInstanceForForm = function (formId) { ...``` [autoform-api.js:927](autoform-api.js#L927)
658+
659+
660+
-
661+
662+
### <a name="AutoForm.viewForForm"></a>*AutoForm*.viewForForm([formId])&nbsp;&nbsp;<sub><i>Client</i></sub> ###
663+
664+
*This method __viewForForm__ is defined in `AutoForm`*
665+
666+
__Arguments__
667+
668+
* __formId__ *{String}* (Optional)
669+
670+
The form's `id` attribute. Do not pass this if calling from within a form context.
671+
672+
673+
__Returns__ *{Blaze.View}*
674+
The `Blaze.View` instance for the autoForm. Always returns the view or throws an error.
675+
676+
677+
Gets the `Blaze.View` instance for the form with formId or the closest form to the current context.
678+
679+
> ```AutoForm.viewForForm = function (formId) { ...``` [autoform-api.js:939](autoform-api.js#L939)
655680
656681

657682
-
@@ -679,7 +704,7 @@ Looks in the document attached to the form to see if the
679704
requested field exists and is an array. If so, returns the
680705
length (count) of the array. Otherwise returns undefined.
681706

682-
> ```AutoForm.getArrayCountFromDocForField = function (formId, field) { ...``` [autoform-api.js:928](autoform-api.js#L928)
707+
> ```AutoForm.getArrayCountFromDocForField = function (formId, field) { ...``` [autoform-api.js:978](autoform-api.js#L978)
683708
684709

685710
-
@@ -704,7 +729,7 @@ or throws an error.
704729
You can call this without a formId from within a helper and
705730
the data for the nearest containing form will be returned.
706731

707-
> ```AutoForm.getCurrentDataForForm = function (formId) { ...``` [autoform-api.js:951](autoform-api.js#L951)
732+
> ```AutoForm.getCurrentDataForForm = function (formId) { ...``` [autoform-api.js:1001](autoform-api.js#L1001)
708733
709734

710735
-
@@ -729,7 +754,7 @@ Always returns an object or throws an error.
729754
You can call this without a formId from within a helper and
730755
the data for the nearest containing form will be returned.
731756

732-
> ```AutoForm.getCurrentDataPlusExtrasForForm = function (formId) { ...``` [autoform-api.js:992](autoform-api.js#L992)
757+
> ```AutoForm.getCurrentDataPlusExtrasForForm = function (formId) { ...``` [autoform-api.js:1016](autoform-api.js#L1016)
733758
734759

735760
-
@@ -751,7 +776,7 @@ The Collection instance
751776

752777
Gets the collection for a form from the `collection` attribute
753778

754-
> ```AutoForm.getFormCollection = function (formId) { ...``` [autoform-api.js:1015](autoform-api.js#L1015)
779+
> ```AutoForm.getFormCollection = function (formId) { ...``` [autoform-api.js:1039](autoform-api.js#L1039)
755780
756781

757782
-
@@ -780,7 +805,7 @@ provided, or from the schema attached to the `Mongo.Collection`
780805
specified in the `collection` attribute. The form must be
781806
currently rendered.
782807

783-
> ```AutoForm.getFormSchema = function (formId, form) { ...``` [autoform-api.js:1032](autoform-api.js#L1032)
808+
> ```AutoForm.getFormSchema = function (formId, form) { ...``` [autoform-api.js:1056](autoform-api.js#L1056)
784809
785810

786811
-
@@ -795,7 +820,7 @@ The containing form's `id` attribute value
795820

796821
Call in a helper to get the containing form's `id` attribute. Reactive.
797822

798-
> ```AutoForm.getFormId = function () { ...``` [autoform-api.js:1044](autoform-api.js#L1044)
823+
> ```AutoForm.getFormId = function () { ...``` [autoform-api.js:1068](autoform-api.js#L1068)
799824
800825

801826
-
@@ -820,7 +845,7 @@ __Returns__ *{undefined}*
820845

821846
Selects the focus the first field (in DOM order) with an error.
822847

823-
> ```AutoForm.selectFirstInvalidField = function selectFirstInvalidField(formId, ss) { ...``` [autoform-api.js:1057](autoform-api.js#L1057)
848+
> ```AutoForm.selectFirstInvalidField = function selectFirstInvalidField(formId, ss) { ...``` [autoform-api.js:1081](autoform-api.js#L1081)
824849
825850

826851
-
@@ -863,6 +888,6 @@ Is the form valid?
863888
If creating a form type, you will often want to call this from the `validateForm` function. It provides the generic form validation logic that does not typically change between form types.
864889

865890

866-
> ```AutoForm._validateFormDoc = function validateFormDoc(doc, isModifier, formId, ss, form, key) { ...``` [autoform-api.js:1086](autoform-api.js#L1086)
891+
> ```AutoForm._validateFormDoc = function validateFormDoc(doc, isModifier, formId, ss, form, key) { ...``` [autoform-api.js:1110](autoform-api.js#L1110)
867892
868893

autoform-api.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* global AutoForm:true, SimpleSchema, Utility, Hooks, deps, globalDefaultTemplate:true, defaultTypeTemplates:true, _validateForm, validateField, arrayTracker, ReactiveVar, getAllFieldsInForm, setDefaults:true, getFlatDocOfFieldValues, MongoObject */
1+
/* global AutoForm:true, SimpleSchema, Utility, Hooks, deps, globalDefaultTemplate:true, defaultTypeTemplates:true, validateField, arrayTracker, ReactiveVar, getAllFieldsInForm, setDefaults:true, getFlatDocOfFieldValues, MongoObject */
22

33
// This file defines the public, exported API
44

@@ -609,8 +609,28 @@ AutoForm.validateField = function autoFormValidateField(formId, fieldName, skipE
609609
* this method causes the reactive validation messages to appear.
610610
*/
611611
AutoForm.validateForm = function autoFormValidateForm(formId) {
612+
var form = AutoForm.getCurrentDataForForm(formId);
613+
var formDoc, formType = form.type;
614+
615+
var ftd = AutoForm._formTypeDefinitions[formType];
616+
if (!ftd) {
617+
throw new Error('AutoForm: Form type "' + formType + '" has not been defined');
618+
}
619+
612620
// Gather all form values
613-
return _validateForm(formId, AutoForm.getFormValues(formId, null, null, false));
621+
if (ftd.needsModifierAndDoc) {
622+
formDoc = AutoForm.getFormValues(formId, null, null);
623+
} else if (ftd.usesModifier) {
624+
formDoc = AutoForm.getFormValues(formId, null, null, true);
625+
} else {
626+
formDoc = AutoForm.getFormValues(formId, null, null, false);
627+
}
628+
629+
return (form.validation === 'none') || ftd.validateForm.call({
630+
form: form,
631+
formDoc: formDoc,
632+
useCollectionSchema: false
633+
});
614634
};
615635

616636
/**

autoform-events.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* global AutoForm, Hooks, _validateForm, validateField, updateTrackedFieldValue, arrayTracker, updateAllTrackedFieldValues, SimpleSchema */
1+
/* global AutoForm, Hooks, validateField, updateTrackedFieldValue, arrayTracker, updateAllTrackedFieldValues, SimpleSchema */
22

33
// all form events handled here
44
var lastAutoSaveElement = null;
@@ -135,6 +135,7 @@ Template.autoForm.events({
135135
event: event,
136136
formAttributes: form,
137137
formId: formId,
138+
formTypeDefinition: ftd,
138139
removeStickyValidationError: function (key) {
139140
delete AutoForm.templateInstanceForForm(formId)._stickyErrors[key];
140141
// revalidate that field
@@ -291,7 +292,12 @@ Template.autoForm.events({
291292
// validate against the form schema. Then before hooks can add any missing
292293
// properties before we validate against the full collection schema.
293294
try {
294-
isValid = _validateForm(formId, formDoc);
295+
isValid = (form.validation === 'none') ||
296+
ftd.validateForm.call({
297+
form: form,
298+
formDoc: formDoc,
299+
useCollectionSchema: false
300+
});
295301
} catch (e) {
296302
// Catch exceptions in validation functions which will bubble up here, cause a form with
297303
// onSubmit() to submit prematurely and prevent the error from being reported

autoform-validation.js

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,4 @@
1-
/* global _validateForm:true, AutoForm, validateField:true */
2-
3-
/*
4-
* all form validation logic is here
5-
*/
6-
7-
_validateForm = function _validateForm(formId, formDoc, useCollectionSchema) {
8-
var form = AutoForm.getCurrentDataForForm(formId);
9-
var formType = form.type;
10-
11-
if (form.validation === 'none') {
12-
return true;
13-
}
14-
15-
// Call validateForm from the requested form type definition
16-
var ftd = AutoForm._formTypeDefinitions[formType];
17-
if (!ftd) {
18-
throw new Error('AutoForm: Form type "' + formType + '" has not been defined');
19-
}
20-
21-
return ftd.validateForm.call({
22-
form: form,
23-
formDoc: formDoc,
24-
useCollectionSchema: useCollectionSchema
25-
});
26-
};
1+
/* global AutoForm, validateField:true */
272

283
function _validateField(key, formId, skipEmpty, onlyIfAlreadyInvalid) {
294
var docToValidate;

formTypes/method-update.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* global AutoForm, _validateForm */
1+
/* global AutoForm */
22

33
AutoForm.addFormType('method-update', {
44
onSubmit: function () {
@@ -16,9 +16,14 @@ AutoForm.addFormType('method-update', {
1616
// Validate. If both schema and collection were provided, then we validate
1717
// against the collection schema here. Otherwise we validate against whichever
1818
// one was passed.
19-
if (_validateForm(c.formId,
20-
updateDoc,
21-
c.ssIsOverride) === false) {
19+
var valid = (c.formAttributes.validation === 'none') ||
20+
c.formTypeDefinition.validateForm.call({
21+
form: c.formAttributes,
22+
formDoc: updateDoc,
23+
useCollectionSchema: c.ssIsOverride
24+
});
25+
26+
if (valid === false) {
2227
c.failedValidation();
2328
} else {
2429
// Call the method

0 commit comments

Comments
 (0)