PHP code example of barryvdh / laravel-form-bridge
1. Go to this page and download the library: Download barryvdh/laravel-form-bridge library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
barryvdh / laravel-form-bridge example snippets
use FormFactory;
use Illuminate\Http\Request;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
Route::any('create', function()
{
$user = App\User::first();
$form = FormFactory::create(FormType::class, $user)
->add('name', TextType::class)
->add('email', EmailType::class, [
'rules' => 'unique:users,email',
])
->add('save', SubmitType::class, ['label' => 'Save user']);
$form->handleRequest();
if ($form->isSubmitted() && $form->isValid()) {
// Save the user with the new mapped data
$user->save();
return redirect('home')->withStatus('User saved!');
}
return view('user.create', compact('form'));
});
use Barryvdh\Form\ValidatesForms;
use Barryvdh\Form\CreatesForms;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
class UserController extends Controller{
use ValidatesForms, CreatesForms;
public function anyIndex(Request $request)
{
$user = User::first();
$form = $this->createFormBuilder($user)
->add('name', TextType::class)
->add('email', EmailType::class)
->add('save', SubmitType::class, ['label' => 'Save user'])
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted()) {
$this->validateForm($form, $request, [
'name' => '
Class User extends Model {
/** @var UploadedFile */
private $file;
public function getFile()
{
return $this->file;
}
public function setFile(UploadedFile $file = null)
{
$this->file = $file;
}
public function upload()
{
// the file property can be empty if the field is not loadRootDir(),
$this->getFile()->getClientOriginalName()
);
// set the path property to the filename where you've saved the file
$this->path = $this->getFile()->getClientOriginalName();
// clean up the file property as you won't need it anymore
$this->file = null;
}
}
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
$user = User::first();
$form = $this->createFormBuilder($user)
->add('name', TextType::class)
->add('file', FileType::class)
->add('save', SubmitType::class, ['label' => 'Save user'])
->getForm();
$form->handleRequest($request);
if ($form->isValid()) {
$user->upload();
$user->save();
}
$this->app->extend('form.types', function($types, $app){
$types[] = new CustomType();
return $types;
});