PHP code example of coyote6 / laravel-forms

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')
			->

<div {{ $attributes->merge([
	'class' => 'new classes'
]) }}></div>
// Or
<x-component :attributes="$attributes->merge(['class'=>'new classes'])"></x-component>

$form = new Form (['theme' => 'minimal']);

$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');



$form->email ('field-name');



$form->email ('field-name')
	->label ('Email')
	->confirm()
	->label ('Confirm Email');



$form->fieldGroup ('field-name')
	->label ('Contact Info');



$form->file ('field-name')
	->label ('Upload File');


public function storeFallback () {

	$upload = request()->file('file');
	if (!is_null ($upload) && $upload->isValid()) {
		
		//
		// Save File using the Laravel method
		//
		// @see https://laravel.com/docs/10.x/filesystem#file-uploads
		//
		
	}
	
	//
	// Note:
	// This still on generateForm () {
	
	$form = form();
	$form->action('/post/path');
	
	$default = '/path/to/file.jpg';
	
	$form->file ('file')
		->label ('File')
		->value ($default);
	
	return $form;
	
}

public function storeFallback () {

	$uploads = request()->file ('files');
	if (!is_null ($uploads) && is_array ($uploads)) {
		foreach ($uploads as $key => $upload) {
			if ($upload->isValid()) {
				
				//
				// Save File using the Laravel method
				//
				// @see https://laravel.com/docs/10.x/filesystem#file-uploads
				//
		
			}
		}
	}
	
	//
	// Note:
	// This still file.jpg'
	];
	
	$form->file ('files')
		->label ('Files')
		->multi()			// Alias to multiple ($maxFiles)
		->value ($default);
	
	return $form;
}

public function store () {

	$this->validate();

	if ($this->file instanceof \Livewire\TemporaryUploadedFile) {
		
		//
		// Save File using the Livewire Temp File method
		//
		// @see https://laravel-livewire.com/docs/2.x/file-uploads#storing-files
		//
		$this->file->store ('dir');
		
	}
	

	foreach ($this->fileRemoved as $hash => $file) {
		
		//
		// Remove File
		//
		// This method depends on how you saved the file.
		//
		
	}
	
}
	
	
protected function generateForm () {
	
	$form = form();
	$form->action('/post/path');
	
	$default = '/path/to/file.jpg';
	
	$form->file ('file')
		->label ('File')
		->value ($default);
	
	return $form;
	
}

public function store () {

	$this->validate();
	foreach ($this->files as $file) {
		
		// Save File using the Livewire Temp File method
		//
		// Multi File Method
		// @see https://laravel-livewire.com/docs/2.x/file-uploads#multiple-files
		// 
		$this->file->store ('dir');
		
	}
	

	foreach ($this->filesRemoved as $hash => $file) {
		
		//
		// Remove Files
		//
		// This method depends on how you saved the file.
		//
		
	}

	
}

protected function generateForm () {
	
	$form = form();
	$form->action('/post/path');
	
	$default = [
		'/path/to/file.jpg'
	];
	
	$form->file ('files')
		->label ('Files')
		->multi()			// Alias to multiple ($maxFiles)
		->value ($default);
	
	return $form;
}


$form->hidden ('field-name')
	->value = 'some value';



$form->html ('field-name')
	->content  ('<em>Custom Html Field</em>');
	


$form->image ('field-name')
	->label ('Upload Image');



$form->number ('field-name')
	->label ('Enter Your Lucky Number');



$form->password ('field-name')
	->label ('Password');
	


$p = $form->password ('field-name')
	->label ('Password')
	->confirm()
	->label ('Password Confirm');
	


$form->radios ('field-name')
	->label ("Please select an option")
	->addOptions([
	  'o1' => 'Option 1',
	  'o2' => 'Option 2',
	  'o3' => 'Option 3',
	  'o4' => 'Option 4'
	])
	->


$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);



$s = $form->select ('field-name');
$s->addOptions([
  'o1' => 'Option 1',
  'o2' => 'Option 2',
  'o3' => 'Option 3',
  'o4' => 'Option 4'
]);
$s->


$form->submitButton ('field-name')
	->value ('submit')
	->content ('Press me');			// $s->label becomes the content, if the content property is not set.



$form->submitButton ('field-name')
	->value ('submit')
	->content ('Press me')
	->renderAsButton();




$form->submitButton ('field-name')
	->value ('submit')
	->label ('Press me')
	->renderAsInput();



$form->text ('field-name');



$form->textarea ('field-name');

sh
php artisan vendor:publish --tag=laravel-forms
 PHP
{!! $form !!}
 PHP
{!! $form !!}
 PHP
Route::post('/store', 'App\Http\Livewire\Example@storeFallback');