function _action_theme_display_form_errors() { $form = FW_Form::get_submitted(); if (!$form || $form->is_valid()) { return; } wp_enqueue_script('fw-theme-show-form-errors', get_template_directory_uri() . '/js/form-errors.js', array('jquery'), '1.0', true); wp_localize_script('fw-theme-show-form-errors', '_localized_form_errors', array('errors' => $form->get_errors(), 'form_id' => $form->get_id())); }
/** * Detect if form errors was not displayed in frontend then display them with default design * Do nothing if the theme already displayed the errors * @internal */ function _action_show_fw_form_errors_in_frontend() { $form = FW_Form::get_submitted(); if (!$form || $form->is_valid()) { return; } if ($form->errors_accessed()) { // already displayed return; } foreach ($form->get_errors() as $input_name => $error_message) { FW_Flash_Messages::add('fw-form-error-' . $input_name, $error_message, 'error'); } }
/** * Display form errors in admin side */ function _action_show_fw_form_errors_in_admin() { $form = FW_Form::get_submitted(); if (!$form || $form->is_valid()) { return; } foreach ($form->get_errors() as $input_name => $error_message) { FW_Flash_Messages::add('fw-form-admin-' . $input_name, $error_message, 'error'); } }
/** * Render form's html * * @param array $data */ public function render($data = array()) { $render_data = array('submit' => array('value' => __('Submit', 'fw'), 'html' => null), 'data' => $data, 'attr' => $this->attr); unset($data); if ($this->callbacks['render']) { ob_start(); $data = call_user_func_array($this->callbacks['render'], array($render_data, $this)); $html = ob_get_clean(); if (empty($data)) { // fix if returned wrong data from callback $data = $render_data; } $render_data = $data; unset($data); } // display form errors in frontend do { if (is_admin()) { // errors in admin side are displayed by a script at the end of this file break; } $submitted_form = FW_Form::get_submitted(); if (!$submitted_form) { break; } if ($submitted_form->get_id() !== $this->get_id()) { // the submitted form is not current form break; } unset($submitted_form); // not needed anymore, below will be used only with $this (because it's the same form) if ($this->is_valid()) { break; } /** * Use this action to customize errors display in your theme */ do_action('fw_form_display_errors_frontend', $this); if ($this->errors_accessed()) { // already displayed, prevent/cancel default display break; } $errors = $this->get_errors(); if (empty($errors)) { break; } echo '<ul class="fw-form-errors">'; foreach ($errors as $input_name => $error_message) { echo fw_html_tag('li', array('data-input-name' => $input_name), $error_message); } echo '</ul>'; unset($errors); } while (false); echo '<form ' . fw_attr_to_html($render_data['attr']) . ' >'; echo fw_html_tag('input', array('type' => 'hidden', 'name' => self::$id_input_name, 'value' => $this->id)); if ($render_data['attr']['method'] == 'post') { wp_nonce_field('submit_fwf', '_nonce_' . md5($this->id)); } if (!empty($render_data['attr']['action']) && $render_data['attr']['method'] == 'get') { /** * Add query vars from the action attribute url to hidden inputs to not loose them */ parse_str(parse_url($render_data['attr']['action'], PHP_URL_QUERY), $query_vars); if (!empty($query_vars)) { foreach ($query_vars as $var_name => $var_value) { echo fw_html_tag('input', array('type' => 'hidden', 'name' => $var_name, 'value' => $var_value)); } } } echo $html; // In filter can be defined custom html for submit button if (isset($render_data['submit']['html'])) { echo $render_data['submit']['html']; } else { echo fw_html_tag('input', array('type' => 'submit', 'value' => $render_data['submit']['value'])); } echo '</form>'; }