Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Making a field Mandatory if selection is made

Avatar

Level 5

Good Day.

I am trying to figure out how to mark a field mandatory if a user selects an option.

What I have.

I have been designing a 3 page form. On the first page there are a number of options a user can select by selecting 1 or all 4 of the check boxes.

If a users selects a check box beside 2 specific (out of the many) options I have a text box that is required to be used for justification.

What I would like to do is;

If a user selects a check boxes beside 1 or both of the 2 specific options, I would like the Text Box to become a mandatory field.

With any luck I have explained this correctly.

Regards

Chomp

1 Accepted Solution

Avatar

Correct answer by
Level 2

Also make sure the Language dropdown is set to JavaScript and not FormCalc

View solution in original post

9 Replies

Avatar

Level 2

Add the following script in the Change event for your particular checkbox and change TextField to the appropriate field that you want to become mandatory:

if

(this.rawValue == "true")

{

TextField.mandatory

= "error";

}

else

{

TextField.mandatory

= "disabled";

}

Avatar

Level 5

Thanks Mike;

I must be doing something wrong. I can not seem to get this puppy to work. This is where I placed the code. I have it colored in gray. This might have something to do with it? If you would not mind, would you let me know wjere I screwed up?

<subform h="10.5in" w="8in">

         <field h="6mm" name="CheckBox1" w="28mm" x="47.625mm" y="12.7mm">

            <ui>

               <checkButton>

                  <border>

                     <?templateDesigner StyleID apcb2?>

                     <edge stroke="lowered"/>

                     <fill/>

                  </border>

               </checkButton>

            </ui>

            <font typeface="Myriad Pro"/>

            <margin leftInset="1mm" rightInset="1mm"/>

            <para vAlign="middle"/>

            <value>

               <integer>0</integer>

            </value>

            If this.rawValue="true"){TextField1.mandatory="error";}

            else

            {TextField1.mandatory="disabled";}

            <caption placement="right" reserve="21.1875mm">

               <para vAlign="middle"/>

               <font typeface="Myriad Pro"/>

               <value>

                  <text>Check Box</text>

               </value></caption>

            <items>

               <integer>1</integer>

               <integer>0</integer>

               <integer>2</integer>

            </items>

         </field>

Avatar

Former Community Member

You shoudl not be adding code to the XML structure (the product will do that for you). You can click on the

object that you want to add the code to then in the script editor choose the event that you want to

code against and add the code there. If the script editor is not open, you can toggle it on in the Window menu.

Paul

Avatar

Level 2

As Paul stated this code belongs in the Script Editor, not in the XML. In LiveCycle Designer go to the Window menu and make sure Script Editor is checked and you are able to see it. It most likely will show up across the top. In the Design View of your form click on the checkbox object you want the script associated to and then in the Script Editor there will be a dropdown menu for all the events for that object. Select "change" in the dropdown for the Change event and add the code there.

Also if your checkbox values are 1 and 0 for On/Off value instead of true and false then change the if statement check to be

if (this.rawValue

== 1)

Avatar

Level 5

Thank you Mike

I added the code (using the script editor) to the Checkbox under change action.

When I clicked on the check box I received the following error

Script failed (languages formCalc; context is xfa[0].form1[0]. #subform[0]. Checkbox1[0] script= if this.RawValue = =”1”) {text field1.mandatory=”error”

Else

{textfield1.manatory=”disabled”}

Error: syntax error near token ‘This’ on line 1 column 7.

(I have tried it with a space between the = signs and without a space and get the same error)

Any ideas

Chomp

Avatar

Level 2

Your syntax looks incorrect. It looks like you are missing the openning parenthesis '('  in the if statement and 1 should not be in quotes. This is an interger value and not a string value like "true". Also the objects are case sensitive so if your object name is TextField1 the script should reflect the same.

if (this.rawValue == 1)

Avatar

Correct answer by
Level 2

Also make sure the Language dropdown is set to JavaScript and not FormCalc

Avatar

Level 5

Good Day;

Ok, this code seems to run without any errors. Does not seem to work. It appears to be the logic?? Let me explain.

I set up a new form with 1 checkbox (labelled checkbox1) and 2 text fields (labelled TextField1 & textField2). I added the following java script to the “change” action for the checkbox

If (this.rawValue == 1)

{TextField1.mandatory  = "error";}

else

{TextField1.mandatory = "disabled";}

The value of the checkbox is 0 = off, 1 = on

Language = java script

When I bring the form up using Acrobat Reader 9, click the checkbox, TextField1 does not become mandatory. It allows me to save it without adding any text.

The xml code for the textbox when I manually set it to “required” looks like this

<text>Text Field1</text>

               </value>

            </caption>

            <validate nullTest="error"/>

         </field>

Does it make a difference if this form is save as; static, dynamic, Adobe 7 or Adobe 8

Any ideas

Chomp

Avatar

Level 5

Ok....This must be my dumb week....

Thanks for all your help. It is working like a charm.

regards

Chomp