/**
  * @return string Proper display name suitable for direct displaying to the user.
  *
  * Handles string translation and adds an asterisk if the field is required.
  *
  * @since 1.9
  */
 public function get_display_name()
 {
     // Try to translate through standard toolset-forms method
     $string_name = sprintf('field %s name', $this->get_slug());
     $display_name = WPToolset_Types::translate($string_name, $this->get_name());
     // Add an asterisk if the field is required
     if ($this->get_is_required() && !empty($display_name)) {
         $display_name .= '*';
     }
     return $display_name;
 }
Exemple #2
0
 /**
  * New validation using API calls from toolset-forms.
  *
  * Uses API cals
  * @uses wptoolset_form_validate_field()
  * @uses wptoolset_form_conditional_check()
  *
  * @todo Make it work with other fields (generic)
  *
  * @param type $post_id
  * @param type $values
  * @return boolean
  */
 function validate($post_id, $values)
 {
     $form_id = $this->form_id;
     $valid = true;
     // Loop over fields
     $form_source_data = $this->_formData->getForm()->post_content;
     preg_match_all("/\\[cred_show_group.*cred_show_group\\]/Uism", $form_source_data, $res);
     $conditional_group_fields = array();
     if (count($res[0]) > 0) {
         for ($i = 0, $res_limit = count($res[0]); $i < $res_limit; $i++) {
             preg_match_all("/field=\"([^\"]+)\"/Uism", $res[0][$i], $parsed_fields);
             if (count($parsed_fields[1]) > 0) {
                 for ($j = 0, $count_parsed_fields = count($parsed_fields); $j < $count_parsed_fields; $j++) {
                     if (!empty($parsed_fields[1][$j])) {
                         $conditional_group_fields[] = trim($parsed_fields[1][$j]);
                     }
                 }
             }
         }
     }
     foreach ($this->form_properties['fields'] as $field) {
         if (in_array(str_replace('wpcf-', '', $field['name']), $conditional_group_fields)) {
             continue;
         }
         // If Types field
         if (isset($field['plugin_type']) && $field['plugin_type'] == 'types') {
             $field_name = $field['name'];
             if (!isset($_POST[$field_name])) {
                 continue;
             }
             /* 	
                               // Adjust field ID
                               $field['id'] = strpos( $field['name'], 'wpcf-' ) === 0 ? substr( $field['name'], 5 ) : $field['name'];
                               // CRED have synonym 'text' for textfield
                               if ( $field['type'] == 'text' ) {
                               $field['type'] = 'textfield';
                               } */
             $field = wpcf_fields_get_field_by_slug(str_replace('wpcf-', '', $field['name']));
             if (empty($field)) {
                 continue;
             }
             // Skip copied fields
             if (isset($_POST['wpcf_repetitive_copy'][$field['slug']])) {
                 continue;
             }
             // Set field config
             $config = wptoolset_form_filter_types_field($field, $post_id);
             //Add custom colorpicer hexadecimal backend validator
             //Fix https://icanlocalize.basecamphq.com/projects/7393061-toolset/todo_items/193712665/comments
             //Todo in future add hexadecimal check in types
             if ($field['type'] == 'colorpicker') {
                 $config['validation']['hexadecimal'] = array('args' => array('$value', '1'), 'message' => 'Enter valid hexadecimal value');
             }
             if (isset($config['conditional']['values'])) {
                 foreach ($config['conditional']['values'] as $post_key => $post_value) {
                     if (isset($this->form_properties['fields'][$post_key])) {
                         $config['conditional']['values'][$post_key] = $this->form_properties['fields'][$post_key]['value'];
                     }
                 }
             }
             // Set values to loop over
             $_values = !empty($values[$field_name]) ? $values[$field_name]['value'] : null;
             if (empty($config['repetitive'])) {
                 $_values = array($_values);
             }
             // Loop over each value
             if (is_array($_values)) {
                 foreach ($_values as $value) {
                     $validation = wptoolset_form_validate_field($form_id, $config, $value);
                     $conditional = wptoolset_form_conditional_check($config);
                     /**
                      * add form_errors messages
                      */
                     if (is_wp_error($validation) && $conditional) {
                         $error_data = $validation->get_error_data();
                         if (isset($error_data[0])) {
                             $this->add_top_message($error_data[0], $config['id']);
                         } else {
                             $this->add_top_message($validation->get_error_message(), $config['id']);
                         }
                         $valid = false;
                         if (empty($ret_validation)) {
                             continue;
                         }
                         //                            foreach( $errors as $error ) {
                         //                                $error = explode( ' ', $error );
                         //                                $key = array_shift($error);
                         //                                $error = implode( ' ', $error );
                         //                                $this->form_errors[$key] = $error;
                         //                                $this->form_messages[$key] = $validation->get_error_message();
                         //                            }
                     }
                 }
             }
         } elseif (!isset($field['plugin_type']) && isset($field['validation'])) {
             if (!isset($_POST[$field['name']])) {
                 continue;
             }
             $config = array('id' => $field['name'], 'type' => $field['type'], 'slug' => $field['name'], 'title' => $field['name'], 'description' => '', 'name' => $field['name'], 'repetitive' => $field['repetitive'], 'validation' => $field['validation'], 'conditional' => array());
             $value = $field['value'];
             require_once WPTOOLSET_FORMS_ABSPATH . '/classes/class.types.php';
             $validation = array();
             foreach ($field['validation'] as $rule => $settings) {
                 if ($settings['active']) {
                     $id = $config['slug'];
                     $validation[$rule] = array('args' => isset($settings['args']) ? array_unshift($value, $settings['args']) : array($value, true), 'message' => WPToolset_Types::translate('field ' . $id . ' validation message ' . $rule, $settings['message']));
                 }
             }
             $config['validation'] = $validation;
             $validation = wptoolset_form_validate_field($form_id, $config, $value);
             if (is_wp_error($validation)) {
                 $error_data = $validation->get_error_data();
                 //TODO: replace id with name
                 if (isset($error_data[0])) {
                     $this->add_top_message($error_data[0], $config['id']);
                 } else {
                     $this->add_top_message($validation->get_error_message(), $config['id']);
                 }
                 $valid = false;
                 if (empty($ret_validation)) {
                     continue;
                 }
                 //                    foreach( $errors as $error ) {
                 //                        $error = explode( ' ', $error );
                 //                        $key = array_shift($error);
                 //                        $error = implode( ' ', $error );
                 //                        $this->form_errors[$key] = $error;
                 //                        $this->form_messages[$key] = $validation->get_error_message();
                 //                    }
             }
         }
     }
     return $valid;
 }