[Forminator Pro] LIMIT FIELD SUBMISSIONS

Hello, I wanted to know if it was possible to Limit a field submission.
For exemple: i want to have some serial numbers in a select field and when one of this serial number selected and the form submitted, this serial number disappear in the select list.

  • Adam Czajczyk
    • Support Gorilla

    Hello Arthur De Frayssinet

    I hope you're well today and thank you for your question!

    Just to make sure that we're on the same side here, an example scenario could be like this:

    - there's a drop-down list (single choice) on a form
    - that list contains a number of options (serial numbers)
    - once the one option is selected and form submitted, that selected and submitted option is fully removed from the selection so nobody else can submit it.

    That wouldn't be currently possible "out of the box" but I think it might be doable with some additional code based on Forminator API. However, to make sure about it I've forwarded this question to our developers to consult it with them.

    Please keep an eye on this ticket and we'll update you here as soon as possible.

    Best regards,
    Adam

  • Konstantinos Xenos
    • Rubber Duck Debugger

    Hi Arthur De Frayssinet !

    I've made a mu-plugin to help you out with this. You can see a preview of the code at the end of this reply.

    First Steps:
    1] Always make sure to keep a backup of your site before changing/adding custom code.
    2] All of your select Options must have a value as well.
    3] The form entries must be kept in the database as well so we can read what has already been submitted.

    How to install:

    1] Navigate to your /wp-content/ directory and create a new one named mu-plugins if it doesn't exist.
    2] Download the attached .zip and extract it. You will find a file named forminator-remove-option-from-select.php
    3] Upload the file from the .zip into the mu-plugins directory.
    4] The final path should look like /wp-content/mu-plugins/forminator-remove-option-from-select.php
    5] Everything should work after that automatically.

    Any option from a <select> input that has already been submitted should not be visible after this point.

    Tell me if this worked for you and if you need any further adjustments !

    Regards,
    Konstantinos

    Code preview:

    <?php
    
    /**
     * Plugin Name: Forminator - Remove option from select after submit.
     * Plugin URI: https://premium.wpmudev.org/
     * Description: mu-plugin for removing an option from the <select> after submission.
     * Version: 1.0.0
     * Author: Konstantinos Xenos @ WPMUDEV
     * Author URI: https://premium.wpmudev.org/
     * License: GPLv2 or later
     */
    
    if ( ! defined( 'ABSPATH' ) ) {
    	exit;
    }
    
    if ( ! class_exists( 'WPMUDEV_Forminator_Remove_Option_From_Select' ) ) {
    
    	/**
    	 * WPMUDEV_Forminator_Remove_Option_From_Select Class
    	 */
    	class WPMUDEV_Forminator_Remove_Option_From_Select {
    		/**
    		 * Constructor.
    		 */
    		public function __construct() {
    			add_filter( 'forminator_field_markup', array( $this, 'my_remove_selected_option_from_select' ), 15, 3 );
    		}
    
    		/**
    		 * Change the body of the mail
    		 *
    		 * @param string $html The html of the input.
    		 * @param array  $field The field data.
    		 * @param object $instance The form instance.
    		 *
    		 * @return string $html The new html of the field.
    		 */
    		public function my_remove_selected_option_from_select( $html, $field, $instance ) {
    
    			if ( ! empty( $field['use-custom-class'] ) && ! empty( $field['custom-class'] ) && 'my-codes' === $field['custom-class'] ) {
    				if ( ! empty( $field['required'] ) && true === $field['required'] ) {
    					$required = 'true';
    				} else {
    					$required = '';
    				}
    
    				$html = '<div class="forminator-field--label">
    							<label class="forminator-label" id="forminator-label-' . $field['element_id'] . '">' . $field['field_label'] . '</label>
    						</div>';
    
    				$html .= '<select class="forminator-select--field forminator-select" id="' . $field['element_id'] . '-field" data-required="' . $required . '" name="' . $field['element_id'] . '">';
    
    				$entries = Forminator_API::get_entries( $instance->model->id );
    
    				foreach ( $entries as $key => $entry ) {
    
    					$entry = (array) $entry;
    
    					foreach ( $field['options'] as $key => $value ) {
    						if ( in_array( $entry['meta_data'][ $field['element_id'] ]['value'], $value, true ) ) {
    							unset( $field['options'][ $key ] );
    						}
    					}
    				}
    
    				foreach ( $field['options'] as $option ) {
    					$html .= '<option value="' . $option['value'] . '">' . $option['label'] . '</option>';
    				}
    
    				$html .= '</select>';
    			}
    
    			return $html;
    		}
    	}
    
    	new WPMUDEV_Forminator_Remove_Option_From_Select();
    }

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.