I have a Joomla (version 3.9.4, important for the subform, but as a newby I cannot add create the Joomla3.9 tag) form with field with type set to 'list' and a subform with min and max, both set to '6'. The subform has 1 field type 'calendar'. So I see the list (with 3 options) and the subform with 6 datepicker items. Now what I want is to change the min and max based on the option of the list. How can I do that? In PHP? Or in Javascript?
I tried looking for the min/max values in the DOM using Javascript/jQuery to find data-minimum and data-maximum, but then the items have already been generated, so that's too late.
Here's the form.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<form>
<fieldset>
<field name="type" type="list" default="" label="type" >
<option value="0">Type A</option>
<option value="1">Type B</option>
<option value="2">Type C</option>
</field>
<field
name="dates"
type="subform"
required="true"
label="Dates"
multiple="true"
min="6" <= This is what I want to change based on what was selected in the list
max="6" <= This is what I want to change based on what was selected in the list
>
<form>
<field
name="date"
type="calendar"
label="Date"
default=""
/>
</form>
</field>
</fieldset>
</form>
And here's the php part to render it:
<?php
defined('_JEXEC') or die('Restricted access');
JHtml::_('behavior.tooltip');
JHtml::_('behavior.formvalidation');
JHtml::_('formbehavior.chosen', 'select');
?>
<form action="<?php echo JRoute::_('index.php?option=com_mycom&view=types&layout=edit&id='.(int) $this->item->id); ?>" method="post" name="adminForm" id="edit-form" class="form-validate" enctype="multipart/form-data">
<fieldset>
<?php echo $this->form->getField('type')->renderField(); ?>
<?php echo $this->form->getField('dates')->renderField(); ?>
</fieldset>
</form>
I'm investigating 2 paths: 1. Subclass the subform adding controls to change min/max. Not sure if this can work as the user changing the type happens after php has generated the form. So this can only work if I then also reload after every change to the type. Don't like that, and breaks the default Joomla form behaviour. 2. Add Javascript code to then show the appropriate number of datepickers based on the type option selected. Something similar to, or extension of, the showon behaviour. Not there yet. Any pointers/help is much appreciated.
There is no proper way in Joomla to do this. For users to add or remove date pickers don't set the min/max values, let them create as many as they need to manually, as by default provided in Joomla subforms