{"id":19319013,"url":"https://github.com/zaxwebs/ex-l8-multi-forms-2","last_synced_at":"2026-05-15T11:13:05.863Z","repository":{"id":101629657,"uuid":"335293848","full_name":"zaxwebs/ex-l8-multi-forms-2","owner":"zaxwebs","description":null,"archived":false,"fork":false,"pushed_at":"2021-02-04T22:25:11.000Z","size":77,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-01-06T04:41:27.681Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zaxwebs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-02-02T13:13:35.000Z","updated_at":"2021-02-04T22:25:14.000Z","dependencies_parsed_at":"2023-06-06T23:15:16.473Z","dependency_job_id":null,"html_url":"https://github.com/zaxwebs/ex-l8-multi-forms-2","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zaxwebs%2Fex-l8-multi-forms-2","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zaxwebs%2Fex-l8-multi-forms-2/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zaxwebs%2Fex-l8-multi-forms-2/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zaxwebs%2Fex-l8-multi-forms-2/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zaxwebs","download_url":"https://codeload.github.com/zaxwebs/ex-l8-multi-forms-2/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240420982,"owners_count":19798502,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-10T01:20:40.210Z","updated_at":"2026-05-15T11:13:00.831Z","avatar_url":"https://github.com/zaxwebs.png","language":"PHP","funding_links":["https://patreon.com/taylorotwell"],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\u003ca href=\"https://laravel.com\" target=\"_blank\"\u003e\u003cimg src=\"https://raw.githubusercontent.com/laravel/art/master/logo-lockup/5%20SVG/2%20CMYK/1%20Full%20Color/laravel-logolockup-cmyk-red.svg\" width=\"400\"\u003e\u003c/a\u003e\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n\u003ca href=\"https://travis-ci.org/laravel/framework\"\u003e\u003cimg src=\"https://travis-ci.org/laravel/framework.svg\" alt=\"Build Status\"\u003e\u003c/a\u003e\n\u003ca href=\"https://packagist.org/packages/laravel/framework\"\u003e\u003cimg src=\"https://img.shields.io/packagist/dt/laravel/framework\" alt=\"Total Downloads\"\u003e\u003c/a\u003e\n\u003ca href=\"https://packagist.org/packages/laravel/framework\"\u003e\u003cimg src=\"https://img.shields.io/packagist/v/laravel/framework\" alt=\"Latest Stable Version\"\u003e\u003c/a\u003e\n\u003ca href=\"https://packagist.org/packages/laravel/framework\"\u003e\u003cimg src=\"https://img.shields.io/packagist/l/laravel/framework\" alt=\"License\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n# Introduction\n\nIn this post, I'd like to explain my process of handling \u0026 validating multiple forms that can have same-named fields. For example, you can have two side by side forms, say, login and signup on the same page. And you are likely to have matching fields like username, password, and such. If you use something like `old('username')` as the value, the respective fields of both forms would be populated even if you submitted just one.\n\nDisclaimer: This is experimental at the moment, I'm still refining my procedure and looking into other ideas. This is a journal of what I have come up with so far.\n\nAlright, let's get down to it.\n\n# The Flow\n1. I add a hidden field called `_name` to my forms. Which holds an identifier for the form e.g `login`, `signup`, etc.\n2. On the controller side there is no change, yet. The validation logic stays the same.\n3. Before utilizing the old value of, say, username field. I check if the old value of `_name` is the same as defined earlier.\n\n# The Setup\n\nAs you might guess this adds some repetition to the forms and I like to keep things DRY. For this, we do have components to the rescue. Note, how flexible you make them is up to you.\n\n```php\n\u003c?PHP\n\n// app/View/Components/Input.php\n\nnamespace App\\View\\Components;\n\nuse Illuminate\\View\\Component;\n\nclass Input extends Component\n{\n    public $name;\n    public $id;\n    public $type;\n    public $label;\n    public $form;\n\n    /**\n     * Create a new component instance.\n     *\n     * @return void\n     */\n    public function __construct($name= null, $id = null, $type=null, $label='text', $form = null)\n    {\n        //\n        $this-\u003ename= $name;\n        $this-\u003eid= $id ? $id : ($form \u0026\u0026 $name ? $form . '-' . $name : null);\n        $this-\u003etype= $type;\n        $this-\u003elabel= $label;\n        $this-\u003eform= $form;\n    }\n\n    /**\n     * Get the view / contents that represent the component.\n     *\n     * @return \\Illuminate\\Contracts\\View\\View|string\n     */\n    public function render()\n    {\n        return view('components.input');\n    }\n}\n```\n\nWouldn't it be good to have some helpers to perform the checks? For this, I set up custom blade directives that handle checking the conditionals.\n```php\n\u003c?php\n\n// app/Providers/AppServiceProvider.php\n\nnamespace App\\Providers;\n\nuse Illuminate\\Support\\ViewErrorBag;\nuse Illuminate\\Support\\Facades\\Blade;\nuse Illuminate\\Support\\ServiceProvider;\n\nclass AppServiceProvider extends ServiceProvider\n{\n    /**\n     * Register any application services.\n     *\n     * @return void\n     */\n    public function register()\n    {\n        //\n    }\n\n    /**\n     * Bootstrap any application services.\n     *\n     * @return void\n     */\n    public function boot()\n    {\n        Blade::if('from', function ($form = null) {\n            return old('_name') === $form;\n        });\n\n        Blade::if('invalid', function ($name, $form = null) {\n            $errors = session()-\u003eget('errors', app(ViewErrorBag::class));\n            return old('_name') === $form \u0026\u0026 $errors-\u003ehas($name);\n        });\n    }\n}\n\n```\n\nHere's how my InputComponent view looks like:\n```php\n\u003cdiv class=\"mb-3\"\u003e\n    @if($label)\n    \u003clabel for=\"{{ $id }}\" class=\"form-label\"\u003e{{ $label }}\u003c/label\u003e\n    @endif\n    \u003cinput type=\"{{ $type }}\" class=\"form-control @invalid($name, $form) is-invalid @endinvalid\" id=\"{{ $id }}\"\n        name=\"{{ $name }}\" value=\"@from($form){{ old($name) }}@endfrom($form)\" /\u003e\n    @invalid($name, $form)\n    \u003cdiv class=\"invalid-feedback\"\u003e\n        {{ $errors-\u003efirst($name) }}\n    \u003c/div\u003e\n    @endinvalid\n\u003c/div\u003e\n```\n\nI also utilize a Form component to handle adding the hidden input field as well as automate handling the method for it.\n\n# Summary\nThis is just one of perhaps many more solutions out there. This works for me at the moment and I will still continue to explore more.\nThank you for reading.\n\n## About Laravel\n\nLaravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experience to be truly fulfilling. Laravel takes the pain out of development by easing common tasks used in many web projects, such as:\n\n- [Simple, fast routing engine](https://laravel.com/docs/routing).\n- [Powerful dependency injection container](https://laravel.com/docs/container).\n- Multiple back-ends for [session](https://laravel.com/docs/session) and [cache](https://laravel.com/docs/cache) storage.\n- Expressive, intuitive [database ORM](https://laravel.com/docs/eloquent).\n- Database agnostic [schema migrations](https://laravel.com/docs/migrations).\n- [Robust background job processing](https://laravel.com/docs/queues).\n- [Real-time event broadcasting](https://laravel.com/docs/broadcasting).\n\nLaravel is accessible, powerful, and provides tools required for large, robust applications.\n\n## Learning Laravel\n\nLaravel has the most extensive and thorough [documentation](https://laravel.com/docs) and video tutorial library of all modern web application frameworks, making it a breeze to get started with the framework.\n\nIf you don't feel like reading, [Laracasts](https://laracasts.com) can help. Laracasts contains over 1500 video tutorials on a range of topics including Laravel, modern PHP, unit testing, and JavaScript. Boost your skills by digging into our comprehensive video library.\n\n## Laravel Sponsors\n\nWe would like to extend our thanks to the following sponsors for funding Laravel development. If you are interested in becoming a sponsor, please visit the Laravel [Patreon page](https://patreon.com/taylorotwell).\n\n### Premium Partners\n\n- **[Vehikl](https://vehikl.com/)**\n- **[Tighten Co.](https://tighten.co)**\n- **[Kirschbaum Development Group](https://kirschbaumdevelopment.com)**\n- **[64 Robots](https://64robots.com)**\n- **[Cubet Techno Labs](https://cubettech.com)**\n- **[Cyber-Duck](https://cyber-duck.co.uk)**\n- **[Many](https://www.many.co.uk)**\n- **[Webdock, Fast VPS Hosting](https://www.webdock.io/en)**\n- **[DevSquad](https://devsquad.com)**\n- **[Curotec](https://www.curotec.com/)**\n- **[OP.GG](https://op.gg)**\n\n## Contributing\n\nThank you for considering contributing to the Laravel framework! The contribution guide can be found in the [Laravel documentation](https://laravel.com/docs/contributions).\n\n## Code of Conduct\n\nIn order to ensure that the Laravel community is welcoming to all, please review and abide by the [Code of Conduct](https://laravel.com/docs/contributions#code-of-conduct).\n\n## Security Vulnerabilities\n\nIf you discover a security vulnerability within Laravel, please send an e-mail to Taylor Otwell via [taylor@laravel.com](mailto:taylor@laravel.com). All security vulnerabilities will be promptly addressed.\n\n## License\n\nThe Laravel framework is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzaxwebs%2Fex-l8-multi-forms-2","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzaxwebs%2Fex-l8-multi-forms-2","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzaxwebs%2Fex-l8-multi-forms-2/lists"}