PHP:Form Series, Part 2: Formtypes & Setting Default Values
(Page 1 out of 3)Welcome to the second part of the PHP:Form Series. In the previous part I explained exactly what PHP:Form is, and one of its core features; inbuilt validators. In case you have forgotten, or haven't read that part yet, it might be wise to read it before reading this part. I'm assuming you have a reasonable understanding of how PHP:Form works.
In this part we're going to have a look at "form types" and the ability to set the default values using PHP. I've also included several code examples and live demos.
Form Types
What are they?
Form types can be used to "re-define" new types of fields. Basically, they are templates that can be used in your PHP:Form. They can save you a lot of time when creating forms, and especially when you need to re-use a certain HTML part often, form types are a real time saver. The below example demonstrates a simple form type:
"text" name="{var:name}" />
"submit" value="Go!" />
As you can see two text fields will be shown, but we've only had to write the HTML for it once. You can create an unlimited amount of form types, and you can even create form types that don't even contain a field, e.g.
This isn't even a {var:bla}
As you might have noticed, you can also use variables. These variables are in the format of {var:attribute_name}. You can use any variable name you like, and simply pass the value of that variable as an attribute on the formfield tag (like I did in the examples).