https://github.com/zaxwebs/ex-l8-multi-forms-2
https://github.com/zaxwebs/ex-l8-multi-forms-2
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/zaxwebs/ex-l8-multi-forms-2
- Owner: zaxwebs
- Created: 2021-02-02T13:13:35.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2021-02-04T22:25:11.000Z (over 5 years ago)
- Last Synced: 2025-01-06T04:41:27.681Z (over 1 year ago)
- Language: PHP
- Size: 75.2 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Introduction
In this post, I'd like to explain my process of handling & 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.
Disclaimer: 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.
Alright, let's get down to it.
# The Flow
1. I add a hidden field called `_name` to my forms. Which holds an identifier for the form e.g `login`, `signup`, etc.
2. On the controller side there is no change, yet. The validation logic stays the same.
3. Before utilizing the old value of, say, username field. I check if the old value of `_name` is the same as defined earlier.
# The Setup
As 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.
```php
name= $name;
$this->id= $id ? $id : ($form && $name ? $form . '-' . $name : null);
$this->type= $type;
$this->label= $label;
$this->form= $form;
}
/**
* Get the view / contents that represent the component.
*
* @return \Illuminate\Contracts\View\View|string
*/
public function render()
{
return view('components.input');
}
}
```
Wouldn'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.
```php
get('errors', app(ViewErrorBag::class));
return old('_name') === $form && $errors->has($name);
});
}
}
```
Here's how my InputComponent view looks like:
```php
@if($label)
{{ $label }}
@endif
@invalid($name, $form)
{{ $errors->first($name) }}
@endinvalid
```
I also utilize a Form component to handle adding the hidden input field as well as automate handling the method for it.
# Summary
This 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.
Thank you for reading.
## About Laravel
Laravel 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:
- [Simple, fast routing engine](https://laravel.com/docs/routing).
- [Powerful dependency injection container](https://laravel.com/docs/container).
- Multiple back-ends for [session](https://laravel.com/docs/session) and [cache](https://laravel.com/docs/cache) storage.
- Expressive, intuitive [database ORM](https://laravel.com/docs/eloquent).
- Database agnostic [schema migrations](https://laravel.com/docs/migrations).
- [Robust background job processing](https://laravel.com/docs/queues).
- [Real-time event broadcasting](https://laravel.com/docs/broadcasting).
Laravel is accessible, powerful, and provides tools required for large, robust applications.
## Learning Laravel
Laravel 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.
If 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.
## Laravel Sponsors
We 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).
### Premium Partners
- **[Vehikl](https://vehikl.com/)**
- **[Tighten Co.](https://tighten.co)**
- **[Kirschbaum Development Group](https://kirschbaumdevelopment.com)**
- **[64 Robots](https://64robots.com)**
- **[Cubet Techno Labs](https://cubettech.com)**
- **[Cyber-Duck](https://cyber-duck.co.uk)**
- **[Many](https://www.many.co.uk)**
- **[Webdock, Fast VPS Hosting](https://www.webdock.io/en)**
- **[DevSquad](https://devsquad.com)**
- **[Curotec](https://www.curotec.com/)**
- **[OP.GG](https://op.gg)**
## Contributing
Thank you for considering contributing to the Laravel framework! The contribution guide can be found in the [Laravel documentation](https://laravel.com/docs/contributions).
## Code of Conduct
In 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).
## Security Vulnerabilities
If 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.
## License
The Laravel framework is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).