Yes this is possible but is a major work around. Joomla has two required verification events. One in he browser and the other on the server just before saving of the form. The browser is the easy part to bridge, the server-side verification is more tricky.
I add an extra hidden field called "not_required" to my form and use this to send the fields not required to the server. Then in the JModelAdmin extended class for this view I rewrite the validate method to take the values in the "not_required" field in account on validation.
Well enough said, here is the code to bridge the browser validation:
jform_LATkbJUPdS_required = false;
// the LATkbJU function
function LATkbJU(add_css_view_LATkbJU)
{
// set the function logic
if (add_css_view_LATkbJU == 1)
{
jQuery('#jform_css_view').closest('.control-group').show();
if (jform_LATkbJUPdS_required)
{
updateFieldRequired('css_view',0);
jQuery('#jform_css_view').prop('required','required');
jQuery('#jform_css_view').attr('aria-required',true);
jQuery('#jform_css_view').addClass('required');
jform_LATkbJUPdS_required = false;
}
}
else
{
jQuery('#jform_css_view').closest('.control-group').hide();
if (!jform_LATkbJUPdS_required)
{
updateFieldRequired('css_view',1);
jQuery('#jform_css_view').removeAttr('required');
jQuery('#jform_css_view').removeAttr('aria-required');
jQuery('#jform_css_view').removeClass('required');
jform_LATkbJUPdS_required = true;
}
}
}
Sorry for the generic naming but the above should give you a good idea how to update a field so the browser verification is disarmed.
You will see there is a function call "updateFieldRequired('css_view',0);"
This is used to set the hidden field that helps on the server side of this validation issue. Here is the code for this function.
// update required fields
function updateFieldRequired(name,status)
{
var not_required = jQuery('#jform_not_required').val();
if(status == 1)
{
if (isSet(not_required) && not_required != 0)
{
not_required = not_required+','+name;
}
else
{
not_required = ','+name;
}
}
else
{
if (isSet(not_required) && not_required != 0)
{
not_required = not_required.replace(','+name,'');
}
}
jQuery('#jform_not_required').val(not_required);
}
Here is the new validate method for the model
protected function checkString($string)
{
if (isset($string) && is_string($string) && strlen($string) > 0)
{
return true;
}
return false;
}
public function validate($form, $data, $group = null)
{
// check if the not_required field is set
if ($this->checkString($data['not_required']))
{
$requiredFields = (array) explode(',',(string) $data['not_required']);
$requiredFields = array_unique($requiredFields);
// now change the required field attributes value
foreach ($requiredFields as $requiredField)
{
// make sure there is a string value
if ($this->checkString($requiredField))
{
// change to false
$form->setFieldAttribute($requiredField, 'required', 'false');
// also clear the data set
$data[$requiredField] = '';
}
}
}
return parent::validate($form, $data, $group);
}
Here I have a demo component on GitHub that has a field dependent implementation take a look also at the model validate method and other scripts related.
Hope this is what you are looking for, until next time take care!
showon
as it's only to show/hide fields. You will need to write a custom form field for this and change therequired
attribute using jQuery (or vanilla JS)