• Resolved vivalis

    (@vivalis)


    Dear Forminator team,

    First, I’d like to offer you my appreciation for your amazing form plug-in and your great support.

    My problem: I have a form with several select fields that get their options dynamically from the back-end via AJAX callback function. Unfortunately, Forminator performs a back-end validation to check whether the selected options in the select fields are actually configured in the fields’ settings. As these checks fail, nothing is stored in the database for these fields.

    My current workaround: To allow the storage of the select fields in question, I have disabled the code in ‘if ( ! value_exists )’ in the function ‘validate’ in library/fields/select.php.

    My question: Is there a hook or another, better solution to disable the back-end validation of select fields? A solution that is “update-proof” (i.e. that still works after a plug-in update)? If not, would it make sense to have a ‘Disable back-end validation’ option in the settings of select fields? I guess I’m not the only one who needs to dynamically fill dropdown content from the back-end… 😉

    Kind regards, Roger

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support Saurabh – WPMU DEV Support

    (@wpmudev-support7)

    Hi @vivalis

    Hope you’re doing well today! Thank you for your kind words really appreciate it.

    I have pinged our SLS team so that they can review your query and see what can be possible in this case. We will post an update here as soon as more information is available.

    Kind Regards,
    Saurabh

    Plugin Support Saurabh – WPMU DEV Support

    (@wpmudev-support7)

    Hello @vivalis

    Hope you’re doing well today! Apologies for the delay here.

    The SLS team was able to check more about this and indeed we validate the select fields like you mentioned.

    A better workaround for this can be by using Forminator_API::update_form_field to add values to the select field too.

    For example, if you wish to list down the existing posts on the select field, you can dynamically populate the posts as options in the select field dropdown using the below snippet.

    <?php
    add_filter(
    	'forminator_cform_render_fields',
    	function( $wrappers, $model_id ) {
    		if( $model_id != 60 ){ // update your form ID here.
    			return $wrappers;
    		}
    
    		$select_fields_data = array(
    			'select-1' => 'post',
    		);
    
    		foreach ( $wrappers as $wrapper_key => $wrapper ) {
    			if ( ! isset( $wrapper[ 'fields' ] ) ) {
    				continue;
    			}
    
    			if ( 
    				isset( $select_fields_data[ $wrapper[ 'fields' ][ 0 ][ 'element_id' ] ] ) &&
    				! empty( $select_fields_data[ $wrapper[ 'fields' ][ 0 ][ 'element_id' ] ] )
    			) {
    				$posts = get_posts( array( 'post_type' => $select_fields_data[ $wrapper[ 'fields' ][ 0 ][ 'element_id' ] ] ) );
    
    				if ( ! empty( $posts ) ) {
    					$new_options = array();
    					$opt_data = array();
    					foreach( $posts as $post ) {
    						$new_options[] = array(
    							'label' => $post->post_title,
    							'value' => $post->post_title,
    							'limit' => '',
    							'key'   => forminator_unique_key(),
    						);
    						$opt_data['options'] = $new_options;
    					}
    					$select_field = Forminator_API::get_form_field( $model_id, $wrapper['fields'][0]['element_id'], true );
    					if( $select_field ){
    						if( $select_field['options'][ 0 ]['label'] != $opt_data['options'][ 0 ]['label'] ){
    							Forminator_API::update_form_field( $model_id, $wrapper['fields'][0]['element_id'], $opt_data );
    							$wrappers[ $wrapper_key ][ 'fields' ][ 0 ][ 'options' ] = $new_options;
    						}
    					}
    				}
    			}
    		}
    
    		return $wrappers;
    	},
    	10,
    	2
    );

    Note: This is just an example snippet which I have shared so that you can use it as a reference on how to dynamically populate the select fields so that you don’t have to disable the validation entirely.

    Hope this helps.

    Kind Regards,
    Saurabh

    Plugin Support Amin – WPMU DEV Support

    (@wpmudev-support2)

    Hi @vivalis ,

    I assume the code worked, as there has been no communication from you for a week now, so I will resolve this topic.

    kind regards,
    Kasia

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

The topic ‘Disable back-end validation of select fields’ is closed to new replies.