This commit adds two new components for handling form inputs in the private section of the site. 'Form.php' provides a base class for forms with methods for retrieving request names. 'Input.php' extends this to handle input fields specifically, allowing title, name, type, and value to be specified. An associated Blade view 'input.blade.php' has been added to render these inputs in views. This helps in reusability and maintainability.
This commit is contained in:
parent
24098415b5
commit
55cd927f12
32
app/View/Components/Private/Forms/Form.php
Normal file
32
app/View/Components/Private/Forms/Form.php
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\View\Components\Private\Forms;
|
||||||
|
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
use Illuminate\View\Component;
|
||||||
|
use Illuminate\View\View;
|
||||||
|
|
||||||
|
abstract class Form extends Component
|
||||||
|
{
|
||||||
|
private ?string $requestName = null;
|
||||||
|
|
||||||
|
abstract protected function getName(): string;
|
||||||
|
abstract public function render(): View;
|
||||||
|
|
||||||
|
protected function getRequestName(): string
|
||||||
|
{
|
||||||
|
if (!is_null($this->requestName)) {
|
||||||
|
return $this->requestName;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->requestName = Str::of($this->getName())
|
||||||
|
->replace(
|
||||||
|
['.', '[', ']'],
|
||||||
|
['_', '.', ''],
|
||||||
|
)
|
||||||
|
->rtrim('.')
|
||||||
|
->value();
|
||||||
|
|
||||||
|
return $this->requestName;
|
||||||
|
}
|
||||||
|
}
|
50
app/View/Components/Private/Forms/Input.php
Normal file
50
app/View/Components/Private/Forms/Input.php
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\View\Components\Private\Forms;
|
||||||
|
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
use Illuminate\View\View;
|
||||||
|
|
||||||
|
final class Input extends Form
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private readonly string $title,
|
||||||
|
private readonly string $name,
|
||||||
|
private readonly string $type = 'text',
|
||||||
|
private readonly ?string $value = ''
|
||||||
|
) { }
|
||||||
|
|
||||||
|
protected function getName(): string
|
||||||
|
{
|
||||||
|
return $this->name;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getTitle(): string
|
||||||
|
{
|
||||||
|
return Str::ucfirst($this->title);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getType(): string
|
||||||
|
{
|
||||||
|
return $this->type;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getValue(): string
|
||||||
|
{
|
||||||
|
return (string) old($this->getRequestName(), $this->value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
public function render(): View
|
||||||
|
{
|
||||||
|
return view('private.components.forms.input', [
|
||||||
|
'title' => $this->getTitle(),
|
||||||
|
'name' => $this->getName(),
|
||||||
|
'requestName' => $this->getRequestName(),
|
||||||
|
'type' => $this->getType(),
|
||||||
|
'value' => $this->getValue()
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
7
resources/views/private/components/forms/input.blade.php
Normal file
7
resources/views/private/components/forms/input.blade.php
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
<div class="mb-4">
|
||||||
|
<label for="form-input-{{ $requestName }}">{{ $title }}</label>
|
||||||
|
<input id="form-input-{{ $requestName }}" class="form-control @error($requestName) is-invalid @enderror" name="{{ $name }}" type="{{ $type }}" @if($type !== 'password') value="{{ $value }}" @endif {{ $attributes }}>
|
||||||
|
@error($name)
|
||||||
|
<span class="invalid-feedback">{{ $message }}</span>
|
||||||
|
@enderror
|
||||||
|
</div>
|
Loading…
Reference in New Issue
Block a user