Skip to content

Commit

Permalink
#5 Remove :errors="$errors" attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
forxer committed Apr 28, 2023
1 parent 4acef10 commit 7c40c25
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 94 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
CHANGELOG
=========

0.2.0 (2023-04-29)
------------------

- #5 Remove :errors="$errors" attribute


0.1.0 (2023-04-14)
------------------

Expand Down
51 changes: 17 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ For example a typical form field with Bootstrap:
```blade
<div class="mb-3">
<x-label for="title" />
<x-input name="title" :errors="$errors" />
<x-error field="title" :errors="$errors" />
<x-input name="title" />
<x-error field="title" />
</div>
```

Expand Down Expand Up @@ -96,7 +96,7 @@ Usage

Take for example the input form field.

If you manually installed, in the file `/config/blade-ui-kit.php` replace the "input" component class:
In the file `/config/blade-ui-kit.php` replace the "input" component class:

```php
'components' => [
Expand All @@ -120,18 +120,14 @@ This allows to load the component from this package instead of *Blade UI Kit*.

You can then use the component as you would from [Blade UI Kit Input Component](https://blade-ui-kit.com/docs/input) but it will directly have the CSS classes from Bootstrap.

The *only difference* is that if you want to apply [Bootstrap's validation CSS classes](https://getbootstrap.com/docs/5.2/forms/validation/#server-side), **you must** pass errors to the component:

```blade
<x-input name="search :errors="$errors" />
<x-input name="search />
```

[Back to index ^](#index)

### Email

<details>
<summary>If you manually installed</summary>
In the file `/config/blade-ui-kit.php` you must replace:

```php
Expand All @@ -143,20 +139,17 @@ By:
```php
'email' => BladeUIKitBootstrap\Components\Forms\Inputs\Email::class,
```
</details>

You can then use the component as you would from [Blade UI Kit Email component](https://blade-ui-kit.com/docs/email) without forgetting to pass the possible errors in more:
You can then use the component as you would from [Blade UI Kit Email component](https://blade-ui-kit.com/docs/email):

```blade
<x-email :errors="$errors" />
<x-email />
```

[Back to index ^](#index)

### Password

<details>
<summary>If you manually installed</summary>
In the file `/config/blade-ui-kit.php` you must replace:

```php
Expand All @@ -168,20 +161,17 @@ By:
```php
'password' => BladeUIKitBootstrap\Components\Forms\Inputs\Password::class,
```
</details>

You can then use the component as you would from [Blade UI Kit Password component](https://blade-ui-kit.com/docs/password) without forgetting to pass the possible errors in more:
You can then use the component as you would from [Blade UI Kit Password component](https://blade-ui-kit.com/docs/password):

```blade
<x-password :errors="$errors" />
<x-password />
```

[Back to index ^](#index)

### Textarea

<details>
<summary>If you manually installed</summary>
In the file `/config/blade-ui-kit.php` you must replace:

```php
Expand All @@ -193,20 +183,17 @@ By:
```php
'textarea' => BladeUIKitBootstrap\Components\Forms\Inputs\Textarea::class,
```
</details>

You can then use the component as you would from [Blade UI Kit Textarea component](https://blade-ui-kit.com/docs/textarea) without forgetting to pass the possible errors in more:
You can then use the component as you would from [Blade UI Kit Textarea component](https://blade-ui-kit.com/docs/textarea):

```blade
<x-textarea name="about" :errors="$errors" />
<x-textarea name="about" />
```

[Back to index ^](#index)

### Label

<details>
<summary>If you manually installed</summary>
In the file `/config/blade-ui-kit.php` you must replace:

```php
Expand All @@ -218,12 +205,11 @@ By:
```php
'error' => BladeUIKitBootstrap\Components\Forms\Error::class,
```
</details>

You can then use the component as you would from [Blade UI Kit Label component](https://blade-ui-kit.com/docs/label).
You can then use the component as you would from [Blade UI Kit Label component](https://blade-ui-kit.com/docs/label):

```blade
<x-label for="search" />
<x-label for="search" />
```

Or composing the content:
Expand All @@ -238,8 +224,6 @@ Or composing the content:

### Error

<details>
<summary>If you manually installed</summary>
In the file `/config/blade-ui-kit.php` you must replace:

```php
Expand All @@ -251,20 +235,17 @@ By:
```php
'error' => BladeUIKitBootstrap\Components\Forms\Error::class,
```
</details>

You can then use the component as you would from [Blade UI Kit Error component](https://blade-ui-kit.com/docs/error) without forgetting to pass the possible errors in more:
You can then use the component as you would from [Blade UI Kit Error component](https://blade-ui-kit.com/docs/error).

```blade
<x-error name="search :errors="$errors" />
<x-error name="search />
```

[Back to index ^](#index)

### Form

<details>
<summary>If you manually installed</summary>
In the file `/config/blade-ui-kit.php` you must replace:

```php
Expand All @@ -276,7 +257,6 @@ By:
```php
'form' => BladeUIKitBootstrap\Components\Forms\Form::class,
```
</details>

You can then use the component as you would from [Blade UI Kit Form component](https://blade-ui-kit.com/docs/form).

Expand All @@ -291,3 +271,6 @@ If you do not want to use this attribute:
```

[Back to index ^](#index)


// 'alert' => Components\Alerts\Alert::class,
42 changes: 0 additions & 42 deletions src/Components/Forms/Concerns/CanHaveError.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/Components/Forms/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ class Error extends BladeComponent
{
use CanHaveErrors;

public function __construct(ViewErrorBag $errors, string $field, string $bag = 'default')
public function __construct(string $field, string $bag = 'default')
{
$this->bootCanHaveErrors($errors, $field, $bag);
$this->bootCanHaveErrors($field, $bag);
}

public function render(): View
Expand Down
5 changes: 2 additions & 3 deletions src/Components/Forms/Inputs/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
namespace BladeUIKitBootstrap\Components\Forms\Inputs;

use Illuminate\Contracts\View\View;
use Illuminate\Support\ViewErrorBag;

class Email extends Input
{
public function __construct(ViewErrorBag $errors, string $name = 'email', string $id = null, ?string $value = '', ?string $errorBag = null)
public function __construct(string $name = 'email', string $id = null, ?string $value = '', ?string $errorBag = null)
{
parent::__construct($errors, $name, $id, 'email', $value, $errorBag);
parent::__construct($name, $id, 'email', $value, $errorBag);
}

public function render(): View
Expand Down
7 changes: 3 additions & 4 deletions src/Components/Forms/Inputs/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@
use BladeUIKitBootstrap\Concerns\CanHaveErrors;
use BladeUIKit\Components\Forms\Inputs\Input as BukInput;
use Illuminate\Contracts\View\View;
use Illuminate\Support\ViewErrorBag;

class Input extends BukInput
{
use CanHaveErrors;

public function __construct(ViewErrorBag $errors, string $name, string $id = null, string $type = 'text', ?string $value = '', ?string $errorBag = null)
public function __construct(string $name, string $id = null, string $type = 'text', ?string $value = '', ?string $errorBag = null)
{
parent::__construct($name, $id, $type, $value, $errorBag);
parent::__construct($name, $id, $type, $value);

$this->bootCanHaveErrors($errors, $name, $errorBag);
$this->bootCanHaveErrors($name, $errorBag);
}

public function render(): View
Expand Down
5 changes: 2 additions & 3 deletions src/Components/Forms/Inputs/Password.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
namespace BladeUIKitBootstrap\Components\Forms\Inputs;

use Illuminate\Contracts\View\View;
use Illuminate\Support\ViewErrorBag;

class Password extends Input
{
public function __construct(ViewErrorBag $errors, string $name = 'password', string $id = null, ?string $errorBag = null)
public function __construct(string $name = 'password', string $id = null, ?string $errorBag = null)
{
parent::__construct($errors, $name, $id, 'password', null, $errorBag);
parent::__construct($name, $id, 'password', null, $errorBag);
}

public function render(): View
Expand Down
6 changes: 3 additions & 3 deletions src/Components/Forms/Inputs/Textarea.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ class Textarea extends BukTextarea
{
use CanHaveErrors;

public function __construct(ViewErrorBag $errors, string $name, string $id = null, $rows = 3, ?string $errorBag = null)
public function __construct(string $name, string $id = null, $rows = 3, ?string $errorBag = null)
{
parent::__construct($name, $id, $rows, $errorBag);
parent::__construct($name, $id, $rows);

$this->bootCanHaveErrors($errors, $name, $errorBag);
$this->bootCanHaveErrors($name, $errorBag);
}

public function render(): View
Expand Down
13 changes: 10 additions & 3 deletions src/Concerns/CanHaveErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace BladeUIKitBootstrap\Concerns;

use Illuminate\Contracts\View\Factory as ViewFactory;
use Illuminate\Support\ViewErrorBag;

trait CanHaveErrors
Expand All @@ -26,13 +27,19 @@ public function messages(): array
return $bag->has($this->errorField) ? $bag->get($this->errorField) : [];
}

protected function bootCanHaveErrors(ViewErrorBag $errors, string $errorField, ?string $errorBag): void
protected function bootCanHaveErrors(string $errorField, ?string $errorBag): void
{
static $view = null;

if ($view === null) {
$view = resolve(ViewFactory::class);
}

$this->errors = $view->shared('errors', new ViewErrorBag);

$this->errorField($errorField);
$this->errorBag($errorBag);

$this->errors = $errors;

$this->hasErrors = $this->hasErrors();
}

Expand Down

0 comments on commit 7c40c25

Please sign in to comment.