BuddyDev

Search

[Resolved] Register custom error messages

  • Participant
    Level: Enlightened
    Posts: 26
    Ahmed on #33024

    Hi

    I use this tutorials to customize my register form :
    https://buddydev.com/show-buddypress-user-registration-form-everywhere-buddypress-site/
    https://buddydev.com/buddypress-better-registration-part-1-remove-full-name-confirm-password-from-buddypress-registration/

    In my form ask firstname, lastname, email and password.

    Here my custom fields in register-form.php:

    <!-- CUSTOM AHMED -->
     <div class="yoostart_name_details">
         <div class="lastname_field">
             <input type="text" name="yoostart_signup_lastname" id="yoostart_signup_lastname" value="<?= get_input('yoostart_signup_lastname') ?>" placeholder="Nom" required>
         </div>
         <div class="firstname_field">
             <input type="text" name="yoostart_signup_firstname" id="yoostart_signup_firstname" value="<?= get_input('yoostart_signup_firstname') ?>" placeholder="Prénom" required>
         </div>
     </div>
     <!-- CUSTOM AHMED -->
    

    and here my signup prevalidate hook:

    function inject_datas()
    {
        $lastname       = sanitize_text_field($_POST['yoostart_signup_lastname']);
        $firstname      = sanitize_text_field($_POST['yoostart_signup_firstname']);
        $fullname       = ucfirst($firstname) . ' ' . ucfirst($lastname);
        $email          = $_POST['signup_email'];
        $exploded_email = explode('@', $email);
        $username       = $exploded_email[0] . '-' . strtolower($firstname);
    
        $_POST['signup_username'] = $username;
        $_POST['field_1'] = $fullname;
        $_POST['signup_password_confirm'] = isset($_POST['signup_password']) ? $_POST['signup_password'] : '';
    }

    the register process work , my question is, how to add custom error messages for my 2 custom fields (firstname and lastname)?

    Ahmed
    Regards

  • Participant
    Level: Enlightened
    Posts: 26
    Ahmed on #33041

    Hi,

    i found the solution, maybe that help someone.

    I create a function to catch non alpha characteres

    
    /**
     * Verifie si une chaine est alphabetique
     *
     * @param $string
     *
     * @return bool
     */
    function is_alpha($string)
    {
        // On cherche tt les caractères autre que [A-z]
        preg_match("/([^A-Za-z\s])/", $string, $result);
    
        // si on trouve des caractère autre que A-z
        if ( ! empty($result)) {
            return false;
        }
    
        return true;
    }
    

    I create my custom error messages

    
    add_action('bp_signup_validate', function () {
            $bp                   = buddypress();
            $firstname_field_post = $_POST['yoostart_signup_firstname'];
            $lastname_field_post  = $_POST['yoostart_signup_lastname'];
    
            if ( ! is_alpha($firstname_field_post)) {
                $bp->signup->errors['yoostart_signup_firstname'] = 'Le prenom ne doit comporter que des caracteres alphabétique';
            }
    
            if ( ! is_alpha($lastname_field_post)) {
                $bp->signup->errors['yoostart_signup_lastname'] = 'Le nom ne doit comporter que des caracteres alphabétique';
            }
        }
    );
    

    and i show them on register-form.php

    
    <!-- CUSTOM AHMED -->
     <div class="yoostart_name_details">
         <?php
         if (buddypress()->signup->errors['yoostart_signup_lastname']) {
             echo buddypress()->signup->errors['yoostart_signup_lastname'];
            }
        ?>
         <div class="lastname_field">
            <input type="text" name="yoostart_signup_lastname" id="yoostart_signup_lastname" value="<?= get_input('yoostart_signup_lastname') ?>" placeholder="Nom" required>
        </div>
        <?php
        if (buddypress()->signup->errors['yoostart_signup_firstname']) {
            echo buddypress()->signup->errors['yoostart_signup_firstname'];
        }
        ?>
        <div class="firstname_field">
            <input type="text" name="yoostart_signup_firstname" id="yoostart_signup_firstname" value="<?= get_input('yoostart_signup_firstname') ?>" placeholder="Prénom" required>
        </div>
    </div>
    <!-- CUSTOM AHMED -->
    
  • Keymaster
    (BuddyDev Team)
    Posts: 21785
    Brajesh Singh on #33046

    Hi Ahmed,
    Thank you for sharing the solution.

    I am glad you resolved it.

    Regards
    Brajesh

You must be logged in to reply to this topic.

This topic is: resolved