cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
FrankKelly84
Level: Powered On

Changing if a field is required

Hey guys,

 

I'm pretty new to PowerApps. I'm trying to make a textbox field required, but only if certain items are selected in a seperate combobox.

 

I have this in the OnChange method of the combobox:

 

If("*" in ComboBox.SelectedItems.Value, DataCard3.Required=true, DataCard3.Required=false)

 

The idea is that, if there is an asterisk in the name of any of the selected items, DataCard3 should be required. Otherwise not. I can't seem to figure it out.

1 ACCEPTED SOLUTION

Accepted Solutions
FrankKelly84
Level: Powered On

Re: Changing if a field is required

A little bit of a hack, but I got it working with this:

 

If(CountIf(DataCardValue.SelectedItems,Right(Value,1)="*")>0, UpdateContext({isRequired:true}), UpdateContext({MMRequired:false}))

 

An asterisk is placed at the end of any ComboBox entry that should make the textbox required.

isRequired is the true/false value in the 'Required' property of the textbox to be required or not required.

 

It essentially counts the number of table entries from SelectedItems that end in *, and if any of them do (more than 0), it sets isRequired to true.

3 REPLIES 3
TimRohr
Level 10

Re: Changing if a field is required

You have to pass the settings through a variable. Try with a Context Variable.

 

In the OnChange of the combobox, try something like:

UpdateContext({DataCard3Req:!IsBlank(ComboBox1)})

This flips a context variable named "DataCard3Req" (name it what you want) to be the OPPOSITE of the result of the IsBlank() function. So if the combobox (which I'm imagining being named "ComboBox1") is blank, we set the DataCard3Req to be 'false'.

 

Then set the Required property of DataCard3 to be "DataCard3Req" (or whatever you named your context variable). You might have to unlock the card to set the property.

 

(By the way, as good as PowerApps is with chasing your name changes throughout your code, changing every code reference to the control, it won't change the variable name... just another reason to rename your DataCard to have a meaningful name, so that your context variable can have a meaningful name, too).

FrankKelly84
Level: Powered On

Re: Changing if a field is required

Thank you for the reply. UpdateContext is probably the right way to go. The problem is that I don't want every item in the combobox to change to change 'required' to true, only if an item in the combox contains an asterisk.

 

I've gotten to this (with isRequired as the boolean variable in the textbox's required property):

 

If("*" in ComboBox.SelectedItems.Value, UpdateContext({isRequired:true}), UpdateContext({isRequired:false}))

 

Any help is appreciated. Thanks again

FrankKelly84
Level: Powered On

Re: Changing if a field is required

A little bit of a hack, but I got it working with this:

 

If(CountIf(DataCardValue.SelectedItems,Right(Value,1)="*")>0, UpdateContext({isRequired:true}), UpdateContext({MMRequired:false}))

 

An asterisk is placed at the end of any ComboBox entry that should make the textbox required.

isRequired is the true/false value in the 'Required' property of the textbox to be required or not required.

 

It essentially counts the number of table entries from SelectedItems that end in *, and if any of them do (more than 0), it sets isRequired to true.