Custom Form Keyword for whether question is required

Hi all

 

Anyone know of any way in a custom form to use a keyword to check whether a question is required or not?

 

There's many keywords for other aspects of a question (name, label, notes, etc), but if I want to interrogate whether a question is required or not is proving tricky.  I thought I could use the 'is_required' attribute combined with an as_asset modifier (it was a long-shot at best, I know), but I can't seem to get/use the actual question id (since it's got a ':' in the middle, breaks out of keyword syntax).

 

At the moment I'm just manually updating the class in the bodycopy, but thought it would be nice to have it done auto-magically.

 

Matrix v4.12

 

Cheers

I think your best bet is to upgrade to 4.14.2 so that you can use the Accessible Form option which will do what you are wanting. 

For older Matrix versions you could also use the 'extras' field on the form question and add the HTML 5 attribute 'required', then use a keyword modifier to detect whether that is present in the resulting HTML tag and print something, e.g.

Required: "%question_field_7524_q1^contains:required:1:0%"

It's not a 100% neat solution, but it could get you closer to what you are trying to achieve.

 

Trying to detect output on some of the attributes of the form question asset is going to be problematic as they contain serialized php.

You could also use the extras field to give it a "required" class, then highlight that field, or use JS or CSS to add an indicator.

I can confirm Ive previously used the CSS approach that Nic mentions and it works fine for me.

Thanks everyone for the excellent suggestions.  I've tried adding 'required' to the extras field so I could try to use Anthony's suggestion as well as get the HTML5 required attribute in there.  Issue is the resultant output tries to give required an actual value - code examples might be better (non-essential parts stripped out):

<!-- what I got -->
<input type="text" required="" /> 

Which is unfortunately not valid HTML5 (is it?).  Therefore, I didn't pursue Anthony's suggestion (though I reckon it would have worked).  What I've settled on instead is setting required on a class on the containing div (we customise the label, rather than the input element itself, to indicate to the user the field is required).  Below is from the page bodycopy:

<!-- what I got -->
<input type="text" required="" /> 

I had planned to use something like (what Anthony suggested) %question_field_134860_q1^contains:required:required: % where I've manually written 'required' in the above code, then it would all be dynamic, but since it renders invalid I think I'll have to forgo that approach (happy to listen to views/thoughts on invalidity of the resultant code).

 

Think only option is Nic's suggestion to wait for us to upgrade to 4.14.2+.

 

Thanks again everyone.

Which is unfortunately not valid HTML5 (is it?). 

 

Either output is valid according to W3C standards, but I think you will find it's the browser manipuating the output of the tag rather than Matrix. You may have been inspecting with a DOM tool which will generally show the HTML as the browser interprets it. If you 'view source' you'll see the raw output without the attribute value appended.

 

It's possible to build valid accessible forms without using the semantic output from newer Matrix versions, but it does require a fair bit more effort as you'll have to manually code the resulting body content. If you are able to wait for the upgrade I think you'll find a fair bit of that time saved by using the semantic output (and it's also easier to style).

My apologies!!  Anthony is exactly correct, I was using Firebug to view the code.  If I do View Source the actual code sent by Matrix is exactly as you said it would be.  I feel appallingly mortified.

 

<rubs hands together>

 

Ok, I'll try implementing with some of the options indicated above.  I'll let you know how it goes.

 

Update:

 

Works perfectly as Anthony suggested.  I added simply 'required' to the extras field for the question.  Then I put this code in the bodycopy that generates the form:

%question_label_134860_q1% %question_field_134860_q1%

Generated source looks like this:

%question_label_134860_q1% %question_field_134860_q1%

Thanks again to everyone who commented and helped with this.  I consider it a win!

Thanks again to everyone who commented and helped with this.  I consider it a win!

 

No worries, glad to hear you got it sorted!