Skip to:
Content
Pages
Categories
Search
Top
Bottom

Validating xProfile fields – limit length


  • Henry
    Member

    @henrywright-1

    I am trying to limit the length of 2 xProfile fields (location {field id 2) and about {field id 3})

    This is what I have come up with so far for location.

    function custom_validate_xprofile_location( $data ) {
        global $bp;
        $max_location_length = 56;
        if ( 2 == $data->field_id ) {
            if ( strlen( $data->value ) > $max_location_length ) {
                bp_core_add_message( 'Too many characters old chap. Please try again', 'error' );
                wp_redirect( $bp->loggedin_user->domain . 'profile/edit/group/1/' );
                exit();
        	}
        }
        return $data;
    }
    add_action( 'xprofile_data_before_save', 'custom_validate_xprofile_location' );

    2 questions:

    1. Is this the best way of doing it?
    2. How do I factor in limiting the length of the about field as well? I think this bit is harder than it looks.

    Note: I’ve also got maxlength=x in my form inputs.

Viewing 3 replies - 1 through 3 (of 3 total)

  • danbp
    Moderator

    @danbp


    Henry
    Member

    @henrywright-1

    Hi @danbp, thanks for the link. That isn’t quite what I’m after. It is more to do with limiting the length of what is echo’d to screen where as I’m trying to validate and error handle the data which is captured.


    Henry
    Member

    @henrywright-1

    After taking a look at how BP does it my questions 1 and 2 have been answered. Now facing a new but related problem:

    When I redirect the user back to the edit profile page to display the “Too many characaters have been entered” message – any new text the user may have entered into the input fields is lost.

    For example – the max characater limit for location is 10

    1. Location value is currently: London
    2. The user updates the location field to: London, United Kingdom
    3. The field validation kicks in – the page is refreshed with error message displayed “Too many characters”
    4. Location value reads: London

    So, you can see the illegal value the user has entered “London, United Kingdom” has been lost. My aim is to redisplay the illegal value so the user can modify it under 10 characters.

    Perhaps location isn’t the best example. Imagine if the user has written an about me paragraph which is over the character limit – on clicking submit the page refreshes and they are told they’ve entered too many characters. Their old about me text is displayed and all their new hard work has been lost.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Validating xProfile fields – limit length’ is closed to new replies.
Skip to toolbar