Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
07/24/2011 01:17:12 PM (13 years ago)
Author:
djpaul
Message:

Fix profile field value handling of existing settings and new values when form submission fails validation. Props r-a-y for patch.
Change select box behaviour to not show "---" placeholder when field is required.
Fixes #3378

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-xprofile/bp-xprofile-template.php

    r4827 r4831  
    326326         * value as long as it's not empty and a required field.
    327327         */
    328         if ( isset( $_POST['field_' . $field->id] ) && isset( $field->data->value ) && $field->data->value != $_POST['field_' . $field->id] ) {
     328        if ( !isset( $field->data->value ) )
     329            $field->data->value = '';
     330
     331        if ( isset( $_POST['field_' . $field->id] ) && $field->data->value != $_POST['field_' . $field->id] ) {
    329332            if ( !empty( $_POST['field_' . $field->id] ) )
    330333                $field->data->value = $_POST['field_' . $field->id];
     334            else
     335                $field->data->value = '';
    331336        }
    332337
     
    417422
    418423        switch ( $field->type ) {
    419 
    420             case 'selectbox': case 'multiselectbox':
    421                 if ( 'multiselectbox' != $field->type )
    422                     $html .= '<option value="">--------</option>';
    423 
     424            case 'selectbox':
     425                if ( !$field->is_required )
     426                    $html .= '<option value="">' . /* translators: no option picked in select box */ __( '----', 'buddypress' ) . '</option>';
     427
     428                $original_option_values = '';
    424429                $original_option_values = maybe_unserialize( BP_XProfile_ProfileData::get_value_byid( $field->id ) );
     430
     431                if ( empty( $original_option_values ) && !empty( $_POST['field_' . $field->id] ) )
     432                    $original_option_values = $_POST['field_' . $field->id];
     433
    425434                $option_values = (array) $original_option_values;
    426435
    427                 for ( $k = 0; $k < count( $options ); $k++ ) {
     436                for ( $k = 0, $count = count( $options ); $k < $count; ++$k ) {
    428437                    // Check for updated posted values, but errors preventing them from being saved first time
    429438                    foreach( $option_values as $i => $option_value ) {
     
    433442                        }
    434443                    }
    435 
    436444                    $selected = '';
    437445
    438                     // Run the allowed option name through the before_save
    439                     // filter, so we'll be sure to get a match
     446                    // Run the allowed option name through the before_save filter, so we'll be sure to get a match
    440447                    $allowed_options = xprofile_sanitize_data_value_before_save( $options[$k]->name, false, false );
    441448
    442                     // First, check to see whether the user-entered value
    443                     // matches
     449                    // First, check to see whether the user-entered value matches
    444450                    if ( in_array( $allowed_options, (array) $option_values ) )
    445451                        $selected = ' selected="selected"';
    446452
    447                     // Then, if the user has not provided a value, check for
    448                     // defaults
    449                     if ( !is_array( $original_option_values ) && empty( $option_values ) & $options[$k]->is_default_option )
     453                    // Then, if the user has not provided a value, check for defaults
     454                    if ( !is_array( $original_option_values ) && empty( $option_values ) && $options[$k]->is_default_option )
    450455                        $selected = ' selected="selected"';
    451456
    452457                    $html .= apply_filters( 'bp_get_the_profile_field_options_select', '<option' . $selected . ' value="' . esc_attr( stripslashes( $options[$k]->name ) ) . '">' . esc_attr( stripslashes( $options[$k]->name ) ) . '</option>', $options[$k] );
     458                }
     459                break;
     460
     461            case 'multiselectbox':
     462                $original_option_values = '';
     463                $original_option_values = maybe_unserialize( BP_XProfile_ProfileData::get_value_byid( $field->id ) );
     464
     465                if ( empty( $original_option_values ) && !empty( $_POST['field_' . $field->id] ) )
     466                    $original_option_values = $_POST['field_' . $field->id];
     467
     468                $option_values = (array) $original_option_values;
     469
     470                for ( $k = 0, $count = count( $options ); $k < $count; ++$k ) {
     471                    // Check for updated posted values, but errors preventing them from being saved first time
     472                    foreach( $option_values as $i => $option_value ) {
     473                        if ( isset( $_POST['field_' . $field->id] ) && $_POST['field_' . $field->id][$i] != $option_value ) {
     474                            if ( !empty( $_POST['field_' . $field->id][$i] ) )
     475                                $option_values[] = $_POST['field_' . $field->id][$i];
     476                        }
     477                    }
     478                    $selected = '';
     479
     480                    // Run the allowed option name through the before_save filter, so we'll be sure to get a match
     481                    $allowed_options = xprofile_sanitize_data_value_before_save( $options[$k]->name, false, false );
     482
     483                    // First, check to see whether the user-entered value matches
     484                    if ( in_array( $allowed_options, (array) $option_values ) )
     485                        $selected = ' selected="selected"';
     486
     487                    // Then, if the user has not provided a value, check for defaults
     488                    if ( !is_array( $original_option_values ) && empty( $option_values ) && $options[$k]->is_default_option )
     489                        $selected = ' selected="selected"';
     490
     491                    $html .= apply_filters( 'bp_get_the_profile_field_options_multiselect', '<option' . $selected . ' value="' . esc_attr( stripslashes( $options[$k]->name ) ) . '">' . esc_attr( stripslashes( $options[$k]->name ) ) . '</option>', $options[$k] );
    453492                }
    454493                break;
     
    458497                $option_value = BP_XProfile_ProfileData::get_value_byid( $field->id );
    459498
    460                 for ( $k = 0; $k < count( $options ); $k++ ) {
     499                for ( $k = 0, $count = count( $options ); $k < $count; ++$k ) {
    461500                    // Check for updated posted values, but errors preventing them from being saved first time
    462501                    if ( isset( $_POST['field_' . $field->id] ) && $option_value != $_POST['field_' . $field->id] ) {
     
    489528                }
    490529
    491                 for ( $k = 0; $k < count( $options ); $k++ ) {
     530                for ( $k = 0, $count = count( $options ); $k < $count; ++$k ) {
    492531                    $selected = '';
    493532
    494533                    // First, check to see whether the user's saved values
    495534                    // match the option
    496                     for ( $j = 0; $j < count( $option_values ); $j++ ) {
     535                    for ( $j = 0, $count = count( $option_values ); $j < $count; ++$j ) {
    497536
    498537                        // Run the allowed option name through the
    499                         // before_save filter, so we'll be sure to get a
    500                         // match
     538                        // before_save filter, so we'll be sure to get a match
    501539                        $allowed_options = xprofile_sanitize_data_value_before_save( $options[$k]->name, false, false );
    502540
Note: See TracChangeset for help on using the changeset viewer.