PHP Zend_Form的示例XML表示

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP Zend_Form的示例XML表示相关的知识,希望对你有一定的参考价值。

Zend_Form PHP:

<?php
class Form_AddNavigation extends Zend_Form {
	public function init() {
		$this->setName('addnavigation');
		$this->setMethod('post');
		$this->setAttrib('id', 'addnavigation');

		$this->addElement('text', 'name', array(
			'required'		=> true,
			'description'	=> "Descriptive name for the new navigation.",
			'filters'    => array('StringTrim'),
			'validators' => array(
				array(
					'StringLength',
					false,
					array(
						2,
						300,
						'messages' => array(
							Zend_Validate_StringLength::TOO_SHORT => 'The name is too short.',
							Zend_Validate_StringLength::TOO_LONG => 'The name is too long.'
						)
					)
				)
			),
			'size'			=>'60',
			'label'			=> 'Name',
			'id'			=>'name',
			'maxlength'		=>'300'
		));
		
		$this->addElement('select', 'lang', array(
			'required'		=> true,
			'description'	=> "Language of the navigation",
			'filters'    => array('StringTrim', 'StringToLower'),
			'validators' => array(
				array(
					'StringLength',
					false,
					array(
						2,
						2
					)
				)
			),
			'label'			=> 'Language',
			'id'			=> 'language',
			'multiOptions' => Uni_Main::getInstance()->languages
		));
		
		$this->addElement('checkbox', 'active', array(
			'required'		=> false,
			'label'			=> 'Activate',
			'id'			=> 'active',
		));
		
		$this->addElement('submit', 'submit',  array(
			'ignore'		=> true,
			'label'			=> 'Add new',
		));
		
		$this->populate($_POST);
	}
}

/**
 * Zend_Form XML:
*/

<?xml version="1.0" encoding="UTF-8"?>
<item type="form" id="addnavigation" state="fail">
    <fragment id="name">addnavigation</fragment>
    <fragment id="method">post</fragment>
    <fragment id="id">addnavigation</fragment>
    <fragment id="enctype">application/x-www-form-urlencoded</fragment>
    <group id="fields">
        <item id="name" type="text">
            <fragment id="name">name</fragment>
            <fragment id="label">Name</fragment>
            <fragment id="value"/>
            <fragment id="required">1</fragment>
            <fragment id="maxlength">300</fragment>
            <fragment id="size">60</fragment>
            <fragment id="readonly"/>
            <fragment id="description">Descriptive name for the new navigation.</fragment>
            <fragment id="valid">false</fragment>
        </item>
        <item id="lang" type="select">
            <fragment id="name">lang</fragment>
            <fragment id="label">Language</fragment>
            <fragment id="value"/>
            <fragment id="required">1</fragment>
            <fragment id="maxlength"/>
            <fragment id="size"/>
            <fragment id="readonly"/>
            <fragment id="description">Language of the navigation</fragment>
            <fragment id="valid">false</fragment>
            <group id="options">
                <item id="option">
                    <fragment id="text">English</fragment>
                    <fragment id="value">en</fragment>
                </item>
                <item id="option">
                    <fragment id="text">Spanish</fragment>
                    <fragment id="value">es</fragment>
                </item>
                <item id="option">
                    <fragment id="text">Portugese</fragment>
                    <fragment id="value">pt</fragment>
                </item>
            </group>
        </item>
        <item id="active" type="checkbox">
            <fragment id="name">active</fragment>
            <fragment id="label">Activate</fragment>
            <fragment id="value">0</fragment>
            <fragment id="required"/>
            <fragment id="maxlength"/>
            <fragment id="size"/>
            <fragment id="readonly"/>
            <fragment id="description"/>
            <fragment id="valid">true</fragment>
        </item>
        <item id="submit" type="submit">
            <fragment id="name">submit</fragment>
            <fragment id="label">Add new</fragment>
            <fragment id="value"/>
            <fragment id="required"/>
            <fragment id="maxlength"/>
            <fragment id="size"/>
            <fragment id="readonly"/>
            <fragment id="description"/>
            <fragment id="valid">true</fragment>
        </item>
    </group>
</item>

/**
 * Zend_Form XSL:
*/


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns="http://www.w3.org/1999/xhtml"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:html="http://www.w3.org/1999/xhtml">
	<xd:doc xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl" scope="stylesheet">
		<xd:desc>
			<xd:p>
				<xd:b>Created on:</xd:b>
				Apr 27, 2011
			</xd:p>
			<xd:p>
				<xd:b>Author:</xd:b>
				Erik Poehler
			</xd:p>
		</xd:desc>
	</xd:doc>
	<xsl:output method="xml" indent="yes" encoding="UTF-8" omit-xml-declaration="yes" />
	<xsl:strip-space elements="*" />

	<xsl:template match="item[@type='form']">
		<form id="{fragment[@id='id']}" class="{fragment[@id='class']} {fragment[@id='state']}" method="{fragment[@id='method']}" action="{fragment[@id='action']}" enctype="{fragment[@id='enctype']}">
			<ul>
				<xsl:apply-templates select="group[@id='fields']" />
				<li><input type="hidden" name="form" value="{fragment[@id='id']}" /></li>
			</ul>
		</form>
	</xsl:template>

	<xsl:template match="group[@id='fields']">
		<xsl:for-each select="item">
			<xsl:variable name="type" select="@type" />
			<xsl:call-template name="field">
				<xsl:with-param name="type" select="$type" />
			</xsl:call-template>
		</xsl:for-each>
	</xsl:template>

	<xsl:template name="field">
		<xsl:param name="type" />
		<li class="{@id} {@valid}">
			<xsl:choose>
				<xsl:when test="$type='text'">
					<xsl:if test="fragment[@id='label']!=''">
						<xsl:call-template name="getLabel"><xsl:with-param name="for" select="fragment[@id='id']" /></xsl:call-template>
					</xsl:if>
					<input name="{@id}" id="{@id}" class="text {fragment[@id='class']}"
						type="text" value="{fragment[@id='value']}">
						<xsl:if test="fragment[@id='readonly']='readonly'">
							<xsl:attribute name="readonly">readonly</xsl:attribute>
						</xsl:if>
						<xsl:if test="fragment[@id='size']!=''">
							<xsl:attribute name="size">
								<xsl:value-of select="fragment[@id='size']" />
							</xsl:attribute>
						</xsl:if>
						<xsl:if test="fragment[@id='maxlength']!=''">
							<xsl:attribute name="maxlength">
								<xsl:value-of select="fragment[@id='maxlength']" />
							</xsl:attribute>
						</xsl:if>
					</input>
					<xsl:if test="fragment[@id='valid']='false'">
						<span class="note error">
							<xsl:for-each select="fragment[@id='error']">
								<xsl:call-template name="getErrorMessage">
									<xsl:with-param name="error" select="." />
									<xsl:with-param name="field" select="../fragment[@id='label']" />
								</xsl:call-template>
							</xsl:for-each>
						</span>
					</xsl:if>
				</xsl:when>
				<xsl:when test="$type='submit'">
					<input class="submit {fragment[@id='class']}" id="{@id}" type="submit">
						<xsl:attribute name="value">
							<xsl:value-of select="fragment[@id='label']" />
						</xsl:attribute>
					</input>
				</xsl:when>
				<xsl:when test="$type='reset'">
					<input class="reset {fragment[@id='class']}" id="{@id}" type="reset"
						value="{fragment[@id='label']}" />
				</xsl:when>
				<xsl:when test="$type='select'">
					<xsl:if test="fragment[@id='label']!=''">
						<xsl:call-template name="getLabel" />
					</xsl:if>
					<select name="{@id}" id="{@id}" class="select styled">
						<xsl:for-each select="group[@id='options']/item">
							<option value="{fragment[@id='value']}">
								<xsl:if
									test="contains(../fragment[@id='value'],fragment[@id='value'])">
									<xsl:attribute name="selected">selected</xsl:attribute>
								</xsl:if>
								
								<!-- <xsl:value-of select="fragment[@id='text']" /> -->
								<xsl:variable name="var-value">
									<xsl:value-of select="fragment[@id='text']" />
								</xsl:variable>
								<xsl:choose>
								<xsl:when test="$var-value !=''">
									<xsl:value-of select="$var-value" />
								</xsl:when>
								<xsl:otherwise>
									<xsl:value-of select="fragment[@id='text']" />
								</xsl:otherwise>
								</xsl:choose>
									
							</option>
						</xsl:for-each>
					</select>
				</xsl:when>
				<xsl:when test="$type='radio'">
					<xsl:for-each select="item">
						<!-- do something -->
					</xsl:for-each>
				</xsl:when>
				<xsl:when test="$type='password'">
					<xsl:if test="fragment[@id='label']!=''">
						<xsl:call-template name="getLabel" />
					</xsl:if>
					<input class="password {fragment[@id='class']}" id="{@id}" type="password" value="" name="{fragment[@id='name']}" />
				</xsl:when>
				<xsl:when test="$type='hidden'">
					<input class="hidden" type="hidden" name="{fragment[@id='name']}" id="{fragment[@id='name']}" value="{fragment[@id='value']}" />
				</xsl:when>
				<xsl:when test="$type='textarea'">
					<textarea class="textarea" name="{fragment[@id='name']}" id="{fragment[@id='name']}">
						<xsl:choose>
							<xsl:when test="fragment[@id='value']=''">
								<xsl:text> </xsl:text>
							</xsl:when>
							<xsl:otherwise>
								<xsl:value-of select="fragment[@id='value']" />
							</xsl:otherwise>
						</xsl:choose>
					</textarea>
				</xsl:when>
				<xsl:when test="$type='checkbox'">
					<input type="checkbox" name="{fragment[@id='name']}" id="{fragment[@id='name']}" class="checkbox {fragment[@id='class']}" /> <xsl:if test="fragment[@id='label']!=''">
						<xsl:call-template name="getLabel" />
					</xsl:if>
				</xsl:when>
				<xsl:otherwise>
					This mode is not implemented yet.
				</xsl:otherwise>
			</xsl:choose>
		</li>
	</xsl:template>

	<xsl:template name="getErrorMessage">
		<xsl:param name="error" select="$error"/>
		<xsl:param name="field" select="$field"/>
		<xsl:choose>
			<xsl:when test="$error='isEmpty'">
				<span class="error-x">X</span> Field is required.
			</xsl:when>
			<xsl:when test="$error='stringLengthTooShort'">
				<span class="error-x">X</span> This is too short.
			</xsl:when>
			<xsl:when test="$error='stringLengthTooLong'">
				<span class="error-x">X</span> This is too long.
			</xsl:when>
			<xsl:otherwise>
				<span class="error-x">X</span> An unknown error occured. (<samp><xsl:value-of select="$error" /></samp>)
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template name="getLabel">
		<label for="{@id}" title="required" class="required">
			<xsl:value-of select="fragment[@id='label']" />
			<xsl:if test="fragment[@id='required']='1'">
				<xsl:text> </xsl:text>
				<span class="required">
					<sup>*</sup>required
				</span>
			</xsl:if>
			<xsl:text> </xsl:text>
		</label>
		<br />
	</xsl:template>
</xsl:stylesheet>

以上是关于PHP Zend_Form的示例XML表示的主要内容,如果未能解决你的问题,请参考以下文章

Zendèu表单的XML表示示例

PHP 在Zend_Form中显示错误

PHP 自定义Zend_Form:更改了装饰器和添加的方法

Zend_Form 创建校验和解析表单的基础--(手冊)

对 Zend_Form 中的特定子表单使用自定义 isValid() 函数

Zend_Form 占位符翻译