Support » Developing with WordPress » Validating drop down field

  • Resolved pandglobal

    (@pandglobal)


    // this validation if the selected value in the drop down is among the array
        add_filter( 'gform_field_validation_19_2', 'custom_validationusd101', 10, 4 );
        function custom_validationusd101( $result, $value, $form, $field ) {
    	
         $choices = array(
    		    array( 'value' => 'MALAYSIA', 'text' => 'NIGERIA'),
    			array( 'value' => 'CHINA', 'text' => 'CHINA'),
    			array( 'value' => 'UNITED', 'text' => 'UNITED STATES'),
    			array( 'value' => 'LONDON', 'text' => 'LONDON'),
    			array( 'value' => 'SPAIN', 'text' => 'SPAIN'),
    			array( 'value' => 'ITALY', 'text' => 'ITALY'),
    			array( 'value' => 'TURKEY', 'text' => 'TURKEY'),
    			array( 'value' => 'ROMANIA', 'text' => 'ROMANIA'),
    		);
            
    		$mango = $choices;
    		$mango =  array( 'value' => 'CHINA' );
    	
        if (!in_array($value, $mango)) {
            $result['is_valid'] = false;
            $result['message'] = 'Sorry, your selection was not part of the options in the array!.';
        }
    	
        return $result;
        }

    i have updated the code, this seems to work if only china is selected from the drop down option, but my concept is that if any of the values in the array is selected, not just china, and i don’t want to be repeating all the values again one by one in the validation code, i want it to be as a single variable that i can use to show values of choices as listed in the array.

    • This topic was modified 1 week, 3 days ago by Jan Dembowski.
    • This topic was modified 1 week, 3 days ago by Steve Stern.
Viewing 10 replies - 1 through 10 (of 10 total)
  • Moderator Steve Stern

    (@sterndata)

    Support Team Volunteer

    What forms plugin are you using?

    Joy

    (@joyously)

    You set your variable twice:

    $mango = $choices;
    $mango =  array( 'value' => 'CHINA' );

    Am using gravity forms, but the problem is am not getting the $mango to represent each of the values declared in the array,so that i don’t need to retype those array again in my if()

    Moderator Steve Stern

    (@sterndata)

    Support Team Volunteer

    You’re using a commercial theme/plugin, so please use their official support channel. We feel they are best equipped to support their products.

    https://www.gravityforms.com/support/

    Commercial products are not supported in these forums.

    Thanks steve but this is not a plugin error or fix, is a simple array function help that i need, it can be as an ordinary html drop down field that i needs a server side validation of. So every persons creating a form that has drop down should know how to validate is values at the server ends, incase a user tries to manipulate the choices from the front end or client side.

    Please help out steve, i know you are a guru.
    Anyone developing with wordpress should consider validations seriously

    Joy

    (@joyously)

    I do this in my theme for sanitizing selects in the Customizer, and I can use the same array for displaying as validating. But the array uses a single array, with the key as the value that is used in the select, and the value is the text that is shown to the user (so it is translated). This is standard for Customizer, so I make a function that returns the choices (and it is filtered). Then a generic function can be used to sanitize several different selects. Here is code from my theme:

    function twenty8teen_featured_image_class_choices() {
    	return apply_filters( 'twenty8teen_featured_image_class_choices', array(
    		/* translators: Style adjectives */
    		'border-outset' => __('Outset border', 'twenty8teen'),
    		'round' => __( 'Rounded', 'twenty8teen' ),
    		'shadow' => __( 'Shadow', 'twenty8teen' ),
    		'sepia' => __( 'Sepia', 'twenty8teen' ),
    		'active-shadow' => __( 'Active shadow', 'twenty8teen' ),
    		'skew' => __( 'Skewed hover', 'twenty8teen' ),
    		'scale-up' => __( 'Scale up hover', 'twenty8teen' ),
    	) );
    }
    
    /**
     * Sanitize a choice from a select input.
     */
    function twenty8teen_sanitize_select( $input, $setting ) {
    	$control = $setting->manager->get_control( $setting->id );
    	$valid = $control->choices;
    	return array_key_exists( $input, $valid ) ? $input : $setting->default;
    }

    For you, it would be more like this:

        add_filter( 'gform_field_validation_19_2', 'custom_validationusd101', 10, 4 );
        function custom_validationusd101( $result, $value, $form, $field ) {
    	
         $choices = array( 'MALAYSIA', 'NIGERIA', 'CHINA', 'UNITED', 'LONDON', 'SPAIN', 'ITALY', 'TURKEY', 'ROMANIA', 
    		); // if these are in $form, then no need to spell them out here.
            
    
        if (!in_array($value, $choices)) {
            $result['is_valid'] = false;
            $result['message'] = 'Sorry, your selection was not part of the options in the array!.';
        }
    	
        return $result;
        }

    Thanks alot @joyously you rightly said the thing, if my arrays where the way you stated above then i won’t be having issues as it validates as $choices.
    But my bigger problem is that am using multiple array not a single array like array(‘China’, ‘London’, ‘Spain’, )

    If only i can get array(array()) into one single variable called $choice then my problems are over.
    I don’t know if using foreach() would help

    Joy

    (@joyously)

    `add_filter( ‘gform_field_validation_19_2’, ‘custom_validationusd22’, 10, 4 );
    function custom_validationusd22( $result, $value, $form, $field ) {

    $choices = array(
    array( ‘value’ => ‘MALAYSIA’, ‘text’ => ‘NIGERIA’),
    array( ‘value’ => ‘CHINA’, ‘text’ => ‘CHINA’),
    array( ‘value’ => ‘UNITED STATES’, ‘text’ => ‘UNITED STATES’),
    array( ‘value’ => ‘LONbDON’, ‘text’ => ‘LONDONdd’),
    array( ‘value’ => ‘SPAIN’, ‘text’ => ‘SPAIN’),
    array( ‘value’ => ‘ITAoLY’, ‘text’ => ‘ITALY’),
    array( ‘value’ => ‘TURKEY’, ‘text’ => ‘TURKEY’),
    array( ‘value’ => ‘ROMANIA’, ‘text’ => ‘ROMANIA’),
    );

    $baad = array_column($choices, ‘value’);

    if ( ! in_array( $value, $baad ) ) {
    $result[‘is_valid’] = false;
    $result[‘message’] = ‘Sorry, your selection was not part of the options in the array!.’;
    }
    return $result;
    }

    Thanks @joyously i was able to get something from the post link you sent to me and i ended up with the above code, it works perfectly for me.

    If you dont mind you can look at my second question in the link https://wordpress.org/support/topic/restrict-a-user-meta-value-to-be-unique-across-all-users/ and see if you can be of any help too.

    Thanks in a million

Viewing 10 replies - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.