Posted By

iroybot on 04/28/11


Tagged

php zend zendframework ZendForm


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

iroybot


Zend_Form Object to XML Data Representation (for use in XSLT)


 / Published in: PHP
 

URL: http://snipplr.com/view/52714/example-xml-representation-of-zendform/

Turns a Zend_Form object into XML data. Click the associated link to see an Example XML result and a XSL Stylesheet used to display regular HMTL.

  1. class XML_Form extends XML_Helper {
  2. /**
  3. *
  4. * Turns a Zend_Form object into a DOMDocument for use in your XSLT-powered frontend
  5. * @param Zend_Form $object
  6. * @param array $data to validate form against
  7. * @return DOMDocument
  8. */
  9. public static function formToDOM(Zend_Form $object=null, $data=null) {
  10. if (is_a($object, "Zend_Form")) {
  11. if (!is_null($data)) {
  12. if ($object->isValid($data)) $state = "valid";
  13. else $state = "fail";
  14. } else {
  15. $state = "init";
  16. }
  17.  
  18. $doc = new DOMDocument('1.0', 'UTF-8');
  19. $doc->loadXML('<item type="form" />');
  20. $root = $doc->documentElement;
  21.  
  22. // 1st level item
  23. $item = $doc->getElementsByTagName('item')->item(0);
  24. $item->setAttribute('id',$object->getAttrib('id'));
  25. $item->setAttribute('state',$state);
  26.  
  27. // 2nd level fragment form properties
  28. $options = $object->getAttribs();
  29. $options['enctype'] = $object->getEnctype();
  30. foreach ($options as $attribute => $value) {
  31. $fragment = $doc->createElement('fragment',$value);
  32. $fragment->setAttribute('id',$attribute);
  33. $item->appendChild($fragment);
  34. }
  35.  
  36. // 3rd level group for form elements
  37. $group_fields = $doc->createElement('group');
  38. $group_fields->setAttribute('id','fields');
  39. foreach ($object->getElements() as $element) {
  40. /** set redirect value if <input type="hidden" name="redirect" /> is present in current form and $_POST
  41. * @todo test redirect functionality, retrieve redirect value in
  42. **/
  43.  
  44. $class = strtolower(str_replace('Zend_Form_Element_','',get_class($element)));
  45. if ($class === 'hidden' && $element->getName() === 'redirect' && isset($_SESSION['reservationform'])) {
  46. $lang = Royal_Language::getLanguage();
  47. $region = Royal_FrontendRegion::getRegion();
  48. $element->setValue("/$lang/$region/memberhome/");
  49. }
  50.  
  51. // 3rd level form elements
  52. $item_element = $doc->createElement('item');
  53. $item_element->setAttribute('id',$element->getName());
  54. $item_element->setAttribute('type', $class);
  55.  
  56. //4th level fragments of element
  57. parent::createFragment('name', $item_element, $element->getName());
  58. parent::createFragment('label', $item_element, $element->getLabel());
  59. parent::createFragment('value', $item_element, $element->getValue());
  60. parent::createFragment('required', $item_element, $element->isRequired());
  61. parent::createFragment('maxlength', $item_element, $element->getAttrib('maxlength'));
  62. parent::createFragment('size', $item_element, $element->getAttrib('size'));
  63. parent::createFragment('readonly', $item_element, $element->getAttrib('readonly'));
  64. parent::createFragment('description', $item_element, $element->getDescription());
  65. if (!is_null($data) && array_key_exists($element->getName(), $data)) {
  66. foreach ($element->getErrors() as $error) {
  67. parent::createFragment('error', $item_element, $error);
  68. }
  69. }
  70. // possible errors
  71. if ($element->isValid($data[$element->getName()])===true) {
  72. $valid = 'true';
  73. } else {
  74. $valid = 'false';
  75. }
  76. parent::createFragment('valid', $item_element, $valid);
  77.  
  78. if (method_exists($element, 'getMultiOptions')) {
  79. $group_options = parent::createGroup('options', $item_element);
  80. foreach ($element->getMultiOptions() as $key => $text) {
  81. $item_option = $doc->createElement('item');
  82. $item_option->setAttribute('id', 'option');
  83. parent::createFragment('text', $item_option, $text);
  84. parent::createFragment('value', $item_option, $key);
  85. $group_options->appendChild($item_option);
  86. }
  87. }
  88.  
  89. $group_fields->appendChild($item_element);
  90. $item->appendChild($group_fields);
  91. }
  92. return $doc->saveXML($root, LIBXML_NOEMPTYTAG); // finally return DOMDocument
  93. } else {
  94. throw new Exception("Parameter <samp>\$object</samp> passed to XML_Form::formToDOM() is required and needs to be of type Zend_Form");
  95. }
  96. }
  97. }
  98.  
  99. class XML_Helper {
  100. /**
  101. *
  102. * __constructor
  103. */
  104. public function Royal_XML_Creator() {
  105. }
  106.  
  107. /**
  108. *
  109. * Appends a <fragment id='id'>value</fragment> to a given origin and returns the node for further use
  110. * @param string $id
  111. * @param DOMNode $origin
  112. * @param string $value
  113. * @return DOMNode
  114. */
  115. public static function createFragment($id, DOMNode $appendTo, $value='') {
  116. /** @todo maybe encode the value in order to make sure the XML stays valid (non-UTF-8 characters, HTML, etc.) **/
  117. $fragment = $appendTo->ownerDocument->createElement('fragment', $value);
  118. $fragment->setAttribute('id', (string) $id);
  119. $appendTo->appendChild($fragment);
  120. return $fragment;
  121. }
  122.  
  123. /**
  124. *
  125. * Appends a <fragment id='id'><![CDATA[value]]></fragment> to a given DOM
  126. * @param unknown_type $id
  127. * @param DOMNode $origin
  128. * @param unknown_type $value
  129. */
  130. public static function createCDATAFragment($id, DOMNode $appendTo, $value='') {
  131. $fragment = $appendTo->ownerDocument->createElement('fragment');
  132. $fragment->appendChild($appendTo->ownerDocument->createCDATASection($value));
  133. $fragment->setAttribute('id', $id);
  134. $appendTo->appendChild($fragment);
  135. return $fragment;
  136. }
  137.  
  138. /**
  139. *
  140. * Appends a group <group id='id' /> to a given origin
  141. * @param string $id
  142. * @param DOMNode $origin
  143. * @return DOMNode
  144. */
  145. public static function createGroup($id, DOMNode $appendTo) {
  146. $group = $appendTo->ownerDocument->createElement('group');
  147. $group->setAttribute('id', $id);
  148. $appendTo->appendChild($group);
  149. return $group;
  150. }
  151. }
  152.  
  153. /**
  154. Example Usage:
  155. **/
  156. $proc = new XSLTProcessor();
  157. $xsldoc = new DOMDocument();
  158. $xsldoc->load(APPLICATION_PATH.'/xsl/forms.xsl');
  159. $proc->importStyleSheet($xsldoc);
  160. echo $proc->transformToXML(new XML_Form::formToDOM(new My_Custom_Zend_Form()));

Report this snippet  

You need to login to post a comment.