Forminator post field required option
-
I have a Forminator form to insert posts. I would like to set some inputs of the post field as required. In Forminator I just can set the whole post field as required. Is there a simple way to change this?
I want titel, content and categories as required but the featured image as optional.
-
Hi @kiterfred,
I’m afraid, at the moment the feature to force required is only possible for the whole “Post Data” field.
I’m checking with our developer to see if there is any workaround that could be suggested.
Will keep you posted once we get further feedback asap.
Best Regards,
NithinHi @kiterfred,
Could you please check and let us know whether the following snippet helps as a workaround?
<?php add_action( 'wp_footer', 'wpmudev_required_check_validate', 9999 ); function wpmudev_required_check_validate(){ global $post; if ( is_a( $post, 'WP_Post' ) && !has_shortcode($post->post_content, 'forminator_form') ) { return; } ?> <script type="text/javascript"> jQuery( document ).ready( function($){ $.validator.addMethod("required_post", function (value, element) { if( value == '' ){ return false; } return true; },"This field is required."); $( "#forminator-module-2910 select[name='postdata-1-category']" ).attr( "required_post", "required_post" ); $( "#forminator-module-2910 input[name='postdata-1-post-title']" ).attr( "required_post", "required_post" ); $( "#forminator-module-2910 textarea[name='postdata-1-post-content']" ).attr( "required_post", "required_post" ); }); </script> <?php }You’ll need to update the line having “2910” in the above code with your form ID.
The above snippet can be added as a mu-plugins. Please check this link on how to implement the above code as a mu-plugins:
https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-pluginsBest Regards,
Nithin
Hello @kiterfred ,
We haven’t heard from you for a week now, so it looks like the proposed workaround worked for you.
Feel free to re-open this ticket if needed.
Kind regards
KasiaHey thanks for your fast reply. Hope you have a merry christmas.
I was able to test ist, but your code snippet didn’t solve the problem. I updated the 3 lines with my form id and put the code in a file in the mu-plugin folder. did i miss something?
Hi @kiterfred,
The snippet is more of a workaround and you’ll also need to set the “Post Data” field to “Optional” and make sure you have the “Load form using AJAX” option enabled under the “Behavior” tab.
Screenshot:
Screenshot at 12:37:55.png
Screenshot at 12:38:13.png
Please do let us know how that goes. Happy Holidays!
Best Regards,
Nithin
Hi Nithin,
i tried the workaround as described but it didn’t solve my issue. When loading the form with ajax i get new issues i have to solve which aren’t part of my real problem.
Is it possible to set a standard image via code, when a person didn’t set one? This would also solve my issue.
Thank you for your help
Hi @kiterfred,
I hope you are doing well today!
Is it possible to set a standard image via code, when a person didn’t set one? This would also solve my issue.
This issue has been flagged to our SLS (Second Line Support) Team so that they can dig into this further. We will post an update here as soon as more information is available.
Thank you for your patience while we look into this further.
Kind regards,
ZaferHi again @kiterfred,
Your request is not possible as Forminator won’t let you submit the form if any required field isn’t set and we can’t set a default value for input type file.
Please share an export of the form with us, so that we can check further on our lab.
You can find more info on how to export the form here : https://wpmudev.com/docs/wpmu-dev-plugins/forminator/#import-export
After exporting, please share the code using a service such as https://pastebin.com which is free to use.
Please always make sure to use such service to share the code and don’t post the code here directly as it will most likely be unusable.
Kind regards,
ZaferHi Zafer,
here is the export of my form:
https://pastebin.com/R9cUutaN
Kind regards
KiterfredHi @kiterfred,
Thanks for the form export. I’m escalating this further to our developer’s attention and will get back to you once we get further feedback asap.
Kind Regards,
Nithin
Hi
just wanted to let you know that i found a solution for my case.
Before I started this topic I also tried to use the upload field in combination with ACF but this didn’t work. So after another search i found someone having this issue and got a solution. I changed a line and got my solution. I didn’t find the website where I found the code but here is my solution using the following code as mu-plugin:<?php /** * Plugin Name: [Forminator] Upload mapping to ACF image field * Description: Solves a bug that prevents upload field mapping to ACF image field through post data form * Author: Prashant Singh @ WPMUDEV * Author URI: https://premium.wpmudev.org * License: GPLv2 or later */ if ( ! defined( 'ABSPATH' ) ) { exit; } if ( defined( 'WP_CLI' ) && WP_CLI ) { return; } add_filter( 'forminator_custom_form_submit_errors', 'wpmudev_map_uplods_acf_post_test', 10, 3 ); function wpmudev_map_uplods_acf_post_test( $submit_errors, $form_id, $field_data_array ) { $fform_id = 1547 ; //Please change form ID to your form ID if( $form_id != $fform_id ){ return $submit_errors; } $submitted_data = Forminator_CForm_Front_Action::$prepared_data; if( isset( $submitted_data['postdata-1'] ) && is_array( $submitted_data['postdata-1'] ) ) { if( isset( $submitted_data['postdata-1']['post-custom'] ) && is_array( $submitted_data['postdata-1']['post-custom'] ) ) { foreach( $submitted_data['postdata-1']['post-custom'] as $post_key => $post_val ) { if( strpos( $post_val['value'], '{upload' ) !== false ) { $field_name = str_replace('{', '', $post_val['value'] ); $field_name = str_replace('}', '', $field_name ); if ( is_array( $submitted_data[ $field_name ] ) && isset( $submitted_data[ $field_name ]['file'] ) ) { if ( isset( $submitted_data[ $field_name ]['file']['file_url'] ) ) { Forminator_CForm_Front_Action::$prepared_data['postdata-1']['post-custom'][$post_key]['value'] = $submitted_data[ $field_name ]['file']['file_url']; } } } } } } return $submit_errors; } add_action( 'forminator_post_data_field_post_saved', 'wpmudev_update_post_acf_uploads_test', 10, 4 ); function wpmudev_update_post_acf_uploads_test( $post_id, $field, $data, $cls ){ $submitted_data = Forminator_CForm_Front_Action::$prepared_data; $form_id = 1547; //Please change form ID to your form ID if ( $submitted_data['form_id'] != $form_id ) { return; } if( isset( $submitted_data['postdata-1'] ) && is_array( $submitted_data['postdata-1'] ) ) { if( isset( $submitted_data['postdata-1']['post-custom'] ) && is_array( $submitted_data['postdata-1']['post-custom'] ) ) { foreach( $submitted_data['postdata-1']['post-custom'] as $post_key => $post_val ) { if ( $post_val['key'] == 'upload-1' ) { //Please change upload-1 to your custom field name if ( isset( $post_val['value'] ) ) { $attach_id = attachment_url_to_postid( $post_val['value'] ); if( $attach_id ) { update_field($post_val['key'], $attach_id, $post_id); $mime_type = wp_get_image_mime( $post_val['value'] ); if( $mime_type ) { $update_data = array( 'ID' => $attach_id, 'post_mime_type' => $mime_type, ); set_post_thumbnail( $post_id, $attach_id ); //this sets the uploaded image as featured image of the post wp_update_post( $update_data ); } } } } } } } }
The topic ‘Forminator post field required option’ is closed to new replies.