Join 34,000+ subscribers and receive articles from our blog about software quality, testing, QA and security.
 

Conditional required field based on status


#1

Hi,

I have a custom field called ‘Devices’ for test case results that I have made required through script (as opposed to the checkbox in when creating the new field). I would like to make it so that the field is only required when the status is NOT set to Pass or Blocked… so that when the user clicks Add Test Result in the dialog, it will only prevent the result from being added if the result is not Pass or Blocked. Here is what I have set up so far:

name: Conditional Required
description: Makes the Devices Field required if the case does not pass
author:
version: 1.0
includes:
excludes:

js:
(document).ready( function() { .subscribe(‘result_dialog.loaded’, ‘uiscript_conditional_required’, function(o)
{
$(’#custom_devices’).prev().append(’ *’);
});

    $.subscribe('result_dialog.validate', 'uiscript_conditional_required', function(o)
    {
        if ($.trim(o.values.custom_devices) == '')
        {
            $('#addResultErrors').append('Field Devices is a required field.');
            $('#addResultErrors').show();
            o.event.stopPropagation();
            return;
        }
    });
}

);

Thanks


#2

Hello Robert,

Thanks for your posting. I already answered this via email but here’s the modified UI script again:

[code]name: Conditional Required
description: Makes the Devices Field required if the case does not pass
author:
version: 1.0
includes:
excludes:

js:
(document).ready( function() { .subscribe(‘result_dialog.loaded’, ‘uiscript_conditional_required’, function(o)
{
$(’#custom_devices’).prev().append(’ *’);
});

$.subscribe('result_dialog.validate', 'uiscript_conditional_required', function(o)
{
    if (o.values.status_id == 1)
    {
        return;
    }

    if ($.trim(o.values.custom_devices) == '')
    {
        $('#addResultErrors').append('Field Devices is a required field.');
        $('#addResultErrors').show();
        o.event.stopPropagation();
        return;
    }
});

});[/code]

The trick is to add an additional if-clause to the validation handler, e.g.:

if (o.values.status_id == 1) { return; }

I hope this helps!

Regards,
Tobias