1. Go to this page and download the library: Download coyote6/laravel-forms 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/ */
coyote6 / laravel-forms example snippets
use Coyote6\LaravelForms\Form\Form;
$form = new Form();
$field1 = $form->text('field-name--1'); // Returns the field object
$field1->addRule ('min', '5');
$field1->label = 'Field 1';
$field1->
namespace App\Http\Controllers;
use Coyote6\LaravelForms\Form\Form;
use Illuminate\Http\Request;
class HomeController extends Controller {
protected function form () {
static $form; // Create a singleton to keep form ids the same when validating.
if (is_null ($form)) {
$form = new Form();
$form->action ('/home'); // Default action is '#' so it will submit to the same page if not set.
$form->method ('POST'); // Available methods GET, POST, PUT, or DELETE
$form->email('email')
->placeholder ('Please, Enter Your Email Address')
->
$form = new Form();
$form->action ('/home')
->method ('PUT');
$form->text ('username')
->label ('Username')
->email')
->label ('Email')
->ly adds extra confirm field and validates it.
->label ('Email Confirmation'); // Return value is the confirmation field.
$form->password ('password')
->label ('Password')
->
@stack('scripts')
namespace App\Http\Livewire;
use Livewire\Component;
use Coyote6\LaravelForms\Form\Form;
class Example extends Component {
public $email = '';
public $password = '';
public $passwordConfirmation = '';
protected function rules() {
return $this->form()->lwRules(); // lwRules() is an alias to livewireRules()
}
public function updated ($field) {
$this->validateOnly($field);
}
public function render () {
return view ('livewire.example', ['form' => $this->form()]);
}
public function store () {
$values = $this->validate();
return ['success' => $values];
}
// Optionally set up a store method as a fallback in case someone shuts off JavaScript
public function storeFallback () {
$values = $this->form()->validate();
return ['success' => $values];
}
public function form () {
static $form; // Create a singleton to keep form ids the same when validating.
if (is_null ($form)) {
$form = new Form ($this); // Sets up the form as a Livewire form...
// alternatively you can call $form->isLivewireForm($this);
$form->action ('/store') // Optional fallback in case someone shuts off Javascript
->method('POST')
->addAttribute ('wire:submit.prevent', 'store');
$form->email ('email')
->label ('Email')
->livewireModel ('email')
->
namespace App\Http\Livewire;
use Coyote6\LaravelForms\Livewire\Component;
use Coyote6\LaravelForms\Form\Form;
class Example extends Component {
public $email = '';
public $password = '';
public $passwordConfirmation = '';
// Optional
public function template () {
return 'livewire.example';
}
public function store () {
$values = $this->validate();
return ['success' => $values];
}
// Optionally set up a store method as a fallback in case someone shuts off JavaScript
public function storeFallback () {
$values = $this->form()->validate();
return ['success' => $values];
}
public function generateForm () {
$form = new Form ([
'lw' => $this,
'cache' => false,
'theme' => 'minimal'
]);
$form->addAttribute ('wire:submit.prevent', 'store');
$form->email ('email')
->lwLazy()
->label ('Email')
->
$form->theme ('minimal'); // Using the method option is chainable
$field->theme ('minimal');
// Or set the property directly.
$form->theme = 'minimal';
$field->theme = 'minimal';
$form->button ('field-name')
->value ('Button Value') // Is the default button content unless the content property is set.
->content ('This is what shows inside the button');
$form->checkbox ('field-name')
->value ('Value when submitted')
->label ('Click me');
$r2 = $form->radios ('field-name')
->lue
$rb1 = new Radio ('field-name');
$rb1->label ('Option 1')
->value ('o1')
->init(); // When building directly from field classes,
// initiating the theme/classes is optional,
// but );
$h2 = new Html ('field-name--html-2');
$h2->value ('Cool HTML info about option 2');
$r2->addField ($rb1);
$r2->addField ($h1);
$r2->addField ($rb2);
$r2->addField ($h2);