Ejemplo n.º 1
0
 /**
  * Create a form with controls from HTML
  * @param string $name Name of the form
  * @param string $html HTML of a form
  * @return FormUI The form created from the supplied HTML
  */
 public static function from_html($name, $html)
 {
     $dom = new HTMLDoc($html);
     $form = new FormUI($name);
     // Set the form to render using the output of the HTMLDoc
     $form->settings['norender'] = true;
     $form->settings['content'] = function () use($dom) {
         return $dom->get();
     };
     $form->dom = $dom;
     // This form's id is an MD5 hash of the original HTML
     $form->settings['control_id'] = md5($html);
     // Add the _form_id and WSSE elements to the form
     // Note that these additional fields must be XML-safe, or they won't be added
     $dom->find_one('form')->append_html(Utils::setup_wsse());
     $dom->find_one('form')->append_html('<input type="hidden" name="_form_id" value="' . $form->control_id() . '" />');
     // Add synthetic controls for any found inputs
     foreach ($dom->find('input') as $input) {
         /** @var FormControl $control */
         $form->append($control = FormControlDom::create($input->name)->set_node($input));
         if ($input->data_validators) {
             foreach (explode(' ', $input->data_validators) as $validator) {
                 $control->add_validator($validator);
             }
         }
     }
     foreach ($dom->find('textarea') as $input) {
         /** @var FormControl $control */
         $form->append($control = FormControlDom::create($input->name)->set_node($input));
         if (!empty($input->data_validators)) {
             foreach (explode(' ', $input->data_validators) as $validator) {
                 $control->add_validator($validator);
             }
         }
     }
     return $form;
 }