Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save wpmudev-sls/71c4ff21d039301b746b1d56ebd87ada to your computer and use it in GitHub Desktop.
Save wpmudev-sls/71c4ff21d039301b746b1d56ebd87ada to your computer and use it in GitHub Desktop.
[Forminator Pro] - Multiselect minimum and maximum selection.
<?php
/**
* Plugin Name: [Forminator Pro] Allow maximum and minimum slection.
* Description: Removes default/empty option and adds maximum and minimum selection limit in modern multiselects.
* Author: Prashant @ WPMUDEV
* Task: SLS-6254
* Author URI: https://premium.wpmudev.org
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
add_action( 'forminator_before_form_render', 'wpmudev_select_data_form_check', 10, 5 );
function wpmudev_select_data_form_check( $id, $form_type, $post_id, $form_fields, $form_settings ) {
$form_ids = array( 2960 ); // Please change the form ID.
if ( in_array( intval( $id ), $form_ids, true ) ) {
add_filter( 'forminator_field_single_markup', 'wpmudev_unset_default_selection_unchecked', 10, 4 );
}
}
// Removes default selection and adds minimum selection limit.
function wpmudev_unset_default_selection_unchecked( $html, $id, $required, $options ) {
$field_ids = array(
'select-1' => 2,
'select-2' => 3,
); // Please match the field IDs and min selection.
foreach ( $field_ids as $key => $value ) {
if ( false !== strpos( $html, '<select multiple' ) && $required && false !== strpos( $id, 'field--' . $key ) ) {
$html = str_replace( '<select multiple', '<select multiple minlength="' . $value . '"', $html );
$html = str_replace( 'value="" selected="selected"', 'value=""', $html );
}
}
return $html;
}
// Adds maximum selection limit and defines error messages.
add_action( 'wp_footer', 'wpmudev_select_selection_min_max', 9999 );
function wpmudev_select_selection_min_max() {
global $post;
if ( is_a( $post, 'WP_Post' ) && ! has_shortcode( $post->post_content, 'forminator_form' ) ) {
return;
}
?>
<script type="text/javascript">
jQuery(document).ready(function($){
setTimeout(function() {
$('.forminator-custom-form').trigger('after.load.forminator');
},500);
$(document).on('after.load.forminator', function(e, form_id) {
if ( 'forminator-module-2960' === e.target.id ) { // Please change the form ID.
$.validator.addMethod("maxselection", function (value, element) {
if ( $.isArray( value ) && value.length > $(element).attr('data-max') ) {
return false;
}
return true;
});
var select_max = {'select-1': 3, 'select-2': 4 }; // Please match the field IDs and max selection.
$.each(select_max, function(index, item) {
$('#' + e.target.id + ' #' + index + ' select').attr('data-max', item);
var minlength = $('#' + e.target.id + ' #' + index + ' select').attr('minlength');
$('#' + e.target.id + ' #' + index + ' select').rules("add", {
'maxselection': true,
messages: {
minlength: 'Please select at least ' + minlength + ' options.',
maxselection: 'Maximum ' + item + ' options allowed.'
}
});
});
}
});
});
</script>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment