Skip to content

Commit 4880b5d

Browse files
committed
Simplify example
1 parent eb8f7b3 commit 4880b5d

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

readme.md

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@ Laravel integration:
77
- Add validation errors
88
- Translate field names
99

10-
See for Laravel 5.1 the [0.2 branch](https://github.com/barryvdh/laravel-form-bridge/tree/v0.2.2)
11-
1210
### Install
1311
- `composer require barryvdh/laravel-form-bridge`
1412
- Add `Barryvdh\Form\ServiceProvider::class,` to you ServiceProviders.
15-
- (optional) Add `'FormRenderer' => Barryvdh\Form\Facade\FormRenderer::class,` to your Facades.
1613
- (optional) Add `'FormFactory' => Barryvdh\Form\Facade\FormFactory::class,` to your Facades.
14+
- (optional) Add `'FormRenderer' => Barryvdh\Form\Facade\FormRenderer::class,` to your Facades.
1715

1816
### Basic example
1917

@@ -26,38 +24,36 @@ Or you can create a Named form, with an empty name.
2624
If you need to set more options, use the `createBuilder` function instead of `create`, to be able to use `setAction()` etc. You need to call `->getForm()` to get the actual form instance again.
2725

2826
```php
27+
use FormFactory;
2928
use Illuminate\Http\Request;
3029
use Symfony\Component\Form\FormFactoryInterface;
3130
use Symfony\Component\Form\Extension\Core\Type\FormType;
3231
use Symfony\Component\Form\Extension\Core\Type\TextType;
3332
use Symfony\Component\Form\Extension\Core\Type\EmailType;
3433
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
3534

36-
Route::any('form', function(Request $request, FormFactoryInterface $factory){
35+
Route::any('create', function()
36+
{
3737
$user = App\User::first();
3838

39-
$form = $factory->create(FormType::class, $user)
39+
$form = FormFactory::create(FormType::class, $user)
4040
->add('name', TextType::class)
41-
->add('email', EmailType::class)
41+
->add('email', EmailType::class, [
42+
'rules' => 'unique:users,email',
43+
)
4244
->add('save', SubmitType::class, array('label' => 'Save user'));
4345

44-
$form->handleRequest($request);
45-
46-
if ($form->isSubmitted()) {
47-
$v = Validator::make($request->get($form->getName()), [
48-
'name' => 'required',
49-
'email' => 'required|email',
50-
]);
46+
$form->handleRequest();
5147

52-
if ($v->fails()) {
53-
return redirect()->back()->withInput()->withErrors($v->errors());
54-
}
55-
48+
if ($form->isSubmitted() && $form->isValid()) {
49+
5650
// Save the user with the new mapped data
5751
$user->save();
52+
53+
return redirect('home')->withStatus('User saved!');
5854
}
5955

60-
return view('form', ['form' => $form->createView()]);
56+
return view('user.create', compact('form'));
6157
});
6258
```
6359

@@ -68,6 +64,7 @@ Use the following in your Blade templates:
6864
@form_widget($form)
6965
@form_end($form)
7066
```
67+
7168
Other directives are: @form, @form_label, @form_errors, @form_rest and @form_row
7269

7370
```php

0 commit comments

Comments
 (0)