Skip to content

Commit

Permalink
Added all_forms_with_novalidate global configuration value
Browse files Browse the repository at this point in the history
  • Loading branch information
forxer committed Jun 16, 2024
1 parent 82550d9 commit 3bbb1ec
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 14 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.14.0 (2024-06-16)
-------------------

- Added `all_forms_with_novalidate` global configuration value


0.13.1 (2024-05-10)
-------------------

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
"laravel/framework": "^10.0 || ^11.0"
},
"require-dev": {
"laravel/pint": "^1.15.3",
"laravel/pint": "^1.16.0",
"driftingly/rector-laravel": "^1.2",
"rector/rector": "^1.0.5"
"rector/rector": "^1.1.0"
},
"autoload": {
"psr-4": {
Expand Down
12 changes: 12 additions & 0 deletions config/blade-ui-kit-bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,18 @@

'prefix' => '',

/*
|--------------------------------------------------------------------------
| Always use novalidate
|--------------------------------------------------------------------------
|
| By default all forms have the `novalidate` attribute,
| you can reverse this behavior with this option.
|
*/

'all_forms_with_novalidate' => true,

/*
|--------------------------------------------------------------------------
| All buttons outline
Expand Down
47 changes: 38 additions & 9 deletions docs/forms.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Forms
-----
=====

Form
----
Expand All @@ -25,24 +25,31 @@ This will output the following HTML:
</form>
```

### Browers validation

By default the `novalidate` attribute, in order to avoid browser validation *and* to use consistent error styles on all types of form fields, is set to `true`.

If you do not want to use this attribute simply set it to `false`:
Of course, you can use PHP in the `action` attribute, for example a named route:

```blade
<x-form action="http://example.com" :novalidate="false">
<x-form :action="route('home')">
Form fields...
</x-form>
```

This will output the following HTML:

```html
<form method="POST" action="http://example.com" novalidate="true">
<input type="hidden" name="_token" value="...">
<input type="hidden" name="_method" value="POST">

Form fields...
</form>
```

### HTTP method

By default a `POST` HTTP method will be set. Of course, you can customize this:

```blade
<x-form action="http://example.com" method="PUT">
<x-form :action="route('home')" method="PUT">
Form fields...
</x-form>
```
Expand All @@ -63,7 +70,7 @@ This will output the following HTML:
To enable file uploads in a form you can make use of the `has-files` attribute:

```blade
<x-form action="http://example.com" has-files>
<x-form :action="route('home')" has-files>
Form fields...
</x-form>
```
Expand All @@ -77,6 +84,28 @@ This will output the following HTML:
</form>
```

### Browers validation

By default the `novalidate` attribute, in order to avoid browser validation *and* to use consistent error styles on all types of form fields, is set to `true`.

If you do not want to use this attribute simply set it to `false`:

```blade
<x-form :action="route('home')" :novalidate="false">
Form fields...
</x-form>
```

If you want this attribute to never be automatically added, you can disable it globally for all forms. To do this, change the value `all_forms_with_novalidate` to `false` in the configuration file.

It will always be possible to use it occasionally by setting it to `true`:

```blade
<x-form :action="route('home')" :novalidate="true">
Form fields...
</x-form>
```

Label
-----

Expand Down
2 changes: 1 addition & 1 deletion docs/inputs/inputs.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Inputs
A set of components to make it easier to write form elements.

* [The `<input>` element](#input)
* [Text](./docs/inputs/inputs.md#input)
* [Text](./text.md)
* [Textarea](./textarea.md)
* [Select](./select.md)
* [Password](./password.md)
Expand Down
1 change: 0 additions & 1 deletion pint.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
},
"blank_line_before_statement": {
"statements": [
"break",
"case",
"continue",
"declare",
Expand Down
4 changes: 3 additions & 1 deletion src/Components/Forms/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ class Form extends BladeComponent
public function __construct(
public string $action,
public bool $hasFiles = false,
public bool $novalidate = true,
public string $method = 'POST',
public ?bool $novalidate = null,
) {
$this->method = $this->validFormMethod($method);

$this->novalidate = $novalidate ?? $this->config('all_forms_with_novalidate');
}

public function viewName(): string
Expand Down

0 comments on commit 3bbb1ec

Please sign in to comment.