We dig into the basic form components and the code that exists behind them.
Part of an unknown length series.
What! I’m just a grown man eating yogurt, Hello and welcome to another episode of Webflow and Code, where I teach you the underlying code you’re writing a Webflow.
Yes, I’m still making Webflow videos, I’ve just been enjoying myself recently exploring Pinegrow which is a new tool that I’ve just been playing around with and learning. I also started a podcast with my good friend Chris Adams where we talk about all things technology so those things have been kind of eating up my time recently as well as just being generally busy with client work.
Whether it’s PHP or whatever and kind of understanding, what is actually happening when you submit a form. So, if that sounds exciting to you then hit the subscribe button, because like I say this will be a series and if you want to be notified when the next episode comes out then you must hit the notification bell icon. So, sit back relax and enjoy the first in a series of forms [Music]
Before we start I just wanted to announce that this week’s episode of that tech show is featuring Joe Krug that texture is my podcast, which you can find more information about That Tech dot Show, so the first things first here we have a form element, a form block rather and what it contains is a form a success message and an error message and I got that through the component pane and you’ve got various different elements here so let’s just start by looking at the raw form, if I preview this and inspect the code what you’ll see is that we have a form um, of course and we have labels that label and an input and a submit button you can think of a form as kind of wrapping a data set.
So, I’ve explained this in the past um, you can have multiple forms on a page, you can have multiple forms on a site, it’s, it’s wrapping a set of information that you want to send to the backend right and in the default case it’s going to be web flow but like I say later on in the series we we’re going to learn to kind of post it elsewhere and so the form represents a data set it could be I mean in this example it looks like a contact field it could be a sign up form for your email list any sort of set of data that you want to send to the backend.
You use a form, if you have a different data set then I would advise you having a different form if say you’ve got a contact form and a sign up form for an email address on your website use, different use two forms, don’t try and bundle them together, don’t try and do anything clever like that really a form is a set of data that’s sort of self-contained in some ways.
So, looking into the, the markup of this form you can see that the form has a name and this will be something that, the you know the um Webflow’s backend will register as obviously the name of the form now they’ve got an id here and some various other things this is just web flow adding things in really, this is nothing um that is essential for for the markup of a form so it’s just the name and the, and the id then we have a label and once again these data W things are just junk that Webflow ads that’s doing things in this backend.
I’m just previewing this actually this could be removed in the, in when you publish it so um but the important thing is on the label is this four attributes so this is so super, super crucial and I think Webflow does a terrible job at sort of communicating this and also enabling you to, to do this because I’ve never been able to um achieve this but you every single form element needs a label this is an accessibility thing and the way it does that is that whatever text right now its name whatever um, whatever label this belongs to the four attribute represents the id of the element that it’s representing.
So in this case we’ve got form, so there must be an input element with an id of name somewhere there’s the id there and the important thing to remember that I, I continue to say, is there, should only be one instance of an id on your page you should not, not that you cannot because absolutely you can because again Webflow’s not doing a very good job at um communicating this there can only be one instance of an element no matter what it is of with an id of name on this page. It can be elsewhere there can be an element with an id of name on another page but there cannot be another element with an id of name on this page that’s why you can confidently say this 4 represents this id, now the name attribute that is the the, the labelling.
Let’s say of the data that’s going to be sent to the front end so whatever is whatever the value is of this input element is going to be recognized as name and that’s how we do that, I think, I think Webflow does something slightly clever and actually take the actual text that you insert into the label um as the as the name um but what’s really happening is the name attribute is, is going to be sent there. So once again yet we have a, we have a label with a for of name which represents uh which reflects the id of this input element the name, like I say is for the backend and we’ve got a max length here of two five six.
I mean that’s just a default value and I don’t believe you can actually change that inside of Webflow but that’s obviously an attribute you can do you can set a max length if you were to go to input element HTML if you to look at some documentation here documentation for the input element if we scroll down we can actually see we can see all the different types that it can be so it can be type let’s just jump back here and pay reference to that this is type text so there can be type check box it could be type color, it can be cut type date, date time local and, and you can see a representation of here’s how it’s going to make how it’s going to render.
It could be a type file you can then, you can actually add these in as attributes if you want them so you’ve got lots of different types here and then we look at the attributes we’ve got accept which hat, which is for a type file so we’re not really concerned with that right now but you know if it was um an input of alt uh with image then you can have an alt let’s scroll down to well let’s look at all, we’ve got autocomplete focus uh disabled form which actually associates the form element to a form elsewhere on the page so actually that’s new to me I actually only thought that was available on the submit button but it looks like you can, you can have a form element anywhere on the page and with the form attribute you, you can associate it with an id of the form scrolling through.
Is it max length there’s the one we saw minimum length, I’m not going to go through all these but you can see that, if you just type in HTML input you can see what it can be and what different types of attributes you can use on your input so jumping back in here then this is the default max length “two four two five six” and once again we have an input of type submit Webflow doesn’t have the idea of buttons actual button elements which is kind of strange because they’re actually very very crucial for interactions on a website, but it’s perfectly okay to have an input of type submit.
And, once again normally these data things are to do with Webflow we’re not really concerned with these data attributes but class is obviously something we’re familiar with and the value is obviously the text that goes within there if you would if you needed to have some sort of HTML inside of um your input inside of your button then you would need to use a button um because you can only put text values inside of this button so you’d use a HTML element and you’d create a button and then, then you can put in some graphics some icons some different text or whatever so that’s the bare minimum kind of form there.
I don’t think there’s anything more to say on that um, you can obviously change these uh waiting text you know button text you can obviously change all these things inside of the form so looking into the settings of a form I’ve gone over this in a previous episode but just to quickly run through of course you can add an id right now it’s been given the id of form if we if I remember correctly you can show the state and this allows you just to play around with the different states so you can style them how, how you’d like all this is doing is on submit Webflow is hiding the form and showing either success or error but depending on these things there’s nothing more to it than that you can see.
These rendered in the preview just as hidden elements the form name again giving it a form name where it redirects to and I would, I would suggest watching my other video on the forms we kind of go into a little bit more detail around this and the different method types um, and the actions and various things like that on the on the label you can’t really do anything but on the input you can have a name and an id and a name and various things like that. Once again I’ve never been able to drag an input in and then drag a, a form element in here so if I drag an input in here and then Webflow tells me that, it once I drag an input and then drag a label.
It should automatically associate those things so if I just preview that and have a little look, I can almost guarantee so there’s no four attribute on that and so I would, I would need to say form uh four sorry field I mean, I can change that to whatever I want but I would, I would need to change the four attribute of the label to field but unfortunately it’s a reserve name so I can’t do that Webflow sort it out what is going on there for again in my other video, I explained what you can do if your design does not show the um, doesn’t it, doesn’t, doesn’t want to show for whatever reason the, the labels.
Now the video goes into that but long story short you can use the “sr” only class so that’s the kind of default form let’s just remove those there and now let’s dig into the actual elements the different types of elements themselves and I can kind of explain them so an input type like we like I said the input type can be very, very lots of different types um and you can even select those in in this plane is obviously equal to text when we look at the elements that are the types, that are available um, scrolling down text that’s the default email which can only accept email and there’s obviously plays into the form validation that happens password, password is nothing more than a text field that obscures the, the, the actual letters into dots.
You, you would have should have put a text plain text and then you can use the pattern, pattern attribute which actually you can put in regular expressions to actually prevent or allow certain types if you only wanted numbers then you would do something like this you know and various things like that it’s quite a clever way to, to only rest to restrict what a user can actually put into the um to the input field and then finally, we’ve got number which as I said well just to wrap up then.
So the phone is actually a new one called tell which obviously only accepts telephone numbers and their number is the number field and once again I would I would encourage you to you know search um well let’s have a look at number for instance number you know I would encourage you to look into these and actually see what attributes are available for that input type you might actually find that there’s some clever things you can do stepping for instance only allows you to add or remove plus 10 or 5 or 3 whatever take a look at the documentation because I think it’s really, really powerful what you can, what you can do with that stuff as long as you know how to read the documentation, that’s it for the text element.
So let’s dive into another input type and kind of just explain it so, we’ve got a file upload which once again is just an input type of file right um this is like a paid for feature but I I don’t know whether this is actually restricted but there’s you could put an input type of file and have that you know play a part in your, in your form um again it’s just an element of input with type of file nothing more complex than that text area once again um, is very, very similar to an input field of type text just so you can have multiple lines and multiple columns so you don’t really get much in the way of settings here, but I encourage you to look at the documentation because I know there’s a lot of things you can do with a text area, oh it’s a text area, it’s not an input type.
So there we go attributes you can auto capitalize, you can auto complete, auto correct, auto focus, columns disabled form, max length, all this stuff might allow you to enhance what you’re doing with the, the text area looking at the next form field now check box and radio I think these two kind of come as a pair because often people actually get quite confused with them so a check box is a set of elements so you can, you, you’ll have multiple um check boxes as long as each of those check boxes have the same name so let’s you know this is the default name of checkbox if I create multiple check boxes so as long as I have the name, the same name of uh the same name they all belong together and they, they get submitted as a of the name check box.
I mean that’s a terrible name let’s use a, let’s use a, a realistic example here contact preferences for instance uh and if we change the name to contact preferences on both of these and then this one with also a name of contact preferences you could have email you could have postal or telephone or whatever and then this will get sent up as one sort of um entry in the database to say their contact preferences are checkboxes are good for multiple values of the same grouping of, of elements radio buttons on the other hand are a set of elements where the user can only select one of the input type so a good example of this would be, uh gender or something like that uh male, female other and you know you might provide another one there for different types of genders, that would be if you only, only want the user to select one of that group of elements so you can make these look.
So it’s quite clever what you can do but the important thing to remember is check boxes are multiple entries for a set of a set of data and then radio buttons are a single entry for a set of data looking at the re, how the radio button is rendered taking a look at this so here they’ve wrapped the entire thing in a label and they’ve not used the for attribute which is also valid HTML so if you don’t use the form attribute then make sure that you’re wrapping your inputs within the within the label and it’s of type radio so it’s just an input once again of type radio and then they’re linked all your radio buttons are linked with the same name so you must use the same name.
If and we can just demonstrate that real quick we’ve got two radio buttons here group name uh gender, gender if we have a look at these but because they have the same name here and here it knows they belong together so we can only select one whereas here I can select multiple another thing to note about check boxes if you if you select on here and add an attribute to the check box the input the actual input itself you can tell it to automatically be checked so checked equals checked same with um, check boxes has to be the actual input checked equals checked then when we publish that then you can pre-select them.
Okay! I guess a point to note is that if you want to, you could make these required field but technically you’re making it required by pre-selecting one value they can’t then unselect that value so this is in theory creating a required field or required set of inputs just by saying one of them is checked jumping back into our fields a select box really there’s not much there’s not much to say about a select box, you can obviously have multiple choices allowing multiple sets a different type of checkbox where you can obviously just select multiple examples if we take a look at how that’s rendered.
It’s just a select field and has a name and that’s about it or an id if you’re going to be using the field if you have a look at when we select allow multiple and how that changes the, the select field if we look at the code then what you can see is uh there should be an attribute of multiple and that’s all that is doing you’re just adding a, an attribute of multiple similarly now reCAPTCHA’s, I think this probably deserves its own episode, episode but recaptures are a way of preventing bots um from submitting data into your form there’s various different types.
So, when you click submit so form, on submit if this is unchecked so if checkbox is unchecked and I’m pseudo coding right now we get into this in another episode but I’m pseudo coding right now if this is unchecked then submit the form if it is checked then don’t do anything. The reason why that’s effective against bots is that bots tend to want to fill out every single form element on the on the form page it just goes through them all and checks them all it’s not 100 effective of course but what you might want to do is put something in it saying if you are a human ignore this checkbox so just in case a human does latch onto it or whatever then that they know to ignore that checkbox uh, and you’d hide it with the sr only class which I’ll link to in the card if you’re interested in sr only.
And it’s, it’s you know legitimate use cases then um this will be a perfect example for that so that’s a honey pot and a way that you can defer bots from actually submitting the data on your page if you didn’t want to use recapture or if recapture is confusing you I know you have to set it up with google and, and register an application and all the rest of it so that’s another way you can defer bots so finally we have a submit button which actually just an input of type submit as we’ve previously discussed and good thing is what we discovered earlier that actually all inputs can, can have the form attribute and this this tends to suggest that you can have elements outside of your form and link to the submission of a form um from elsewhere on the page.
Typically, I would, I’ve used that in the past of a submit button outside of the form when that button is clicked then you can use that so that’s the only I guess nugget of information I can show on a submit button but in the context of Webflow of course you can select the waiting text um and button text here if you did want to create your own button as I mentioned earlier you would need to go into your components here and not have any other submit buttons and just by the very fact that this is inside a form means that this button will be forever linked to your form, and now in here you can put some svgs you know you can have other texts um, and as long as it’s of type equals submit.
Then this will now submit that form so if we close that svg there now you can have graphics icons or or whatever in your in your um in your form and then, if I preview you can see that, if I click that then you’ll get some um, page valid uh, form validation here so the last thing to note is you might want to watch my video on focus states. I think having a focus state in your form is very very important so instead of explaining again here I’ll direct you to my focus video where you can understand the importance of focus states in Webflow.
So if you like this episode please hit the like button because that’s the easiest way to let me know that you actually want more of this content and if you want to stay up to date with more episodes on Forms and Webflow then hit the subscribe button and generally this episode really is all about frontend development and accessibility in particular, so if you want to support the channel then you can support me over at patreon.com fake Sam Gregory and until next time happy no coding [Music]