javascript - How to use the same form for updating/inserting records using quickForm? -


i have meteor template:

<template name="personaldetailsform">   {{> quickform collection="personaldetails" id="personaldetailsform" type="insert"}}  {{> quickform collection="personaldetails" doc=editingdoc id="personaldetailsform" type="update"}}  </template> 

the forms displayed expect, want 1 form. blank form when there no data insert when form submitted. when form reloaded data submitted shown on form. if form submitted again data has changed updated.

currently insert form displaying , underneath update form displaying, data has been inserted. trying update data on second form doesn't work, instead inserts new record. imagine because form ids same.

ideally this:

<body> {{#if personaldetails}}     {{> personaldetailsformupdate}} {{ else }}     {{> personaldetailsforminsert}} {{/if}} </body>  <template name="personaldetailsforminsert">  {{> quickform collection="personaldetails" id="personaldetailsforminsert" type="insert"}} </template>   <template name="personaldetailsformupdate">  {{> quickform collection="personaldetails" doc=editingdoc id="personaldetailsformupdate" type="update"}} </template> 

i think part of documentation i'm looking for:

can reuse same quickform or autoform both inserts , updates?

yes. code flips between states should following in order:

change type attribute's value "insert" or "update" appropriate, updating reactive variable. change doc attribute's value correct document update or null (or document containing default values) insert, updating reactive variable. call autoform.resetform(formid). clear existing validation errors form. 

can provide example of this?

the question asked time ago, had same issue , solved it.

it quite easy:

get context data usual. e.g. iron-router, build route like:

router.route('options', {   path: 'options',   data: function() {     var options = options.findone({owner: meteor.userid()});     return options;   } }); 

build new helper, checks if context data (this in helper) empty or not (example global helper works in every template):

ui.registerhelper("formtype", function(){    if(_.isempty(this)) {     return 'insert'   } else {     return 'update';   }  }); 

setup template new helper:

<template name="options"> <h1>options</h1> {{> quickform collection="options" doc=this id="optionsform" type=formtype omitfields="owner"}} </template> 

everything should work now. if database doesn't give value back, form automatically switch insert. in example, if open form first time, use insert. second time, use update.


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -