• Resolved gabrieldivji

    (@gabrieldivji)


    Hello, im using this code (provided by UM here https://docs.ultimatemember.com/article/94-apply-custom-validation-to-a-field) to allow users to register only if they have a unique key (TEST_VALUE in this example):

    /**
    * Validate field T2N_Code
    * @param string $key
    * @param attay $array
    * @param array $args
    */
    function um_custom_validate_username( $key, $array, $args ) {
    if ( isset( $args[$key] ) && $args[$key] !== ‘TEST_VALUE’ ) {
    UM()->form()->add_error( $key, __( ‘Please enter valid code.’, ‘ultimate-member’ ) );
    }
    }
    add_action( ‘um_custom_field_validation_T2N_Code’, ‘um_custom_validate_username’, 30, 3 );

    Is it possible to have several keys accepted and not only one? How would that code look like?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @gabrieldivji

    Yes, you can try the following sample code:

    function um_custom_validate_username( $key, $array, $args ) {
        // Validate T2N Code
       if ( isset( $args["T2N_Code"] ) && $args["T2N_Code"] !== ‘TEST_VALUE’ ) {
        UM()->form()->add_error( $key, __( ‘Please enter valid code.’, ‘ultimate-member’ ) );   
      }
       // Validate Gender
      if ( isset( $args["gender"] ) && $args["gender"] !== ‘male’ ) {
        UM()->form()->add_error( $key, __( ‘You are not a Male.’, ‘ultimate-member’ ) );   
      }
    }
    add_action( ‘um_custom_field_validation_T2N_Code’, ‘um_custom_validate_username’, 30, 3 );

    Please feel free to re-open this thread if there’s any question that may come up.

    Regards,

    Hi @champsupertramp

    in this function:

    function um_custom_validate_username( $key, $array, $args ) {
    // Validate T2N Code
    if ( isset( $args[“T2N_Code”] ) && $args[“T2N_Code”] !== ‘TEST_VALUE’ ) {
    UM()->form()->add_error( $key, __( ‘Please enter valid code.’, ‘ultimate-member’ ) );
    }

    How can i add severals validate values ?

    Regards,

    • This reply was modified 4 years, 10 months ago by cyrfan.
    Thread Starter gabrieldivji

    (@gabrieldivji)

    My question was actually the same as @cyrfan made, I want the same field to accept several values. How can this be done?

    Thank you

    hi @gabrieldivji
    With $regcode you can add multiple value like in this example with 5 values (You can add more if you want) :

    function um_custom_validate_username( $key, $array, $args ) {
    global $regcode;
    $regcode = $args[$key];
    if ( isset( $regcode ) && $regcode !==’value1′ && $regcode !==’value2′ && $regcode !==’value3′ && $regcode !==’value4′ && $regcode !==’value5′ ) {
    UM()->form()->add_error( $key, __( ‘incorrect value.’, ‘ultimate-member’ ) );
    }
    }

    add_action( ‘um_custom_field_validation_T2N_Code’, ‘um_custom_validate_username’, 30, 3 );

    • This reply was modified 4 years, 10 months ago by cyrfan.
    Thread Starter gabrieldivji

    (@gabrieldivji)

    It works! Thank you so much @cyrfan for your super fast answer and the solution!

    @gabrieldivji your welcome!

    UPDATE:

    This code is really better than i wrote before.
    In this example you can get 91 different correct codes (ABC109DEF, ABC110DEF etc etc…):

    function um_custom_validate_username($key, $array, $args)
    {
    global $regcode;
    $regcode = $args[$key];
    $valid_codes = [];
    for ($i = 109; $i <= 200; $i++) {
    array_push($valid_codes, ‘ABC’ . $i . ‘DEF’);
    }
    if(isset($regcode) && !in_array($regcode, $valid_codes)) {
    UM()->form()->add_error($key, __(‘Code non valide.’, ‘ultimate-member’));
    }
    }

    add_action(‘um_custom_field_validation_codecle’, ‘um_custom_validate_username’, 30, 3);

    • This reply was modified 4 years, 9 months ago by cyrfan.
    • This reply was modified 4 years, 9 months ago by cyrfan.
Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Several custom validations to a field’ is closed to new replies.