Is it possible to get a widget by its ID?

Is it possible to get a widget by its ID?

Hi OutSystems community,

In my application I have a number of hardcoded input fields and comboboxes that I use to define a type of product.

Some of these input fields and comboboxes have validations that are checked everytime the user changes its content.

I'm trying to change the widget properties (Valid and ValidationMessage) depending on the result of my validation check and I would like to do this dynamically (e.g. I pass the ID of the widget and I want to get the widget with that specific ID).

Is this possible? It would be great if I could define a list with those widgets and then run a function "getWidgetById" to filter the result.

Thanks in advance,
It's not possible to get a widget by its Id since in platform widgets are not objects (they have records, but you can't assing it to a variable for example).

However, you can achieve this by javascript. You could get the widgets elements at clientside (find by a extended property that identifies it) and change the associated Validation Message. It will demand some work, but it is possible.

Hope it helps.
Thanks for the quick answer but I'm relatively new to OutSystems and didn't quite understand your explanation!

When I change the content in the input field it triggers a screen action.

Should I add an extended property to the widget and then use it on the screen action?

The extended property would be only to helps you to find the element (by using a JQuery attribute selector, for example). But actually you don't need that. You can create a "SetWidgetValidationNameById" function that receives as input paramenter the WidgetId. What this actions would do is to execute a RunJavascript action (from HTTPRequestHandler extension) containing the script to change the validation message at client side. I can't say exactly how this script would work, but if you find your widget element by its id on the html you will find the way to change the validation message (it is probably some span)
Ok thanks. I think I can change the validation message, but I'm not being able to change the Widget attribute Valid to false. Is it also customizable through JS?
I think this attribute can be only changed at server side, since its escope is limited to the server. It is important to note that validation can be client & server or server only.

If you choose the first one, outsystems platform will peform some validations via javascript and, in case invalid, no post will occur. It is still possible to do client-side custom validations. Check this component to see how to do it.

If the validation is server only, the server peforms the validation, and in the response it sets the validation message to display (at this point you can change the message value). In this case to set the Valid attribute of an specific element, you'll have to set it on server. You could put a switch condition in the triggered screen action, passing its id as input parameter. If the element has the WidgetId xxxx you set its Valid attribute to false... Of course it is not the better approach, but is a solution.
Thanks again but I think I found a good way to do it.

All my validations are custom so I check them when the input is changed. Depending on the result of those validations I will then change the widget state by modifying its className.

Through inspecting the html, I found that the class name of a invalid input widget is:
    className += " Not_Valid Not_Valid"

At the end of the screen action I run my RunJavaScript function using the WidgetId and I get the expected result.

Thanks a lot André :)

Awesome! Good work :)