1. Go to this page and download the library: Download ramondev/formcraft 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/ */
ramondev / formcraft example snippets
use Formcraft\FormProcessor;
use Formcraft\FormFields;
use Formcraft\LaravelFormTemplate;
class ContactForm extends FormProcessor
{
public function setFields(): LaravelFormTemplate
{
return new LaravelFormTemplate(fn (FormFields $f) => [
'name' => $f->CharField(label: 'Full name'),
'email' => $f->EmailField(),
]);
}
}
public function store(Request $request, ContactForm $form)
{
$form->validate($request->all());
if (! $form->is_validated()) {
return back()->withErrors($form->errors()->all())->withInput();
}
// do something with $form->values()
}
use Formcraft\FormProcessor;
$addressSet = FormProcessor::formSet(AddressForm::class, num: 3, label: 'address');
// Render each copy (names are auto-namespaced: address_0, address_1, address_2)
foreach ($addressSet->set() as $addressForm) {
echo $addressForm;
}
public function create(Request $request)
{
$rows = max((int) $request->query('rows', 3), 1);
if ($request->session()->hasOldInput()) {
$rows = max((int) old('item_count', $rows), 1); // keep the same row count
}
$items = FormProcessor::formSet(OrderItemForm::class, $rows, 'item');
if ($request->session()->hasOldInput()) {
$items->validate($request->session()->getOldInput());
}
return view('orders.create', compact('items'));
}
use Formcraft\FormWizard;
$wizard = new FormWizard('signup', [
'account' => AccountStepForm::class,
'profile' => ProfileStepForm::class,
'billing' => BillingStepForm::class,
]);
// Show the current step
$step = $wizard->currentStep(); // e.g. 'account' - null once all steps are done
$form = $wizard->form(); // that step's FormProcessor, pre-filled from any prior submission
// Handle a submission
if ($wizard->submitStep($request->all(), $request->allFiles())) {
return redirect()->route('signup.step', $wizard->currentStep());
}
// Failed - use $wizard->lastForm()->errors(). If the failed step has a file
// input, render $wizard->lastForm() directly instead of redirecting, since a
// redirect can't carry an uploaded file back with it.
// Once done
if ($wizard->isComplete()) {
$allData = $wizard->allData(); // ['account' => [...], 'profile' => [...], 'billing' => [...]]
}
protected function defaultTheme(): FormTheme
{
return new \Formcraft\Support\TailwindTheme();
}
use Formcraft\Support\FormTheme;
use Formcraft\ObjectClass;
class MyTheme implements FormTheme
{
public function inputClasses(string $renderType, array $props): string
{
// $renderType: "input" | "textarea" | "select" | "groupField"
// for "input", $props['type'] is the HTML input type
return 'my-input';
}
public function labelClasses(string $renderType, array $props): string
{
return 'my-label';
}
public function render(string $name, ObjectClass $fields, array $props, string $renderType): string
{
// $fields->label / ->input are already-rendered HTML strings
// $fields->errors is a Collection of this field's error messages
$wrapperTag = ($props['group'] ?? false) ? 'fieldset' : 'div';
return "<{$wrapperTag}>{$fields->label}{$fields->input}</{$wrapperTag}>";
}
}
namespace App\Forms;
use Formcraft\FormFields;
use Formcraft\FormProcessor;
use Formcraft\LaravelFormTemplate;
use Formcraft\Support\FormTheme;
use Formcraft\Support\BootstrapTheme;
class RegistrationForm extends FormProcessor
{
protected function defaultTheme(): FormTheme
{
return new BootstrapTheme();
}
public function setFields(): LaravelFormTemplate
{
return new LaravelFormTemplate(fn (FormFields $f) => [
'name' => $f->CharField(
label: 'Full name',
minLength: 2,
maxLength: 100,
),
'email' => $f->EmailField(
validateRule: ['unique:users,email'],
),
'password' => $f->PasswordField(
minLength: 8,
hashValue: true,
),
'avatar' => $f->ImageField(
namespace App\Http\Controllers;
use App\Forms\RegistrationForm;
use App\Models\User;
use Illuminate\Http\Request;
class RegistrationController extends Controller
{
public function create(RegistrationForm $form)
{
return view('register', compact('form'));
}
public function store(Request $request, RegistrationForm $form)
{
$form->validate($request->all(), $request->allFiles());
if (! $form->is_validated()) {
return back()->withErrors($form->errors()->all())->withInput();
}
$form->uploadFiles();
$form->setModel(new User());
$user = $form->save();
return redirect()->route('dashboard')->with('status', 'Welcome, ' . $user->name . '!');
}
}