danielo515 / obsidian-modal-form

Define forms for filling data that you will be able to open from anywhere you can run JS
https://danielorodriguez.com/obsidian-modal-form/
MIT License
199 stars 17 forks source link

[QUESTION-HELP] Templater,Quickadd Problem - name of the note should come from the form? #176

Closed xverbatim1971 closed 8 months ago

xverbatim1971 commented 8 months ago

great plugin, many thanks to the developer. i have a problem with modal form and templater, quickadd.

how can i manage to pass a field from modal form to templater as the title of the note?

unfortunately i can't create the note via a form ;(

is there any way to do that?

I can use suggester from the templater plugin, but I want to make the input uniform and only via a form. I have a form and the first field is a name.

The note should then be called exactly the same as the name. Unfortunately I can't find a solution! Does anyone know what to do?


sorry if this post is not allowed here... i didn't know where i could get help ...

the1gofer commented 8 months ago

there are a few ways to do it. start with https://github.com/danielo515/obsidian-modal-form?tab=readme-ov-file#formresult-methods.

there is also a result.get("somthing") name that isn't documented that you can use.

xverbatim1971 commented 8 months ago

Hi, thank you for your answer. Unfortunately I can't get it to work. I have a template with the code: i start the plugin via the plugin templater and call up the form, before that a note "untitled.md" is created. if i have a field in the form with the name "customer name", i would like the file "untitled.md" to be renamed to the content of the field Kundenname after the form has been submitted. unfortunately i always get a parsing error. unfortunately i can't program that well and don't know where to put what and the exact syntax ;(


---

<%*
const modalForm = app.plugins.plugins.modalforms.api;
const result = await modalForm.openForm('form');
tR += result.asFrontmatterString();
-%>
---

<%*
let filename = tp.file.title
if ( filename.startsWith("Untitled") ) {
   await tp.file.rename(tR += result.get("Kundenname");)
} 
%>
the1gofer commented 8 months ago

im not an expert .. so atb a glance

I don't think you have that first block correct. I'd try something like

<%*
const modalForm = app.plugins.plugins.modalforms.api;
const result = await modalForm.openForm('form');
let frontmatter = result.asFrontmatterString();
-%>
---
<% frontmatter %>
---

also

await tp.file.rename(tR += result.get("Kundenname");)

looks wrong

alo check to make sure all the names match exactly between your template, and form.

I also don't think your tR is what you think it is. have a long string with the YAML in it, then you append the file name to the nend of it with the +=. then you rename the file to the that long string.

when in doubt use <% variableName %> in the template by itself to see what's in it.

xverbatim1971 commented 8 months ago

thanks again for the quick reply. i have found a solution :-) the file has the name of the field and is moved to a specific folder :-) perfect. thank you very much and all the best

---
<%*
const modalForm = app.plugins.plugins.modalforms.api;
const result = await modalForm.openForm('form');
tR += result.asFrontmatterString();

let filename = tp.file.title

if ( filename.startsWith("Untitled") ) {

  await tp.file.rename(result.get("Kundenname"));
  await tp.file.move("Kunden/" + result.get("Kundenname"));
} 

-%>
---
danielo515 commented 8 months ago

I'm super happy to see people from what looks like a little community helping each other. All the comments of @the1gofer are correct. tR is a special templater variable that just contains the text body of the note you are creating. The right way of doing this is, if you don't have the title from the beginning, ask for the right values in the form,then move and rename from templater just like what you did .

@the1gofer let me know how we can improve the discoverability of the documentation, because the get method is documented. Check the "managing results" section (sorry for the lack of detail, on my phone now)