Skip to:
Content
Pages
Categories
Search
Top
Bottom

Custom Error Message and Validation of Profile Fields


  • bcanr2d2
    Participant

    @bcanr2d2

    My Scenario:

    I only have one profile group, and two member types.

    Each of them has some shared required fields, which I have set as required, but I would like some additional fields to be made required for each member type.

    IE

    Shared Member Fields: Name, Age

    Separate for Member Type A: XProfile Field A

    Separate for Member Type B: Xprofile Field B

    Currently, I am showing this fields conditionally using the Conditional Profile Fields for BuddyPress plugin as they select a Member Type, and at this stage I’d consider them mandatory, although I can’t have both XProfile Field A and B both marked as required in BuddyPress.

    I’ve looked up a couple of solutions, and mine is mainly based on the following: buddypress bp_core_add_message() doesn’t fire why?

    For a required field, it shows the error message, but not so for the custom error message I want to display, could someone give me a pointer as to why?

     //Validate and force values for items that are NOT required
    function validate_required_unshared_fields($data) {
            global $bp;
    //check for member type
    $member_type_field_id = xprofile_get_field_id_from_name('Member Type');
    $member_type = $_POST['field_'.$member_type_field_id];
    switch($member_type){
    case "babysitterornanny":
    $cost_per_hour_field_id = xprofile_get_field_id_from_name('Cost Per Hour');
    $cost_per_hour = $_POST['field_'.$cost_per_hour_field_id];
    
    if ($cost_per_hour ==''){
    
    bp_core_add_message( __( 'You need to fill out your Cost Per Hour.', 'buddypress' ), 'error' );
              wp_redirect( $bp->loggedin_user->domain . 'profile/edit/group/1/' );
    
           exit();
    }
    
    }
    
    return $data;
    }
    
    add_action( 'xprofile_data_before_save', 'validate_required_unshared_fields');

    I’ve changed to a default Twenty12 theme, still doesn’t display, what is causing it not to display??

  • You must be logged in to reply to this topic.
Skip to toolbar