jayrchamp / simple-php-form-builder
Yet another php form builder
v1.0.0
2018-10-02 23:31 UTC
Requires
- php: >=5.4
This package is auto-updated.
Last update: 2024-09-29 04:20:25 UTC
README
composer require jayrchamp/simple-php-form-builder
Basic Usage
Getting Started
1) Instantiate the class
$form = new SimplePhpFormBuilder\Form();
2) Add the required methods (attribute) to the form object and open the form for fields
$form->action('/page/submit.php')->open();
Methods ( attributes )
-
action
- The action to be performed when the form is submitted.
- (string) (Required)
-
method
- Accepts
GET
,POST
. - (string) (Optional) (Default
POST
)
- Accepts
-
enctype
- Accepts
multipart/form-data
,application/x-www-form-urlencoded
. - (string) (Optional) (Default
multipart/form-data
)
- Accepts
-
id
- Form's id attribute.
- (string) (Optional)
-
class
- Form's classes as an array or string.
- (array | string ) (Optional)
-
attr
- A key-value pair array of custom attributes with their associated value.
- Or a string of a single attribute
- (array | string) (Optional) (Default
[]
)
Action methods
open
- Creates the form and gets it ready to receive field inputs.
- Needs to be the last method added on the form object.
- (void) (Required)
Complete example ( form's instantiation )
$form->id('form_id') ->action('/page/submit.php') ->method('POST') ->class('form_class1')->class(['form_class2', 'form_class3']) ->attr(['data-form' => 'form-data']) ->enctype('multipart/form-data') ->open();
3) Add form fields
$form->field( string $type )->label( string $label )->build();
Methods ( attributes )
-
field
- Field's type.
- (string) (Required)
-
label
- Field's label
- (string) (Required)
-
name
- Field's name attribute.
- If not set, label will be slugified and added as field name attribute.
- If you set it, it will automatically be slugified for you. Not need to add underscore, etc.
- It will keep the square brackets, if you need an array.
- (string) (Optional) (Default
Slugified label
)
-
value
- Field's value attribute
- (string) (Optional) (Default
''
)
-
id
- Form's id attribute.
- (string) (Optional)
-
class
- Form's classes as an array or string.
- (array | string ) (Optional)
-
placeholder
- Field's placeholder
- (string) (Optional) (Default
''
)
-
attr
- A key-value pair array of custom attributes with their associated value.
- Or a string of a single attribute
- (array) (Optional) (Default
[]
)
Action methods
build
- Creates the field.
- Needs to be the last method added on the field object.
- (void) (Required)
Complete example ( Creation of a form field )
$form->field( 'text' ) ->label('What is your name?') ->name('name') ->placeholder('ie.: John Doe') ->id('text_id') ->class('text_class1')->class(['text_class2', 'text_class3']) ->attr(['data-text' => 'text-data']) ->attr('required') ->build();
4) Add a submit field
$form->field('submit')->value('Send')->build();
Methods ( attributes )
-
field
- Field's type.
- (string) (Required)
-
label
- Field's label
- (string) (Optional) (work but useless)
-
value
- Field's value attribute
- (string) (Optional) (Default
''
)
-
id
- Form's id attribute.
- (string) (Optional)
-
class
- Form's classes as an array or string.
- (array | string ) (Optional)
-
attr
- Either a key-value pair array of custom attributes with their associated value.
- or a string for a single attribute without value
- (array) (Optional) (Default
[]
)
Action methods
build
- Creates the field.
- Needs to be the last method added on the field object.
- (void) (Required)
5) Close the form
$form->close();
More examples
select
form field
$form->field('select') ->label('Where do you want to go?') ->placeholder('Select') ->addOption('To the beach') ->addOption('Las vegas') ->addOption('Planet Mars') ->build();
checkbox
form field
$form->field('checkbox')->label('Banana')->name('Favorite Fruits[]')->build(); $form->field('checkbox')->label('Apple')->name('Favorite Fruits[]')->build(); $form->field('checkbox')->label('Kiwi')->name('Favorite Fruits[]')->build();
radio
form field
$form->field('radio')->label('Canada')->name('Favorite_country')->build(); $form->field('radio')->label('USA')->name('Favorite_country')->build(); $form->field('radio')->label('China')->name('Favorite_country')->build();
hidden
form field
$form->field('hidden')->name('my hidden field')->value('done')->build();
Methods ( attributes )
-
field
- Field's type.
- (string) (Required)
-
name
- Field's name. Is Required since no label will be slugified.
- (string) (Optional)
-
value
- Field's value. Not required but would be brian to add it.
- (string) (Optional) (Default
''
)
-
id
- Form's id attribute.
- (string) (Optional)
-
class
- Form's classes as an array or string.
- (array | string ) (Optional)
-
attr
- Either a key-value pair array of custom attributes with their associated value.
- or a string for a single attribute without value
- (array) (Optional) (Default
[]
)
-
label
- Field's label. Work but useless.
- (string) (Optional)
Action methods
build
- Creates the field.
- Needs to be the last method added on the field object.
- (void) (Required)