Is there an easy way to flag a UDinput field as mandatory, and label it with special color code?
Hi @cn9ne,
You could use input validation, either by marking the parameter in your endpoint as Mandatory = $true or using the UD validate stuffs.
See this:
https://docs.universaldashboard.io/components/inputs#validating-input
Happy hunting
Thanks, @BoSen29.
And, would it be possible to have âmandatoryâ input field label with custom colors? I did a simple - âparam([Parameter(Mandatory = $true)]$emailaddress,â - but I dont see any ârequiredâ flag in css inspect. Any idea?
Hi @cn9ne,
Sadly, i donât think there is any easily implemented functions for this, other than the validation message when the param is not provided (which you can customize).
Iâd suggest just â* Email addressâ, add the star in front of the label in order to symbolize that itâs required.
Yeah, I already did that.
But my user complains they canât see those small asterisks - they want a different color. SighhâŚ
Hmm, you could do custom style if you had a way of referencing the speisific input.
Either that or just simply have the âplaceholderâ to be âREQUIREDâ
I can look into the first option if youâd like?
Yeah, sure.
But i am not sure if we can assign IDs - I can only call the input not the label. Been trying to figure out if I can set custom attributes /style for specific inputfield. Or how do you flag the particular input as ârequiredâ.
Thanks for the help by the way.
The ID of a UDInputField is the Name parameter (or the parameter name if you are using a basic UDInput.)
New-UDInputField -Type textbox -Name FirstName -Placeholder 'First Name'
will show up in the HTML as
<div class="input-field col">
<input class="ud-input" type="text" id="FirstName" name="FirstName" value="" style="~truncated~">
<label class="" for="FirstName">First Name</label>
</div>
You could create custom CSS for the input field or for the label. Your CSS selector for the Input (HTML tag, which is contained in the UDInput) would be by ID #FirstName
or input#FirstName
and your CSS selector for the label would be label[for="FirstName"]
From there you could set Color = 'red'
or `border = â1px solid redâ or whatever.
Thanks alot @sysiphean. That worked!
By the way, any idea for select drop-down label - the id keep on changing.
Donât bother with an ID for the label, as it wonât have one. Reference it instead using a different CSS selector:
label[for="FirstName"]
Replace FirstName with the ID of your input field. That way you are selecting the label with an Attribute selector, specifically an attribute equals selector.
Yes, it worked for standard input text box. I am struggling with disabled input box and select drop-down. For select drop-down, when I used id=4 the font color changed, but now the ID has changed to 6. Probably the Id is generated at random or sequence?
UPDATE:
Okay, for disabled input label this worked.
'input[type=text]:not(.browser-default):disabled+label[for="LDAPid"]'= @{
'color' = 'Red'
}
I am struggling to figure out on how to change color for dropdown select label - the for=nr keeps on changing. For example, when I used for=4 the font color changed, but now the ID has changed to 6. Probably the number is generated at random or sequence? Not sure if itâs possible to do css text selector that contains specific string. Any idea?
A little fussing with this this morning left me with a clue, but Iâm not sure how helpful it will be.
I started by grabbing the default Custom Form code from the docs, and playing with it by rearranging the order of the Input Fields. Comparing what I saw there with the ud-input-field.jsx code that generates it (which I only half-understand, as Iâm just learning the language) I think I follow what it is doing.
Here is the HTML source for the UDInput itself, collapsed, with the label from the Select (dropdown) selected.
In this example (which is pulled straight from the docs page) it has 6 UDInputFields, in order:
- -Type âtextboxâ -Name âEmailâ
- -Type âcheckboxâ -Name âNewsletterâ
- -Type âselectâ -Name âFavoriteLanguageâ
- -Type âradioButtonsâ -Name âFavoriteEditorâ
- -Type âpasswordâ -Name âpasswordâ
- -Type âtextareaâ -Name ânotesâ
All of these are generated directly under the same <div>, but not all the same way. The CheckBox and RadioButtons are generated as <label> with the content below them, while the Textbox, Select, Password, and TextArea are generated as <div> with a <div> and a <label> below those. (Side note: I did not check how the other Types of Switch, Date, File, Time, or BinaryFile render.) Follow so far?
When I use the order you see here, we get <label for=â2â>. If I move it to the top, it stays at 2, if I move it below Password it goes to 3, but moving it above and below the Checkbox and RadioButtons makes no difference. So it is based on some of the ones that render as a <div> but not all.
Which is to say, after all that, that you should be able to moderately predict the number, based on the form as designed, but if you change the form you will need to double-check it. Alternately, if you only have one Select in the form, you could just jam a bunch CSS selectors for the things it could come out for:
#IDOfTheForm label[for="4"], #IDOfTheForm label[for="6"]
That would get it whether it is 4 or 6, so long as itâs under the form with -ID âIDOfTheFormâ
If I understood the .jsx a little better, or if I didnât have a job Iâm supposed to be doing while Iâm fussing with this , I may be able to give a better answer.
Thank you for the thorough checks, I truly appreciate it.
I get the idea. I declared the #IDofUDinput label[for=âxâ] in theme, but unfortunately the label number keep on changing from 4,5,8,12. I donât know how this was generated, the select is the second UDinput field in the form - although under this UDinput thereâs another nested UDinput. The label number changes when I go to other pages, or did a CTRL+F5. This is really disappointing.
@adam - Will custom color for mandatory field be included in the V3 UDform release?
I added a second page with an identical (except ID) form, one page as -Content and one as -Endpoint. As I reload them, they seem to switch back and forth between IDs 3 and 6. It would make sense that as you add more the possible numbers would randomly increase.
Until thereâs some way to set an ID on these (eagerly awaiting V3âŚ) you may have to get creative with Complex CSS Selectors. Particularly under Pseudo-Classes, Iâd look into the :nth-of-type(n) info.
In this example from before, you could use a selector:
#IDOfTheForm > .card-content > div.input-field:nth-of-type(2) > label
The > character denotes a direct child element, so you can walk down from the <div> with that ID, then the card-content <div>, then the second child of that which is a <div> with class input-field, then the label below that. Itâs crazy, but it should work. Just play with your form to figure out which âNthâ your Select field is, and change it from 2 to whatever matches your form layout.
OMG! That worked!
Youâre truly a savior! Thank you!!!