PHP code example of schenke-io / livewire-auto-form

1. Go to this page and download the library: Download schenke-io/livewire-auto-form 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/ */

    

schenke-io / livewire-auto-form example snippets


  // In your model
  public function rules(): array
  {
      return [
          'name' => 'your component
  public function ruleKeys(): array
  {
      return [
          'name',
          'email',
      ];
  }

  public function rules(): array
  {
      return $this->scanInheritedRules([
          'name' => 'sometimes', // overrides model rule
      ]);
  }
  

class User extends Model
{
    protected $casts = [
        'settings' => 'array',
    ];

    public function rules(): array
    {
        return [
            'settings.theme' => '

class EditUserSettings extends AutoForm
{
    public function mount(User $user)
    {
        $this->setModel($user);
    }

    public function ruleKeys(): array
    {
        return [
            'settings.theme',
            'settings.notifications.email',
        ];
    }

    public function rules(): array
    {
        return $this->scanInheritedRules();
    }
}

use SchenkeIo\LivewireAutoForm\AutoForm;

class EditPost extends AutoForm
{
    public function ruleKeys(): array
    {
        // Will fetch `name` and `author.email` rules from the active model
        return ['name', 'author.email'];
    }

    public function rules(): array
    {
        return $this->scanInheritedRules(
            ['author.email' => 'nullable|email'] // component override takes precedence
        );
    }
}

use SchenkeIo\LivewireAutoForm\AutoForm;

class EditPost extends AutoForm
{
    public function mount(Post $post)
    {
        $this->setModel($post);
    }

    public function rules(): array
    {
        return [
            'title' => '

class EditPost extends AutoForm
{
    public function mount(Post $post)
    {
        $this->setModel($post);
        $this->autoSave = true;
    }

    public function rules(): array
    {
        return [
            'title' => '

class EditBrand extends AutoForm
{
    public function mount(Brand $brand)
    {
        $this->setModel($brand);
    }

    public function rules(): array
    {
        return [
            'name' => '

enum Status: string implements AutoFormOptions
{
    case DRAFT = 'draft';
    case PUBLISHED = 'published';

    public static function getOptions(?string $labelMask = null): array
    {
        return [
            self::DRAFT->value => 'The Draft',
            self::PUBLISHED->value => 'Live on Site',
        ];
    }
}

class ManageProducts extends AutoForm
{
    public function mount()
    {
        $this->setModel(new Product);
    }

    public function rules(): array
    {
        return [
            'name' => '

class UserWizard extends AutoWizardForm
{
    public array $structure = [
        'profile' => ['name'],
        'address' => ['city'],
    ];
    
    public string $stepViewPrefix = 'livewire.steps.';

    public function rules(): array
    {
        return [
            'name' => '

class CreatePost extends AutoForm
{
    public function mount(Post $post)
    {
        $this->setModel($post);
    }

    public function rules(): array
    {
        return [
            'title' => 'm your redirect
        $this->redirect(route('posts.index'));
    }
}

namespace App\Livewire;

use App\Models\User;
use SchenkeIo\LivewireAutoForm\AutoWizardForm;

class UserWizard extends AutoWizardForm
{
    public array $structure = [
        'profile' => ['name', 'email'],
        'address' => ['city'],
        'preferences' => ['marketing_opt_in']
    ];
    
    public string $stepViewPrefix = 'livewire.user-wizard-steps.';

    public function mount(User $user)
    {
        $this->setModel($user);
        parent::mount();
    }

    public function rules(): array
    {
        return [
            'name' => '

use SchenkeIo\LivewireAutoForm\AutoForm;

class MyComponent extends AutoForm
{
    public function mount(User $user)
    {
        $this->setModel($user);
    }

    public function rules(): array
    {
        return $this->scanInheritedRules();
    }

    public function ruleKeys(): array
    {
        return [
            'name',
            'email',
            'posts.title' // Relation support
        ];
    }
}

class Country extends Model implements AutoFormOptions 
{
    public static function getOptions(?string $labelMask = null): array 
    {
        return self::pluck('name', 'id')->toArray();
    }
}

// lang/en/enums.php
return [
    'status' => [
        'active' => 'Active User',
        'pending' => 'Review Pending'
    ]
];

public static function getOptions(?string $labelMask = null): array {
    return [
        'active' => [
            'key' => 'enums.status.active_count', 
            'replace' => ['count' => User::where('active', true)->count()]
        ]
    ];
}

enum UserStatus: string implements AutoFormOptions {
    use AutoFormLocalisedEnumOptions;
    const OPTION_TRANSLATION_PREFIX = 'enums.user_status';

    case ACTIVE = 'active';
}

public function rules(): array
{
    return [
        'author.name' => '

use Livewire\Attributes\On;

#[On('saved')]
public function handleSave($context, $id)
{
    // Do something when a model is saved
}
html
<form wire:submit.prevent="submit">
    @foreach($this->getSteps() as $index => $step)
        @/form>
html
<form wire:submit.prevent="submit">
    @foreach($this->getSteps() as $index => $step)
        @ctions">
        @if($currentStepIndex > 0)
            <button type="button" wire:click="previous">Previous</button>
        @endif

        <button type="submit">
            {{ $this->isLastStep() ? 'Finish' : 'Next' }}
        </button>
    </div>
</form>
html
<select wire:model.blur="form.status">
    @foreach($this->optionsFor('status') as [$value, $label])
        <option value="{{ $value }}">{{ $label }}</option>
    @endforeach
</select>