Skip to content

Instantly share code, notes, and snippets.

@cursosdesarrolloweb
Last active Nov 19, 2021
Embed
What would you like to do?
<div>
<form wire:submit.prevent="save">
<div class="grid grid-cols-12 gap-2 mt-3">
<div class="col-span-12">
<label for="name">{{ __("Nombre") }}</label>
{!! FormFacade::input('text', 'name', null, ['wire:model.defer' => 'customer.name', 'autocomplete' => 'off']) !!}
@error('customer.name')
<div class="text-theme-21 mt-2">
{{ $errors->first('customer.name') }}
</div>
@enderror
</div>
</div>
<div class="input-form mt-3">
<label for="comments">{{ __("Comentarios") }}</label>
{!! FormFacade::textarea('comments', null, ['wire:model.defer' => 'customer.comments']) !!}
@error('customer.comments')
<div class="text-theme-24 mt-2">
{{ $errors->first('customer.comments') }}
</div>
@enderror
</div>
{!! FormFacade::submit($textButton, ["class" => "btn btn-primary mt-5"]) !!}
</form>
</div>
<?php
namespace Http\Livewire\Customer;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\RedirectResponse;
use Illuminate\Validation\Rule;
use Livewire\Component;
use Livewire\Redirector;
use App\Models\Customer;
class Form extends Component
{
/**
* @var Customer
*/
public Customer $customer;
/**
* @var string
*/
public string $textButton;
/**
* @var bool
*/
public bool $updating = false;
public function rules(): array {
if (!$this->updating) {
return [
'customer.name' => ['required', 'string', 'min:2', 'max:100'],
'customer.comments' => 'nullable|string|max:1000',
];
}
return [
'customer.name' => ['required', 'string', 'min:2', 'max:100', Rule::unique('customers', 'name')->where(function ($query) {
return $query
->where("id", "!=", $this->customer->id)
})],
'customer.comments' => 'nullable|string|max:1000',
];
}
public function render(): Renderable {
return view('livewire.customer.form');
}
/**
* @return RedirectResponse|Redirector
*/
public function save(): RedirectResponse|Redirector {
$this->validate();
$this->customer->save();
// flash|redirect
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment