PHP code example of balfour / laravel-form-builder
1. Go to this page and download the library: Download balfour/laravel-form-builder 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/ */
balfour / laravel-form-builder example snippets
namespace App\Forms;
use App\Models\Role;
use Balfour\LaravelFormBuilder\Form;
use Balfour\LaravelFormBuilder\Components\Checkboxes;
use Balfour\LaravelFormBuilder\Components\EmailInput;
use Balfour\LaravelFormBuilder\Components\MobileNumberInput;
use Balfour\LaravelFormBuilder\Components\NumberInput;
use Balfour\LaravelFormBuilder\Components\PhoneNumberInput;
use Balfour\LaravelFormBuilder\Components\Row;
use Balfour\LaravelFormBuilder\Components\TextInput;
class CreateUserForm extends Form
{
public function __construct()
{
$this->route('post.create_user')
->button('Create')
->with([
TextInput::build()
->name('first_name')
-> ->rule('unique:users'),
TextInput::build()
->name('skype')
->rule('unique:users'),
]),
Checkboxes::build()
->name('roles')
->options(Role::class)
->setVisibility(auth()->user()->can('assign-role')),
]);
}
}
return view('create_user', ['form' => new CreateUserForm()])
namespace App\Forms;
use App\Models\Role;
use App\Models\User;
use Balfour\LaravelFormBuilder\Form;
use Balfour\LaravelFormBuilder\Components\Checkboxes;
use Balfour\LaravelFormBuilder\Components\EmailInput;
use Balfour\LaravelFormBuilder\Components\MobileNumberInput;
use Balfour\LaravelFormBuilder\Components\NumberInput;
use Balfour\LaravelFormBuilder\Components\PhoneNumberInput;
use Balfour\LaravelFormBuilder\Components\Row;
use Balfour\LaravelFormBuilder\Components\TextInput;
use Illuminate\Validation\Rule;
class EditUserForm extends Form
{
public function __construct(User $user)
{
$this->route('post.edit_user', [$user])
->button('Save')
->with([
TextInput::build()
->name('first_name')
->ue('users')->ignore($user)),
TextInput::build()
->name('skype')
->rule(Rule::unique('users')->ignore($user)),
]),
Checkboxes::build()
->name('roles')
->options(Role::class)
->setVisibility(auth()->user()->can('assign-role'))
->defaults(function () use ($user) {
return $user->roles
->pluck('id')
->toArray();
}),
ToggleSwitch::build()
->name('is_two_factor_auth_enforced')
->label('2 Factor Authentication Enforced')
->onLabel('Yes')
->offLabel('No'),
ToggleSwitch::build()
->name('is_enabled')
->label('Account Enabled')
->onLabel('Yes')
->offLabel('No'),
])->fill($user);
}
}
use App\Forms\EditUserForm;
use App\Models\User;
$user = User::find(3);
$form = new EditUserForm($user);
echo $form->render();
use App\Forms\CreateUserForm;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
class UserController extends Controller
{
public function postCreate(Request $request)
{
$request->validate((new CreateUserForm())->getValidationRules());
}
}
namespace App\Http\Requests;
use App\Forms\CreateUserForm;
use Illuminate\Foundation\Http\FormRequest;
class CreateUserRequest extends FormRequest
{
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return (new CreateUserForm())->getValidationRules();
}
}
use Balfour\LaravelFormBuilder\Components\Checkbox;
Checkbox::build()
->name('remember_me')
->label('Remember me for my next visit')
->defaults(1);
Checkbox::build()
->name('agree')
->label('I agree to the terms and conditions')
->rule('accepted');
use App\Models\Role;
use Balfour\LaravelFormBuilder\Components\Checkboxes;
$user = auth()->user();
// using an array of options
$roles = Role::pluck('name', 'id')
->toArray();
Checkboxes::build()
->name('roles')
->options($roles)
->defaults(function () use ($user) {
return $user->roles
->pluck('id')
->toArray();
});
// using a model, or options class
// the class must have a static listify() method which returns an array of key => value pairs
Checkboxes::build()
->name('roles')
->options(Role::class)
->setVisibility(auth()->user()->can('assign-role'))
->defaults(function () use ($user) {
return $user->roles
->pluck('id')
->toArray();
});
// using a callable to resolve options
Checkboxes::build()
->name('roles')
->options(function () {
return Role::pluck('name', 'id')
->toArray();
});
use Balfour\LaravelFormBuilder\Components\DateInput;
// if no label is given, it's automagically generated from the component's name
DateInput::build()
->name('start_date');
// we can add a custom validation rule on top of the automagically generated ones
DateInput::build()
->name('start_date')
->rule('after:today');
use Balfour\LaravelFormBuilder\Components\EmailInput;
EmailInput::build()
->name('email');
// here's another custom validation rule on top of the 'email' one
EmailInput::build()
->name('email')
->rule('unique:users,email');
use Balfour\LaravelFormBuilder\Components\DateInput;
use Balfour\LaravelFormBuilder\Components\FieldSet;
FieldSet::build()
->legend('General')
->with([
DateInput::build()
->name('start_date')
->
use Balfour\LaravelFormBuilder\Components\FileInput;
FileInput::build()
->name('photo')
->
use Balfour\LaravelFormBuilder\Components\HiddenInput;
HiddenInput::build()
->name('user_id')
->defaults(1);
use Balfour\LaravelFormBuilder\Components\MobileNumberInput;
MobileNumberInput::build()
->defaults(auth()->user()->mobile_number);
use Balfour\LaravelFormBuilder\Components\NumberInput;
NumberInput::build()
->name('age');
use Balfour\LaravelFormBuilder\Components\PasswordInput;
PasswordInput::build();
// if you want a custom label...
PasswordInput::build()
->name('Your Password');
use Balfour\LaravelFormBuilder\Components\PhoneNumberInput;
PhoneNumberInput::build()
->defaults(auth()->user()->phone_number);
use Balfour\LaravelFormBuilder\Components\RichTextEditor;
// this will render a textarea with a 'wysiwyg' class
// it's up to you to instantiate a rich text editor on this element, such as with
// TinyMCE or Froala Editor
RichTextEditor::build()
->name('description')
->rows(10)
->
use Balfour\LaravelFormBuilder\Components\DateInput;
use Balfour\LaravelFormBuilder\Components\Row;
Row::build()
->with([
DateInput::build()
->name('start_date')
->
use App\Models\Country;
use Balfour\LaravelFormBuilder\Components\Select;
// using an array of options, and m',
3 => 'Italy',
])
->hasEmptyOption();
// using a model, or options class
// the class must have a static listify() method which returns an array of key => value pairs
Select::build()
->name('country_id')
->options(Country::class)
->defaults(1);
// using a callable to resolve options
Select::build()
->name('country_id')
->options(function () {
return [
1 => 'South Africa',
2 => 'Amsterdam',
3 => 'Italy',
];
});
use Balfour\LaravelFormBuilder\Components\TextArea;
TextArea::build()
->name('description')
->rows(15)
->
use Balfour\LaravelFormBuilder\Components\TimePicker;
// the component is rendered as an input with a 'time-picker' class
// you'd need to use a timepicker js lib of your choice to render it
TimePicker::build()
->name('start_time')
->
use Balfour\LaravelFormBuilder\Components\ToggleSwitch;
ToggleSwitch::build()
->name('two_factor_authentication_enabled')
->defaults(true);
// use custom on and off labels
ToggleSwitch::build()
->name('subscribe')
->onLabel('Yes')
->offLabel('No')
->defaults(true);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.