Skip to content

Commit

Permalink
Merge pull request #11 from kgrzelak/dev
Browse files Browse the repository at this point in the history
Select & readme update
  • Loading branch information
kgrzelak authored Jun 30, 2024
2 parents 595d58a + e560e54 commit 1716d2a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
39 changes: 36 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
# Html form helper for Laravel

## Features
- Simple form elements creation with laravel errors from validation
- Fully customizable form elements with fluent interface

## Installation
```bash
composer require kgrzelak/laravel-form
```

### Publish files
After installation, you can publish config file.
```bash
php artisan vendor:publish --provider="Kgrzelak\LaravelForm\LaravelFormServiceProvider"
```

## Usage example
## Usage examples in blade

### Form input
```php
Form::input()
->name('input-name')
Expand All @@ -23,3 +27,32 @@ Form::input()
->attribute('readonly', 'readonly')
->attribute('required', 'required');
```

### Form textarea
```php
Form::textarea()
->name('textarea-name')
->value('textarea-value')
->placeholder('textarea-placeholder')
->setClass('form-control')
->addClass('mt-5')
->attribute('readonly', 'readonly')
->attribute('required', 'required');
```

### Form select
```php
Form::select()
->name('select-name')
->setOptions([
'option-value-1' => 'option-label-1',
'option-value-2' => 'option-label-2',
'option-value-3' => 'option-label-3',
])
->addOption('option-value-4', 'option-label-4')
->value('option-value-2')
->setClass('form-control')
->addClass('mt-5')
->attribute('readonly', 'readonly')
->attribute('required', 'required');
```
8 changes: 7 additions & 1 deletion src/Items/FormSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ class FormSelect extends BaseItem

public function setOptions(array $options): static
{
$this->options = $options;
if (count($options) === count($options, COUNT_RECURSIVE)) {
foreach ($options as $value => $label) {
$this->options[] = ['value' => $value, 'label' => $label];
}
} else {
$this->options = $options;
}

return $this;
}
Expand Down
14 changes: 14 additions & 0 deletions tests/FormSelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,18 @@ public function testSelectedOptionWorks()

$this->assertEquals('<select name="test" class="form-select"><option value="test">Test</option><option value="test2" selected>Test 2</option></select>', $select);
}

public function testCanSetOptionsAsMultidimensionalArray()
{
$select = LaravelForm::select()->name('test')->setOptions([['value' => 'test', 'label' => 'Test'], ['value' => 'test2', 'label' => 'Test 2']]);

$this->assertEquals('<select name="test" class="form-select"><option value="test">Test</option><option value="test2">Test 2</option></select>', $select);
}

public function testCanSetOptionsToSelect(): void
{
$select = LaravelForm::select()->name('test')->setOptions(['test' => 'Test', 'test2' => 'Test 2']);

$this->assertEquals('<select name="test" class="form-select"><option value="test">Test</option><option value="test2">Test 2</option></select>', $select);
}
}

0 comments on commit 1716d2a

Please sign in to comment.