-
Notifications
You must be signed in to change notification settings - Fork 0
Form class
Important: This documentation is still under construction, methods and/or properties may be missing or not fully documented
The Form class is a helper that provides a set of static methods for generating commonly used HTML form tags.
Nearly all of the methods in this class allow setting additional attributes for the HTML tags they generate. You can specify, for example, class, style or id for an HTML element using the $attributes parameter. See the HTML::tag() for more details.
Note: If your markup is nearly static, it's better to use HTML directly. There's no need to wrap absolutely everything in Html helper calls.
The code for generating a tag looks like the following:
use JPToolkit\HtmlHelper\Form;
$attributes = [
'name' => 'username',
'id' => 'username',
'value' => '',
'type' => 'text'
];
echo Form::input( $attributes );The $attributes is an array of HTML attributes. In this array the key is the name of the attribute (such as class, value or id), and the value is its value. You can also use a query string to set the tag attributes.
The code above will generate the following HTML:
<input name="username" value="" type="text" id="username" />Generates a complete HTML input tag. Can be used for generate almost any input tag, some of the other methods in this class are partially a shorthand of this method.
Form::input( array $attributes = [ 'value' => '', 'type' => 'text' ] )$attributes (array|string) (Optional)
Associative array of attributes with name-value pairs. These will be rendered as HTML attributes of the resulting tag. See Html::parse_attributes() for more details.
return (string)
The generated input tag.
$attributes = [
'class' => 'form-control'
];
echo Form::input( $attributes );The code above will generate the following HTML:
<input value="" type="text" class="form-control" />Generates a complete HTML textarea tag.
Form::textarea( string $content, array $attributes = [ ] )$content (string) (Optional)
The content to be enclosed between the start and end tags. This parameter is escaped using esc_textarea().
$attributes (array|string) (Optional)
Associative array of attributes with name-value pairs. These will be rendered as HTML attributes of the resulting tag. See Html::parse_attributes() for more details.
return (string)
The generated textarea tag.
$attributes = [
'class' => 'form-control'
];
echo Form::textarea( 'Textarea content' ,$attributes );The code above will generate the following HTML:
<textarea class="form-control">Textarea content</textarea>Generates a label tag.
Form::label( string $content, array $attributes = [ ] )$content (string) (Optional)
The content to be enclosed between the start and end tags.
$attributes (array|string) (Optional)
Associative array of attributes with name-value pairs. These will be rendered as HTML attributes of the resulting tag.
return (string)
The generated textarea tag.
$attributes = [
'for' => 'field-id'
];
echo Form::label( 'Field label', $attributes );
// This method is an aliases of the following
echo Html::tag( 'label' , 'Field label', $attributes );In the code above both examples will generate the following HTML:
<label for="field-id">Field label</label>Generates a set of options tags.
Form::options( array|string $options, string $selected = '' )$options (array|string) (Required)
An array with elements to list as option sets. Nested arrays are allowed, in this case the option tags will be grouped in optgroup. The available shorthands are weekdays and months and you can add your own shorthands.
$selected (string) (Optional)
Value to check in the array to mark as selected, this value is compared with the key of array.
return (string)
The generated option tags.
$colors = [
'#FFFFFF' => __( 'White', 'textdomain' ),
'#808080' => __( 'Gray', 'textdomain' ),
'#000000' => __( 'Black', 'textdomain' ),
'#FF0000' => __( 'Red', 'textdomain' ),
'#FFFF00' => __( 'Yellow', 'textdomain' ),
'#008000' => __( 'Green', 'textdomain' ),
'#0000FF' => __( 'Blue', 'textdomain' ),
];
echo Form::options( $colors );In the code above will generate the following HTML:
<option value="#FFFFFF">White</option>
<option value="#808080">Gray</option>
<option value="#000000">Black</option>
<option value="#FF0000">Red</option>
<option value="#FFFF00">Yellow</option>
<option value="#008000">Green</option>
<option value="#0000FF">Blue</option>$animals = [
'Sky' => [
'crow' => 'Crow',
'peacock' => 'Peacock',
'dove' => 'Dove',
'sparrow' => 'Sparrow',
'owl' => 'Owl',
],
'Ground' => [
'squirrel' => 'Squirrel',
'dog' => 'Dog',
'lion' => 'Lion',
'mouse' => 'Mouse',
'horse' => 'Horse',
],
'Sea' => [
'whale' => 'Whale',
'seahorse' => 'Seahorse',
'shark' => 'Shark',
'octopus' => 'Octopus',
'fish' => 'Fish',
],
];
echo Form::options( $animals, 'octopus' );In the code above will generate the following HTML:
<optgroup label="Sky">
<option value="crow">Crow</option>
<option value="peacock">Peacock</option>
<option value="dove">Dove</option>
<option value="sparrow">Sparrow</option>
<option value="owl">Owl</option>
</optgroup>
<optgroup label="Ground">
<option value="squirrel">Squirrel</option>
<option value="dog">Dog</option>
<option value="lion">Lion</option>
<option value="mouse">Mouse</option>
<option value="horse">Horse</option>
</optgroup>
<optgroup label="Sea">
<option value="whale">Whale</option>
<option value="seahorse">Seahorse</option>
<option value="shark">Shark</option>
<option value="octopus" selected>Octopus</option>
<option value="fish">Fish</option>
</optgroup>Note that the options is grouped in optgroup and the octopus option is selected.
| Version | Description |
|---|---|
| 1.0.0 | Introduced |