Search

Hello,

I am creating a blog that displays 10 entries per page. Each entry has a comment form at its bottom. When an error occurs (e.g. the user forgets to fill in one field), the error message appears at all entries. How can I make the forms unique so that errors are only shown in the form submitted? By the way, I am using the utility form controls.

Thanks for your help!

:D mwhahahha !

The perfect job for Sections Event. Go check it out.

By the way, I am using the utility form controls

You'll be using the improved SForm Controls. It's Form Controls on steroids.

Hello vladG,

thanks for your quick post and recommendation. I have switched my form to your Sections Event. It works well but with the same restriction as before: an error is shown in all forms, I have not yet managed to make the forms unique.

Can you please give me a hint? Thanks – Moritz

Do you have a structure like this:

<form>
    entry #1...
    entry #2...
    entry #3...
    submit
</form>

or a structure like this:

<form>
    entry #1...
    submit
</form>

<form>
    entry #2...
    submit
</form>

<form>
    entry #3...
    submit
</form>

If you have structure #1 it is normal to get the error reporting for all entries because you're sending all of them in the same form.

If you have structure #2, then only one entry will be submitted with that form thus obtaining the validation for that entry.

Thank you for your structural sketch – I checked my code and it has the correct second structure.

I believe the problem arises from the way the forms are generated: A template writes for each entry a form. Therefore I end up with forms that have inputs with the same ids – could this be the problem?

<form>
    <input type=text id=name>
    <input type=text id=comment>
</form>

<form>
    <input type=text id=name>
    <input type=text id=comment>
</form>

Would it help to make the input ids unique? If yes, may I overwrite the ids and how? I already gave them a custom name but I did not succeed in adding a variable to the name.

Thanks a lot again!

How are you calling a template for an input? Paste here the XSLT.

This is my code, simplified:

<xsl:template match="data">
    <xsl:apply-templates select="nachrichten/entry"/>
</xsl:template>

<xsl:template match="nachrichten/entry">
    <xsl:value-of select="ueberschrift"/>
    <xsl:value-of select="nachricht"/>

    <form method="post" action="" name="{current()/@id}">
        <xsl:call-template name="sform:input">
            <xsl:call-template name="sform:input">
                <xsl:with-param name="section" select="'kommentare'"/>
                <xsl:with-param name="handle" select="'name'"/>
                <xsl:with-param name="attributes">
                    <placeholder>Dein Name</placeholder>
            </xsl:with-param>
        </xsl:call-template>

        <xsl:call-template name="sform:input">
            <xsl:with-param name="section" select="'kommentare'"/>
            <xsl:with-param name="handle" select="'email'"/>
            <xsl:with-param name="attributes">
                <placeholder>Deine E-Mailadresse</placeholder>
            </xsl:with-param>
        </xsl:call-template>

        <button type="submit" name="action[sections]">Send</button>
    </form>

</xsl:template>

Therefore I end up with forms that have inputs with the same ids – could this be the problem?

Yeah, that's the problem.

I recommend adjusting your form controls templates to include the $position parameter as well. This way you will obtain unique ID's automatically.

<xsl:template match="nachrichten/entry">
    <xsl:value-of select="ueberschrift"/>
    <xsl:value-of select="nachricht"/>

    <form method="post" action="" name="{current()/@id}">
        <xsl:call-template name="sform:input">
            <xsl:call-template name="sform:input">
                <xsl:with-param name="section" select="'kommentare'"/>
                <xsl:with-param name="position" select="position()"/>
                <xsl:with-param name="handle" select="'name'"/>
                <xsl:with-param name="attributes">
                    <placeholder>Dein Name</placeholder>
            </xsl:with-param>
        </xsl:call-template>

        <xsl:call-template name="sform:input">
            <xsl:with-param name="section" select="'kommentare'"/>
            <xsl:with-param name="position" select="position()"/>
            <xsl:with-param name="handle" select="'email'"/>
            <xsl:with-param name="attributes">
                <placeholder>Deine E-Mailadresse</placeholder>
            </xsl:with-param>
        </xsl:call-template>

        <button type="submit" name="action[sections]">Send</button>
    </form>

</xsl:template>

Btw, you don't need current() on this line. You already are in nachrichten/entry context:

<form method="post" action="" name="{current()/@id}">

This will suffice:

<form method="post" action="" name="{@id}">

current() is mainly used in an xPath cross match:

<xsl:for-each select="/data/categories/entry">
    <xsl:value-of select="category-title"/>

    <xsl:for-each select="/data/articles-by-category/entry[ category/item/@id = current()/@id ]">
        <xsl:value-of select="article-title"/>
    </xsl:for-each>
</xsl:for-each>

In this context with current() you can access the category context even if you are in an article context.

Wonderful, this now works like I intended. Thank you! Also thank you for the explanation of current().

May I ask one more question? How can I jump to an anchor when the form is sent? The page reloads, either the data is sent or errors are displayed – and I want to make the page jump to the beginning of the form in case of errors and to the added entry in case of success.

Put an ID on an element (not necessarily the form itself) and the hash tag in action attribute of the form:

<form id="entry-{@id}" method="post" action="#entry-{@id}" name="{@id}">

Thanks for your idea. It does work but lacks one functionality: I'd like to jump to a different anchor depending on success or failure. Could I insert a variable into the action? How can I check, with a simple if statement for example, whether errors occurred in the form or not?

Additionally I am looking for a method of implementing a honeypot.

Thank you very much for all your help!

How can I check, with a simple if statement for example, whether errors occurred in the form or not?

Since you're using SForm Controls, you should have an $interpretation variable (see example #6). More details about it in the validation section of the Readme. Based on the information it contains you can achieve your goals.

Create an account or sign in to comment.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details