This question about Topic Markup Language and applications: Asked

How can I tell a form to remove spaces when saving an entry?


I'm using the %FORMFIELD{"FieldNameBla"}% option to check for topics on other webs. If somebody accidentally entered a name with a trailing space, like "foo " instead of "foo", then the page will not be found, because A is not equal B.

Two questions for solutions:
- How can I make the data in the form be saved without the trailing space?
- How can I add a trim-function to the calling %FORMFIELD{"FieldNameBla"}% ?

-- LieVen - 27 Apr 2015


Just to make sure I understand your question, when you say "somebody accidentally entered a name with a trailing space", I assume you referring to using FORMFIELD macro (e.g. %FORMFIELD{"FieldNameBla "}% with space after "Bla"). I ask this because having a trailing space after field name in the DataForm definition would not present a problem.

Adding a space between the quotes in FORMFIELD macro will definitely present a problem and I don't have a quick fix for that within the scope of the FORMFIELD macro itself. That might be a legitimate "enhancement" request. Doing a little testing, I did discover that VarQUERY (an more powerful macro with over-lapping functionality to FORMFIELD) does appear to trim extra spaces, so
%QUERY{" Status  "}%

...renders as:

One other option in case your users need to use FORMFIELD a lot and won't convert to using QUERY, I can imagine a work-around where you define a custom macro like this:


...and then your FIELD macro would act just like FORMFIELDbut be more trailing (and leading) space-tolerant. So
%FIELD{" TopicTitle  "}%

...would render as:
"How can I tell a form to remove spaces when saving an entry?"

Hope this helps!

-- LynnwoodBrown - 27 Apr 2015


Thanks a lot for answering so quickly, but I_believe_ that we are talking about different things... What I meant is the data saved in each file after entered in the form. How is that treated?

I tried to recreate the problem in this page, using the field "Related Topics", entering a topic with a space, but the field automatically cut of the trailing space (!!) How did it do that? THIS is what I need to know. (just found out by accident.) It seems like it does so, because it automatically checks ("search") for the existing topics. So I cannot recreate the problem.

In order to create the problem, for instance you have the user-form, where people add their name, e.g. "John". If there is a topic named "John", I can make a link to it using the simple function [[%FIELD{"UserName"}%]]. If someone has entered "John " instead, there will no topic be found, because there is no topic "John ", only "John".

This could be solved in the first place by saving the data without spaces, or I need to tell the [[%FIELD{"UserName"}%]] function to remove spaces.

-- LieVen - 29 Apr 2015


Ok, sorry about that. Now I'm with you and in some ways this is an easier question to answer. You have several options which fall under two general approaches: 1) reducing user data-entry variability or 2) trying to accommodate that variability after the fact.

The "quick-and-dirty" solution for your example of link to user topic which falls under the second general approach would be to do something like [[%CALC{"$TRIM(%FORMFIELD{"UserName"}%)"}%]]. That would trim any blank space from the field value before linking. You could implement this as a variation on the custom FIELD macro like so:

...then if you would just enter %LINKFIELD{"UserName"}% to create the link.

The other approach would be to use a field type which limits user input to valid values. This has the virtue of also avoiding mis-spelling, typos, etc. In the case of the "Related Topics" field, if you check out the data form definition (in raw view), you'll see it uses the textboxlistfield type which provides a dynamic list of valid topic names. When you tested it, the field trimmed the spaces before finding the right topic. At the bottom of the QuestionForm topic, you'll find the search that provides the list of values. A variation on that search which would provide a list of users would look like this:
<literal>%SEARCH{"'*UserForm' AND lc(name)~lc('%URLPARAM{"q" default="somethingTAHTNevErExists"}%*')" type="query" topic="%URLPARAM{"q" default="somethingTaHTNeVERexists"}%*" nonoise="on" format="$web.$topic" web="%USERSWEB%" }%</literal>


(I didn't actually test this search but I'm pretty sure it's correct.) Setting up a data form using the textboxlist field type and the above search should allow you to use that field to link to user topics correctly every time in any web.

There are a number of other strategies to restrict (or validate) user input depending on the kind of data being entered, but this is probably the easiest where the desired data is wiki topics.

-- LynnwoodBrown - 29 Apr 2015


Thanks again, one version of those could work for me. I cannot use the version of checking existing topics, because non-existing topics are accepted in this case. Could you tell me how (or if?) I can properly include this into my IF-istopic-string? I'm sometimes struggling with these macros inside macros. Does this work?

IF "istopic 'En.%FORMFIELD{"EnglishTopicName"}%'"


IF "istopic 'En.%CALC{"$TRIM(%FORMFIELD{"EnglishTopicName"}%)"}%'"

(As a side note, for your interest: I have a wiki with different languages, like wikipedia, using different webs. When a new topic is written, one can add the names of the other languages into a form, for instance the article named "one" in english will have a formfield called "german" with the data "zwei". In the sidebar, a link to the german article will take this information. Unfortunately, it does not work when the entered link contains a space. That's why I want to either cut of the space when saving the form, or cut it when using the date in the sidebar.)

-- LieVen - 30 Apr 2015


This should work:
%IF{"istopic 'En.%CALC{"$TRIM(%FORMFIELD{"EnglishTopicName"}%)"}%'" then="Something" else="Something Else"}%

Btw, this discussion has strayed pretty far from the original question asked. If the original question has been adequately answered, I'd recommend changing status to answered and post new question for other aspects of your project.

-- LynnwoodBrown - 06 May 2015

You're right. Since it was my question which was misleading, I have now changed the question itself, so your answers are not so far from the question any more. Your proposed solution may be working, but only on the place where I "use" the data, not where it is stored. I would also need to remember to use a TRIM wherever I get data from the formfield, and new users may not know about that. What I really need is a code inside the form telling him to delete spaces. This also happens when you create a new topic, and the page says "Topic will be named: Blah "

-- LieVen - 23 May 2015


QuestionForm edit

Subject Topic Markup Language and applications
Version Foswiki 1.1.9
Status Asked
Related Topics
Topic revision: r7 - 23 May 2015, LieVen - This page was cached on 03 Jun 2020 - 20:06.

The copyright of the content on this website is held by the contributing authors, except where stated elsewhere. See Copyright Statement. Creative Commons License    Legal Imprint    Privacy Policy