1

I would like to solve this problem:

I got some Fields in my Form. Some are Combo-Boxes, Radio Buttons, Text-Inputs and so on.

Now I would like to do the following: If Radio-Button 1 has value 1, text-input shall appear (works with showon) and if it appears it should be required.

If it is not shown it should not be required.

Is it possible to do that?

2

1 Answer 1

3

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.