redux-framework icon indicating copy to clipboard operation
redux-framework copied to clipboard

Validation woes : $values data structure is bogus

Open sandrobilbeisi opened this issue 12 years ago • 9 comments

Validation woes : $values data structure is bogus

The framework seems to be a bit weak when it comes to validation multi-value fields this may require some patching to the main framework.php

sandrobilbeisi avatar Dec 12 '13 20:12 sandrobilbeisi

Yea, that was just added recently. We never had it before. You're welcome to try and fix it.

dovy avatar Dec 12 '13 20:12 dovy

I found the same problem and I have done some changes, maybe is not the best way but right now I have not time to rewrite with more detail framework.php (sorry for my english):

in framework.php :


public function _validate_values( $plugin_options, $options ) {
            //error_log(json_encode($this->sections));
            foreach( $this->sections as $k => $section ) {
                if( isset( $section['fields'] ) ) {
                    foreach( $section['fields'] as $field ) {
                        $field['section_id'] = $k;
                        if( isset( $field['type'] ) && ( $field['type'] == 'checkbox' || $field['type'] == 'checkbox_hide_below' || $field['type'] == 'checkbox_hide_all' ) ) {
                            if( !isset( $plugin_options[$field['id']] ) )
                                $plugin_options[$field['id']] = 0;
                        }
                        if( isset( $field['type'] ) && $field['type'] == 'multi_text' ) continue; // We can't validate this yet
                        if( !isset( $plugin_options[$field['id']] ) || $plugin_options[$field['id']] == '' ) continue;
                        // Force validate of custom field types
                        if( isset( $field['type'] ) && !isset( $field['validate'] ) ) {
                            if( $field['type'] == 'color' || $field['type'] == 'color_gradient' ) {
                                $field['validate'] = 'color';
                            } elseif( $field['type'] == 'date' ) {
                                $field['validate'] = 'date';
                            }elseif( $field['type'] == 'group' ){
                                foreach($field['subfields'] as $key => $subfields){
                                    if( $subfields['type'] == 'color' || $subfields['type'] == 'color_gradient' ) {
                                        $subfields['validate'] = 'color';
                                    } elseif( $subfields['type'] == 'date' ) {
                                        $subfields['validate'] = 'date';
                                    }
                                    if( isset( $subfields['validate'] ) ){
                                        $validate = 'Redux_Validation_' . $subfields['validate'];
                                        foreach($plugin_options[$subfields['id']] as $keys => $subfield){
                                            if( !class_exists( $validate ) ) {
                                                $class_file = apply_filters( 'redux-validateclass-load', self::$_dir . 'inc/validation/' . $subfields['validate'] . '/validation_' . $subfields['validate'] . '.php', $validate );
                                                if( $class_file ) {
                                                    /** @noinspection PhpIncludeInspection */
                                                    require_once( $class_file );
                                                }
                                            }
                                            if( class_exists( $validate ) ) {
                                                //!DOVY - DB saving stuff. Is this right?
                                                /*if ( empty ( $options[$field['id']] ) ) {
                                                    $options[$field['id']] = '';
                                                }*/
                                                $validation = new $validate( $subfields, $plugin_options[$subfields['id']][$keys], $options[$subfields['id']][$keys]  );
                                                $plugin_options[$field['id'][$keys]] = $validation->value;
                                                if( isset( $validation->error ) ){
                                                    $validation->error['group'] = '1';
                                                    $validation->error['key'] = $keys;
                                                    $validation->error['section_id'] = $k;
                                                    $this->errors[] = $validation->error;
                                                    error_log(json_encode($k));
                                                }
                                                if( isset( $validation->warning) )
                                                    $this->warnings[] = $validation->warning;
                                                //continue;
                                            }
                                        }
                                    }
                                    if( isset( $subfields['validate_callback'] ) && function_exists( $subfields['validate_callback'] ) ) {
                                        $callbackvalues = call_user_func( $subfields['validate_callback'], $subfields, $plugin_options[$subfields['id']][$keys], $options[$subfields['id']][$keys] );
                                        $plugin_options[$subfields['id']][$keys] = $callbackvalues['value'];
                                        if( isset( $callbackvalues['error'] ) )
                                            $this->errors[] = $callbackvalues['error'];
                                        if( isset( $callbackvalues['warning'] ) )
                                            $this->warnings[] = $callbackvalues['warning'];
                                    }
                                }
                            }
                        }
                        if( isset( $field['validate'] ) ) {
                            $validate = 'Redux_Validation_' . $field['validate'];
                            if( !class_exists( $validate ) ) {
                                $class_file = apply_filters( 'redux-validateclass-load', self::$_dir . 'inc/validation/' . $field['validate'] . '/validation_' . $field['validate'] . '.php', $validate );
                                if( $class_file ) {
                                    /** @noinspection PhpIncludeInspection */
                                    require_once( $class_file );
                                }
                            }
                            if( class_exists( $validate ) ) {
                                //!DOVY - DB saving stuff. Is this right?
                                if ( empty ( $options[$field['id']] ) ) {
                                    $options[$field['id']] = '';
                                }
                                $validation = new $validate( $field, $plugin_options[$field['id']], $options[$field['id']] );
                                $plugin_options[$field['id']] = $validation->value;
                                if( isset( $validation->error ) )
                                    $this->errors[] = $validation->error;
                                if( isset( $validation->warning) )
                                    $this->warnings[] = $validation->warning;
                                continue;
                            }
                        }
                        if( isset( $field['validate_callback'] ) && function_exists( $field['validate_callback'] ) ) {
                            $callbackvalues = call_user_func( $field['validate_callback'], $field, $plugin_options[$field['id']], $options[$field['id']] );
                            $plugin_options[$field['id']] = $callbackvalues['value'];
                            if( isset( $callbackvalues['error'] ) )
                                $this->errors[] = $callbackvalues['error'];
                            if( isset( $callbackvalues['warning'] ) )
                                $this->warnings[] = $callbackvalues['warning'];
                        }
                    }
                }
            }
            return $plugin_options;
        }

in admin.js:


if (redux_opts.errors !== undefined) {
        jQuery("#redux-field-errors span").html(redux_opts.errors.total);
        jQuery("#redux-field-errors").show();
        jQuery.each(redux_opts.errors.errors, function(sectionID, sectionArray) {
            jQuery("#" + sectionID + "_section_group_li_a").prepend('' + sectionArray.total + '');
            jQuery("#" + sectionID + "_section_group_li_a").addClass("hasError");
            console.log('section ID'+ sectionID);
            jQuery.each(sectionArray.errors, function(key, value) {
                console.log(value);
                if( value.group !== undefined){
                    var $selectDiv = jQuery('[name = "'+redux_opts.opt_name+'['+value.id+']'+'['+value.key+']"]');
                    $selectDiv.parent().addClass("redux-field-error");
                    $selectDiv.parent().append('
' + value.msg + '
'); $selectDiv.closest('.redux-groups-accordion-group').find('> h3').css('border-color','#b94a48'); }else{ jQuery("#" + redux_opts.opt_name+'-'+value.id).addClass("redux-field-error"); jQuery("#" + redux_opts.opt_name+'-'+value.id).append('
' + value.msg + '
'); } }); }); }

I hope this could help you;

elcrespo avatar Dec 13 '13 16:12 elcrespo

Can someone else verify if this functions as it should?

dovy avatar Dec 13 '13 16:12 dovy

Sorry, the code I wrote is for the "group field" validation, I don't test it yet with "multi-value" fields. :( I read fast and I thought that you were talking about "group field". My bad.

elcrespo avatar Dec 13 '13 19:12 elcrespo

Oh.... Can you look at both? :)

dovy avatar Dec 13 '13 19:12 dovy

It works for Color RGBA !!! YAY !!!

sandrobilbeisi avatar Dec 13 '13 22:12 sandrobilbeisi

So did we fully implement this or not? @kprovance Did you touch this?

dovy avatar Dec 25 '13 19:12 dovy

I have not touched it. I'm not sure what is going on with this one.

kprovance avatar Dec 25 '13 20:12 kprovance

So, @sandrobilbeisi Can you tell me if i change to this code if everything will work or will all validation need to be resolved?

On a side note, need to separate sterilization and validation badly. hah.

dovy avatar Dec 31 '13 16:12 dovy