• Resolved sukaanda

    (@sukaanda)


    Hi Dev,

    I have problem after trying to submit my form, I think it might be related to select option that I modified with this code https://gist.github.com/patrickfreitasdev/6196a5fd4cdebdfa5831868edcbed6a1

    I want to make option from my post titles for my select input.. but after I add the codes it raise an error if I submit the form

    this is my form export https://drive.google.com/file/d/1NKwBTPRt4wRJMabrno2ZtaG6ciX-NyxP/view?usp=drive_link

    add_filter(
    	'forminator_cform_render_fields',
    	function( $wrappers, $form_id ) {
    
    		$allowed_forms = array (
    			847,
    		);
    
    		if ( ! in_array( $form_id, $allowed_forms) ) {
    			return $wrappers;
    		}
    
    		$select_fields_data = array(
    			'select-1' => 'post',
    			//'select-2' => 'CPT_2',
    		);
    
    		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' ] ], 
    						'category_name' => 'career'
    					) 
    				);
    
    				if ( ! empty( $posts ) ) {
    					$new_options = array();
    
    					foreach( $posts as $post ) {
    						$new_options[] = array(
    							'label' => $post->post_title,
    							'value' => str_replace(' ', '-', $post->post_title),
    							'limit' => '',
    							'key'   => forminator_unique_key(),
    						);
    					}
    
    					$wrappers[ $wrapper_key ][ 'fields' ][ 0 ][ 'options' ] = $new_options;
    				}
    			}
    		}
    
    		return $wrappers;
    	},
    	10,
    	2
    );
    
    add_filter(
    	'forminator_replace_form_data', 
    	function( $content, $data, $fields ) {
    		
    		$allowed_forms = array (
    			847,
    		);
    
    		if ( ! in_array( $form_id, $allowed_forms) ) {
    			return $content;
    		}
    		
    		if ( ! empty( $content ) ) {
    			return $content;
    		}
    
    		$form_fields = Forminator_API::get_form_fields( $data['form_id'] );
    		$form_fields = Forminator_API::get_form_fields( 847 );
    		$data_field = '';
    		foreach($data as $key => $value){
    	    	if ( strpos( $key, 'select' ) !== false ) {
    	    		$values = '';
    	    		$field_value = isset( $data[ $key ] ) ? $data[ $key ] : null;
    
    		    	if ( ! is_null( $field_value ) ) {
    		    		$fields_slugs  = wp_list_pluck( $form_fields, 'slug' );
    					$field_key     = array_search( $key, $fields_slugs, true );
    					$field_options = false !== $field_key && ! empty( $form_fields[ $field_key ]->raw['options'] )
    							? wp_list_pluck( $form_fields[ $field_key ]->options, 'label', 'value' )
    							: array();
    
    					if ( ! isset( $field_options[ $field_value ] ) && isset( $_POST[ $key ] ) ) {
    						return sanitize_text_field( $_POST[ $key ] );
    					}
    				}
    			}
    		}
    	    return $content;
    	},
    	10,
    	3
    );
    
    

    that’s my codes

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support Patrick – WPMU DEV Support

    (@wpmudevsupport12)

    Hi @sukaanda

    I hope you are doing well.

    Can you please test the following version:

    <?php
    
    add_filter(
    	'forminator_cform_render_fields',
    	function( $wrappers, $model_id ) {
    		if( $model_id != 847 ){
    			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' ] ], 
    						'category_name' => 'career'
    					) 
    				);
    
    				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
    );
    
    add_filter(
    	'forminator_replace_form_data', 
    	function( $content, $data, $fields ) {
    		if( $data['form_id'] != 847 ){
    			return $content;
    		}
    
    		if ( ! empty( $content ) ) {
    			return $content;
    		}
    
    		$form_fields = Forminator_API::get_form_fields( $data['form_id'] );
    		$data_field = '';
    		foreach($data as $key => $value){
    	    	if ( strpos( $key, 'select' ) !== false ) {
    	    		$values = '';
    	    		$field_value = isset( $data[ $key ] ) ? $data[ $key ] : null;
    
    		    	if ( ! is_null( $field_value ) ) {
    		    		$fields_slugs  = wp_list_pluck( $form_fields, 'slug' );
    					$field_key     = array_search( $key, $fields_slugs, true );
    					$field_options = false !== $field_key && ! empty( $form_fields[ $field_key ]->raw['options'] )
    							? wp_list_pluck( $form_fields[ $field_key ]->options, 'label', 'value' )
    							: array();
    
    					if ( ! isset( $field_options[ $field_value ] ) && isset( $_POST[ $key ] ) ) {
    						return sanitize_text_field( $_POST[ $key ] );
    					}
    				}
    			}
    		}
    	    return $content;
    	},
    	10,
    	3
    );

    Best Regards
    Patrick Freitas

    Thread Starter sukaanda

    (@sukaanda)

    Hi Patrick @wpmudevsupport12

    thank you for responding me, hope you doing well too

    I tested your code and it works very well

    thank you for helping me, thank you very much

    best regards,

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Error: Your form is not valid after pull Post Titles into a select field’ is closed to new replies.