选择字段的粘性形式

时间:2011-03-20 15:09:58

标签: xslt symphony-cms

我想让我的表单在出现错误时保持提交的值。你可以看到我为选项“3”创建的示例。我不想为每个选择字段的每个选项编写相同的代码块。肯定有更好的办法。思考?谢谢你们!

<select id="card-expires-month" name="fields[card-expires-month]">
    <option value="1" selected="selected">1 - January</option>
    <option value="2">2 - February</option>

    <xsl:element name="option">

        <xsl:attribute name="value">3</xsl:attribute>

        <xsl:if test="//data/events/deposit/post-values/card-expires-month = 3">
            <xsl:attribute name="selected">selected</xsl:attribute>
        </xsl:if>       

        <xsl:text>3 - March</xsl:text>

    </xsl:element>

    <option value="4">4 - April</option>
    <option value="5">5 - May</option>
    <option value="6">6 - June</option>
    <option value="7">7 - July</option>
    <option value="8">8 - August</option>
    <option value="9">9 - September</option>
    <option value="10">10 - October</option>
    <option value="11">11 - November</option>
    <option value="12">12 - December</option>
</select>

1 个答案:

答案 0 :(得分:3)

用Wendell Piez方法:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html"/>
    <xsl:template match="/">
        <xsl:variable name="vMonths">
            <xsl:text>1 - January|2 - February|3 - March|</xsl:text>
            <xsl:text>4 - April|5 - May|6 - June|</xsl:text>
            <xsl:text>7 - July|8 - August|9 - September|</xsl:text>
            <xsl:text>10 - October|11 - November|12 - December|</xsl:text>
        </xsl:variable>
        <select id="card-expires-month" name="fields[card-expires-month]">
            <xsl:for-each select="(//node())[12 >= position()]">
                <option value="{position()}">
                    <xsl:if test="//data
                                    /events
                                      /deposit
                                        /post-values
                                          /card-expires-month
                                  = position()">
                        <xsl:attribute name="selected">selected</xsl:attribute>
                    </xsl:if>
                    <xsl:value-of select="concat(
                                         position(),
                                         substring-before(
                                            substring-after(
                                               $vMonths,
                                               position()
                                            ),
                                            '|'
                                         )
                                      )"/>
                </option>
            </xsl:for-each>
        </select>
    </xsl:template>
</xsl:stylesheet>

注意$vMonths声明只是考虑可读性

使用普通递归:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html"/>
    <xsl:template match="/">
        <select id="card-expires-month" name="fields[card-expires-month]">
            <xsl:call-template name="tokenize"/>
        </select>
    </xsl:template>
    <xsl:template name="tokenize">
        <xsl:param name="pString">
            <xsl:text>January|February|March|April|</xsl:text>
            <xsl:text>May|June|July|August|</xsl:text>
            <xsl:text>September|October|November|December</xsl:text>
        </xsl:param>
        <xsl:param name="pPosition" select="1"/>
        <xsl:choose>
            <xsl:when test="$pString=''"/>
            <xsl:when test="contains($pString,'|')">
                <xsl:call-template name="tokenize">
                    <xsl:with-param name="pString"
                                    select="substring-before($pString,'|')"/>
                    <xsl:with-param name="pPosition" select="$pPosition"/>
                </xsl:call-template>
                <xsl:call-template name="tokenize">
                    <xsl:with-param name="pString"
                                    select="substring-after($pString,'|')"/>
                    <xsl:with-param name="pPosition" select="$pPosition + 1"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <option value="{$pPosition}">
                    <xsl:if test="//data
                                    /events
                                      /deposit
                                        /post-values
                                          /card-expires-month
                                  = $pPosition">
                        <xsl:attribute name="selected">selected</xsl:attribute>
                    </xsl:if>
                    <xsl:value-of select="concat($pPosition,' - ',$pString)"/>
                </option>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

两个输出:

<select id="card-expires-month" name="fields[card-expires-month]">
    <option value="1">1 - January</option>
    <option value="2">2 - February</option>
    <option value="3">3 - March</option>
    <option value="4">4 - April</option>
    <option value="5">5 - May</option>
    <option value="6">6 - June</option>
    <option value="7">7 - July</option>
    <option value="8">8 - August</option>
    <option value="9">9 - September</option>
    <option value="10">10 - October</option>
    <option value="11">11 - November</option>
    <option value="12">12 - December</option>
</select>

编辑:关于@ Flack的评论,内联或外部来源的回答:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:m="month"
 exclude-result-prefixes="m">
    <xsl:output method="html"/>
    <m:m>January</m:m>
    <m:m>February</m:m>
    <m:m>March</m:m>
    <m:m>April</m:m>
    <m:m>May</m:m>
    <m:m>June</m:m>
    <m:m>July</m:m>
    <m:m>August</m:m>
    <m:m>September</m:m>
    <m:m>October</m:m>
    <m:m>November</m:m>
    <m:m>December</m:m>
    <xsl:template match="/">
        <xsl:variable name="vSelected"
         select="//data/events/deposit/post-values/card-expires-month"/>
        <select id="card-expires-month" name="fields[card-expires-month]">
            <xsl:for-each select="document('')/*/m:m">
                <option value="{position()}">
                    <xsl:if test="$vSelected = position()">
                        <xsl:attribute name="selected">selected</xsl:attribute>
                    </xsl:if>
                    <xsl:value-of select="concat(position(),' - ',.)"/>
                </option>
            </xsl:for-each>
        </select>
    </xsl:template>
</xsl:stylesheet>