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

[BUG] File renaming with templater doesn't handle getValue results well #187

Closed andrew-m-j closed 8 months ago

andrew-m-j commented 8 months ago

Describe the bug I'm trying to rename a new note based on modal content but the script throws an error and doesn't insert the template. The console reports a "Template parsing error"—it is a templater error, but based on wording in the documentation for getValue, I would have expected it to work. The documentation on the ResultValue page says about getValue that "By default, the value is rendered as a string…. You don't need to call or do anything special to render the value as a string, just use it as if it was a string.." I'm not totally familiar with Obsidian scripting, but looking at the console, it looks like it is an object that is parsed or rendered as a string in most cases. But here, that isn't the case and the interaction causes problems.

To Reproduce Steps to reproduce the bug:

  1. Make a templater template that calls the modal form and includes the await tp.file.rename() function after creation. For example,
<%*
const modalForm = app.plugins.plugins.modalforms.api;
const result = await modalForm.openForm('example-form');
-%>
<% await tp.file.rename( result.getValue('name') ) %>
  1. Fill out the form, including the name item, and submit.

Expected behavior

Based on the documentation, I expected the getValue to pass a string to the tp.file.rename command. This is maybe where my understanding isn't correct for how Obsidian handles the getValue objects. If that's the case, it might be helpful to update the documentation for getValue somehow.

Additional context I found two workarounds to rename the files. First, using the asString function that is mentioned on the main page, but not the ResultsValues page as so: <% await tp.file.rename(result.asString('{{name}}') ) %> Second, to coerce the title to be a string: <% await tp.file.rename( "" + result.getValue('name') ) %>

danielo515 commented 8 months ago

Happy to see you figured out a workaround. getValue doesn't return a string, it returns an object that can be used in most places a string can, but not all. This is a little javascript trick for convenience, but it can be confusing if you are not aware of that. The get method will return just the value. As a side note, thanks to some of the latest updates you can write your workaround shorter like "" + result.name

andrew-m-j commented 8 months ago

Thanks for the response. Totally understand that the object being returned is the expected behavior. There are a few different ways to get the string that aren't all that complicated, just have to be aware of it. The property-style access will be a nice update in the new version.

danielo515 commented 8 months ago

If you have any suggestions about how to make it clearer or easier I will be happy to discuss them.

El mar, 2 ene 2024, 19:41, Andrew Jackson @.***> escribió:

Thanks for the response. Totally understand that the object being returned is the expected behavior. There are a few different ways to get the string that aren't all that complicated, just have to be aware of it. The property-style access will be a nice update in the new version.

— Reply to this email directly, view it on GitHub https://github.com/danielo515/obsidian-modal-form/issues/187#issuecomment-1874403814, or unsubscribe https://github.com/notifications/unsubscribe-auth/AARKJWKGISEP5A5ZWXRLA4TYMRIDZAVCNFSM6AAAAABBG4JWRSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQNZUGQYDGOBRGQ . You are receiving this because you were assigned.Message ID: @.***>