/** * Helper function to render the form. * * See render_p() for a usage example. * * @credit Django Project (http://www.djangoproject.com/) * @param string Normal row. * @param string Error row. * @param string Row ender. * @param string Help text HTML. * @param bool Should we display errors on a separate row. * @return string HTML of the form. */ protected function htmlOutput($normal_row, $error_row, $row_ender, $help_text_html, $errors_on_separate_row) { $top_errors = isset($this->errors['__all__']) ? $this->errors['__all__'] : array(); array_walk($top_errors, 'Pluf_Form_htmlspecialcharsArray'); $output = array(); $hidden_fields = array(); foreach ($this->fields as $name => $field) { $bf = new Pluf_Form_BoundField($this, $field, $name); $bf_errors = $bf->errors; array_walk($bf_errors, 'Pluf_Form_htmlspecialcharsArray'); if ($field->widget->is_hidden) { foreach ($bf_errors as $_e) { $top_errors[] = sprintf(__('(Hidden field %1$s) %2$s'), $name, $_e); } $hidden_fields[] = $bf; // Not rendered } else { if ($errors_on_separate_row and count($bf_errors)) { $output[] = sprintf($error_row, Pluf_Form_renderErrorsAsHTML($bf_errors)); } if (strlen($bf->label) > 0) { $label = htmlspecialchars($bf->label, ENT_COMPAT, 'UTF-8'); if ($this->label_suffix) { if (!in_array(mb_substr($label, -1, 1), array(':', '?', '.', '!'))) { $label .= $this->label_suffix; } } $label = $bf->labelTag($label); } else { $label = ''; } if ($bf->help_text) { // $bf->help_text can contains HTML and is not // escaped. $help_text = sprintf($help_text_html, $bf->help_text); } else { $help_text = ''; } $errors = ''; if (!$errors_on_separate_row and count($bf_errors)) { $errors = Pluf_Form_renderErrorsAsHTML($bf_errors); } $output[] = sprintf($normal_row, $errors, $label, $bf->render_w(), $help_text); } } if (count($top_errors)) { $errors = sprintf($error_row, Pluf_Form_renderErrorsAsHTML($top_errors)); array_unshift($output, $errors); } if (count($hidden_fields)) { $_tmp = ''; foreach ($hidden_fields as $hd) { $_tmp .= $hd->render_w(); } if (count($output)) { $last_row = array_pop($output); $last_row = substr($last_row, 0, -strlen($row_ender)) . $_tmp . $row_ender; $output[] = $last_row; } else { $output[] = $_tmp; } } return new Pluf_Template_SafeString(implode("\n", $output), true); }
/** * Return HTML to display the errors. */ public function fieldErrors() { Pluf::loadFunction('Pluf_Form_renderErrorsAsHTML'); return new Pluf_Template_SafeString(Pluf_Form_renderErrorsAsHTML($this->errors), true); }