Example XML representation of Zend_Form


/ Published in: PHP
Save to your folder(s)

the link explains how the XML is generated.


Copy this code and paste it in your HTML
  1. Zend_Form PHP:
  2.  
  3. <?php
  4. class Form_AddNavigation extends Zend_Form {
  5. public function init() {
  6. $this->setName('addnavigation');
  7. $this->setMethod('post');
  8. $this->setAttrib('id', 'addnavigation');
  9.  
  10. $this->addElement('text', 'name', array(
  11. 'required' => true,
  12. 'description' => "Descriptive name for the new navigation.",
  13. 'filters' => array('StringTrim'),
  14. 'validators' => array(
  15. 'StringLength',
  16. false,
  17. 2,
  18. 300,
  19. 'messages' => array(
  20. Zend_Validate_StringLength::TOO_SHORT => 'The name is too short.',
  21. Zend_Validate_StringLength::TOO_LONG => 'The name is too long.'
  22. )
  23. )
  24. )
  25. ),
  26. 'size' =>'60',
  27. 'label' => 'Name',
  28. 'id' =>'name',
  29. 'maxlength' =>'300'
  30. ));
  31.  
  32. $this->addElement('select', 'lang', array(
  33. 'required' => true,
  34. 'description' => "Language of the navigation",
  35. 'filters' => array('StringTrim', 'StringToLower'),
  36. 'validators' => array(
  37. 'StringLength',
  38. false,
  39. 2,
  40. 2
  41. )
  42. )
  43. ),
  44. 'label' => 'Language',
  45. 'id' => 'language',
  46. 'multiOptions' => Uni_Main::getInstance()->languages
  47. ));
  48.  
  49. $this->addElement('checkbox', 'active', array(
  50. 'required' => false,
  51. 'label' => 'Activate',
  52. 'id' => 'active',
  53. ));
  54.  
  55. $this->addElement('submit', 'submit', array(
  56. 'ignore' => true,
  57. 'label' => 'Add new',
  58. ));
  59.  
  60. $this->populate($_POST);
  61. }
  62. }
  63.  
  64. /**
  65.  * Zend_Form XML:
  66. */
  67.  
  68. <?xml version="1.0" encoding="UTF-8"?>
  69. <item type="form" id="addnavigation" state="fail">
  70. <fragment id="name">addnavigation</fragment>
  71. <fragment id="method">post</fragment>
  72. <fragment id="id">addnavigation</fragment>
  73. <fragment id="enctype">application/x-www-form-urlencoded</fragment>
  74. <group id="fields">
  75. <item id="name" type="text">
  76. <fragment id="name">name</fragment>
  77. <fragment id="label">Name</fragment>
  78. <fragment id="value"/>
  79. <fragment id="required">1</fragment>
  80. <fragment id="maxlength">300</fragment>
  81. <fragment id="size">60</fragment>
  82. <fragment id="readonly"/>
  83. <fragment id="description">Descriptive name for the new navigation.</fragment>
  84. <fragment id="valid">false</fragment>
  85. </item>
  86. <item id="lang" type="select">
  87. <fragment id="name">lang</fragment>
  88. <fragment id="label">Language</fragment>
  89. <fragment id="value"/>
  90. <fragment id="required">1</fragment>
  91. <fragment id="maxlength"/>
  92. <fragment id="size"/>
  93. <fragment id="readonly"/>
  94. <fragment id="description">Language of the navigation</fragment>
  95. <fragment id="valid">false</fragment>
  96. <group id="options">
  97. <item id="option">
  98. <fragment id="text">English</fragment>
  99. <fragment id="value">en</fragment>
  100. </item>
  101. <item id="option">
  102. <fragment id="text">Spanish</fragment>
  103. <fragment id="value">es</fragment>
  104. </item>
  105. <item id="option">
  106. <fragment id="text">Portugese</fragment>
  107. <fragment id="value">pt</fragment>
  108. </item>
  109. </group>
  110. </item>
  111. <item id="active" type="checkbox">
  112. <fragment id="name">active</fragment>
  113. <fragment id="label">Activate</fragment>
  114. <fragment id="value">0</fragment>
  115. <fragment id="required"/>
  116. <fragment id="maxlength"/>
  117. <fragment id="size"/>
  118. <fragment id="readonly"/>
  119. <fragment id="description"/>
  120. <fragment id="valid">true</fragment>
  121. </item>
  122. <item id="submit" type="submit">
  123. <fragment id="name">submit</fragment>
  124. <fragment id="label">Add new</fragment>
  125. <fragment id="value"/>
  126. <fragment id="required"/>
  127. <fragment id="maxlength"/>
  128. <fragment id="size"/>
  129. <fragment id="readonly"/>
  130. <fragment id="description"/>
  131. <fragment id="valid">true</fragment>
  132. </item>
  133. </group>
  134. </item>
  135.  
  136. /**
  137. * Zend_Form XSL:
  138. */
  139.  
  140.  
  141. <?xml version="1.0" encoding="UTF-8"?>
  142. <xsl:stylesheet version="1.0" xmlns="http://www.w3.org/1999/xhtml"
  143. xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:html="http://www.w3.org/1999/xhtml">
  144. <xd:doc xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl" scope="stylesheet">
  145. <xd:desc>
  146. <xd:p>
  147. <xd:b>Created on:</xd:b>
  148. Apr 27, 2011
  149. </xd:p>
  150. <xd:p>
  151. <xd:b>Author:</xd:b>
  152. Erik Poehler
  153. </xd:p>
  154. </xd:desc>
  155. </xd:doc>
  156. <xsl:output method="xml" indent="yes" encoding="UTF-8" omit-xml-declaration="yes" />
  157. <xsl:strip-space elements="*" />
  158.  
  159. <xsl:template match="item[@type='form']">
  160. <form id="{fragment[@id='id']}" class="{fragment[@id='class']} {fragment[@id='state']}" method="{fragment[@id='method']}" action="{fragment[@id='action']}" enctype="{fragment[@id='enctype']}">
  161. <ul>
  162. <xsl:apply-templates select="group[@id='fields']" />
  163. <li><input type="hidden" name="form" value="{fragment[@id='id']}" /></li>
  164. </ul>
  165. </form>
  166. </xsl:template>
  167.  
  168. <xsl:template match="group[@id='fields']">
  169. <xsl:for-each select="item">
  170. <xsl:variable name="type" select="@type" />
  171. <xsl:call-template name="field">
  172. <xsl:with-param name="type" select="$type" />
  173. </xsl:call-template>
  174. </xsl:for-each>
  175. </xsl:template>
  176.  
  177. <xsl:template name="field">
  178. <xsl:param name="type" />
  179. <li class="{@id} {@valid}">
  180. <xsl:choose>
  181. <xsl:when test="$type='text'">
  182. <xsl:if test="fragment[@id='label']!=''">
  183. <xsl:call-template name="getLabel"><xsl:with-param name="for" select="fragment[@id='id']" /></xsl:call-template>
  184. </xsl:if>
  185. <input name="{@id}" id="{@id}" class="text {fragment[@id='class']}"
  186. type="text" value="{fragment[@id='value']}">
  187. <xsl:if test="fragment[@id='readonly']='readonly'">
  188. <xsl:attribute name="readonly">readonly</xsl:attribute>
  189. </xsl:if>
  190. <xsl:if test="fragment[@id='size']!=''">
  191. <xsl:attribute name="size">
  192. <xsl:value-of select="fragment[@id='size']" />
  193. </xsl:attribute>
  194. </xsl:if>
  195. <xsl:if test="fragment[@id='maxlength']!=''">
  196. <xsl:attribute name="maxlength">
  197. <xsl:value-of select="fragment[@id='maxlength']" />
  198. </xsl:attribute>
  199. </xsl:if>
  200. </input>
  201. <xsl:if test="fragment[@id='valid']='false'">
  202. <span class="note error">
  203. <xsl:for-each select="fragment[@id='error']">
  204. <xsl:call-template name="getErrorMessage">
  205. <xsl:with-param name="error" select="." />
  206. <xsl:with-param name="field" select="../fragment[@id='label']" />
  207. </xsl:call-template>
  208. </xsl:for-each>
  209. </span>
  210. </xsl:if>
  211. </xsl:when>
  212. <xsl:when test="$type='submit'">
  213. <input class="submit {fragment[@id='class']}" id="{@id}" type="submit">
  214. <xsl:attribute name="value">
  215. <xsl:value-of select="fragment[@id='label']" />
  216. </xsl:attribute>
  217. </input>
  218. </xsl:when>
  219. <xsl:when test="$type='reset'">
  220. <input class="reset {fragment[@id='class']}" id="{@id}" type="reset"
  221. value="{fragment[@id='label']}" />
  222. </xsl:when>
  223. <xsl:when test="$type='select'">
  224. <xsl:if test="fragment[@id='label']!=''">
  225. <xsl:call-template name="getLabel" />
  226. </xsl:if>
  227. <select name="{@id}" id="{@id}" class="select styled">
  228. <xsl:for-each select="group[@id='options']/item">
  229. <option value="{fragment[@id='value']}">
  230. <xsl:if
  231. test="contains(../fragment[@id='value'],fragment[@id='value'])">
  232. <xsl:attribute name="selected">selected</xsl:attribute>
  233. </xsl:if>
  234.  
  235. <!-- <xsl:value-of select="fragment[@id='text']" /> -->
  236. <xsl:variable name="var-value">
  237. <xsl:value-of select="fragment[@id='text']" />
  238. </xsl:variable>
  239. <xsl:choose>
  240. <xsl:when test="$var-value !=''">
  241. <xsl:value-of select="$var-value" />
  242. </xsl:when>
  243. <xsl:otherwise>
  244. <xsl:value-of select="fragment[@id='text']" />
  245. </xsl:otherwise>
  246. </xsl:choose>
  247.  
  248. </option>
  249. </xsl:for-each>
  250. </select>
  251. </xsl:when>
  252. <xsl:when test="$type='radio'">
  253. <xsl:for-each select="item">
  254. <!-- do something -->
  255. </xsl:for-each>
  256. </xsl:when>
  257. <xsl:when test="$type='password'">
  258. <xsl:if test="fragment[@id='label']!=''">
  259. <xsl:call-template name="getLabel" />
  260. </xsl:if>
  261. <input class="password {fragment[@id='class']}" id="{@id}" type="password" value="" name="{fragment[@id='name']}" />
  262. </xsl:when>
  263. <xsl:when test="$type='hidden'">
  264. <input class="hidden" type="hidden" name="{fragment[@id='name']}" id="{fragment[@id='name']}" value="{fragment[@id='value']}" />
  265. </xsl:when>
  266. <xsl:when test="$type='textarea'">
  267. <textarea class="textarea" name="{fragment[@id='name']}" id="{fragment[@id='name']}">
  268. <xsl:choose>
  269. <xsl:when test="fragment[@id='value']=''">
  270. <xsl:text> </xsl:text>
  271. </xsl:when>
  272. <xsl:otherwise>
  273. <xsl:value-of select="fragment[@id='value']" />
  274. </xsl:otherwise>
  275. </xsl:choose>
  276. </textarea>
  277. </xsl:when>
  278. <xsl:when test="$type='checkbox'">
  279. <input type="checkbox" name="{fragment[@id='name']}" id="{fragment[@id='name']}" class="checkbox {fragment[@id='class']}" /> <xsl:if test="fragment[@id='label']!=''">
  280. <xsl:call-template name="getLabel" />
  281. </xsl:if>
  282. </xsl:when>
  283. <xsl:otherwise>
  284. This mode is not implemented yet.
  285. </xsl:otherwise>
  286. </xsl:choose>
  287. </li>
  288. </xsl:template>
  289.  
  290. <xsl:template name="getErrorMessage">
  291. <xsl:param name="error" select="$error"/>
  292. <xsl:param name="field" select="$field"/>
  293. <xsl:choose>
  294. <xsl:when test="$error='isEmpty'">
  295. <span class="error-x">X</span> Field is required.
  296. </xsl:when>
  297. <xsl:when test="$error='stringLengthTooShort'">
  298. <span class="error-x">X</span> This is too short.
  299. </xsl:when>
  300. <xsl:when test="$error='stringLengthTooLong'">
  301. <span class="error-x">X</span> This is too long.
  302. </xsl:when>
  303. <xsl:otherwise>
  304. <span class="error-x">X</span> An unknown error occured. (<samp><xsl:value-of select="$error" /></samp>)
  305. </xsl:otherwise>
  306. </xsl:choose>
  307. </xsl:template>
  308.  
  309. <xsl:template name="getLabel">
  310. <label for="{@id}" title="required" class="required">
  311. <xsl:value-of select="fragment[@id='label']" />
  312. <xsl:if test="fragment[@id='required']='1'">
  313. <xsl:text> </xsl:text>
  314. <span class="required">
  315. <sup>*</sup>required
  316. </span>
  317. </xsl:if>
  318. <xsl:text> </xsl:text>
  319. </label>
  320. <br />
  321. </xsl:template>
  322. </xsl:stylesheet>

URL: http://snipplr.com/view/52713/zendform--object-to-xml-data-representation-for-use-in-xslt/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.