imran/form

A simple and consistent way to generate various types of HTML form fields.

1.0.0 2023-01-20 12:47 UTC

This package is auto-updated.

Last update: 2024-04-23 09:42:36 UTC


README

The Form class is a utility class that provides a simple and consistent way to generate various types of HTML form fields.

Installation

You can Download the class file and include it in your project.

or you can install by using composer, this is recommended way.

composer require imran/form

Usage

Opening and closing a form

To create an opening form tag with attributes, use the open() method:

echo Form::open(['action' => '/submit', 'method' => 'post']);

To create a closing form tag, use the close() method:

echo Form::close();

Input fields

To create an input field of any type, use the input() method:

echo Form::input('text', 'first_name', 'John');
echo Form::input('date', 'birthday', '1990-01-01');

To create a textarea field, use the textarea() method:

echo Form::textarea('message', 'Hello, World!');

To create a checkbox field, use the checkbox() method:

echo Form::checkbox('agreement', 1, true);

To create a radio button, use the radio() method:

echo Form::radio('payment', 'credit', true);

To create a file input field, use the file() method:

echo Form::file('resume');

To create a password input field, use the password() method:

echo Form::password('password');

To create an email input field, use the email() method:

echo Form::email('email', 'example@example.com');

To create a label element, use the label() method:

echo Form::label('email', 'Email');

Registering Custom Types

You can register custom types by using the registerCustomType() method and passing in a name for the custom type and a callback function that will generate the HTML for that custom type.

Form::registerCustomType('custom_input', function($type, $name, $value, $attributes = []) {
    $attr_string = Form::attributesToString($attributes);
    return "<input type='$type' name='$name' value='$value' $attr_string>";
});

You can then use the registered custom type by using the custom() method and passing in the name of the custom type and an array of parameters to be passed to the callback function

echo Form::custom('custom_input', ['text', 'first_name', 'John']);

Running Tests

To run tests, use following command

.\vendor\bin\phpunit tests/FormTest.php

Conclusion

The Form class provides a simple and consistent way to generate various types of HTML form fields, making it easy to create forms in your PHP projects. You can also register your custom types to the class, giving you more flexibility and control over your forms. With the open() and close() methods, you can easily create the basic structure of a form, and with the various other methods, you can add different types of input fields, labels, and buttons to your form. The class also allows you to pass in attributes for each field, giving you more control over the appearance and behavior of your form fields. Overall, the Form class is a useful tool for creating and managing forms in your PHP projects.

Hi, I'm Imran Ali! 👋

🚀 About Me

Senior Full-Stack Developer specializing in front end and back-end development. Experienced with all stages of the development cycle for dynamic web projects. Innovative, creative and a proven team player, I possess a Tech Degree in Front End Development and have 7 years building developing and managing websites, applications and programs for various companies. I seek to secure the position of Senior Full Stack Developer where i can share my skills, expertise and experience with valuable clients.

🛠 Skills

PHP OOP, Laravel, Codeigniter Javascript, Node, React, Vue, Git, HTML, Rest Api, Typescript, Angular, SCSS, Docker, CI/CD Jenkins, Bootstrap, Responsive Design, ASP.NET Core

🔗 Follow on

linkedin twitter

License

MIT License GPLv3 License AGPL License

Contributing

Contributions are always welcome!

See contributing.md for ways to get started.

Please adhere to this project's code of conduct.