Skip to content

Commit

Permalink
v1.0.1 (#1)
Browse files Browse the repository at this point in the history
* enhance(console): Add make:form command
* chore(docs): Add console command to docs
* chore(docs): Add error message customization to docs
* fix(component): Fix hidden slot
* fix(ci): Fix CircleCI
  • Loading branch information
Log1x authored May 17, 2020
1 parent 548c715 commit 4532745
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 26 deletions.
65 changes: 44 additions & 21 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,29 +1,52 @@
version: 2.1

orbs:
php: circleci/php@0.1

# To be removed after https://github.com/CircleCI-Public/php-orb/pull/11
executors:
default:
executors:
php-73:
docker:
- image: 'circleci/php:7.3-stretch'
jobs:
build-php:
executor: php-73
steps:
- run: php -v
- checkout
- restore_cache:
keys:
- composer-v1-{{ checksum "composer.lock" }}
- composer-v1-
- run: composer install -n --prefer-dist --no-scripts --no-suggest
- run: composer lint
- save_cache:
key: composer-v1-{{ checksum "composer.lock" }}
paths:
- vendor
description: The official next-gen CircleCI PHP Docker image.
parameters:
tag:
description: The `cimg/php` Docker image version tag.
type: string
docker:
- image: 'cimg/php:<< parameters.tag >>'

# To be removed after https://github.com/CircleCI-Public/php-orb/issues/23
jobs:
build-php:
parameters:
version:
default: '7.4'
description: The `cimg/php` Docker image version tag.
type: string
executor:
name: default
tag: << parameters.version >>
steps:
# Because squizlabs/php_codesniffer requires ext-simplexml.
# To be removed after https://github.com/CircleCI-Public/cimg-php/pull/51
- run: sudo apt-get update
- run: sudo apt-get install -y php<< parameters.version >>-xml
- run: php -v
- run: composer --version
# To be removed after https://github.com/CircleCI-Public/cimg-php/pull/52
- run: sudo chown $(whoami):$(whoami) ~/.composer
- checkout
- php/load-cache:
key: v1
- run: composer install -n --prefer-dist
- run: composer lint
- php/save-cache:
key: v1

workflows:
build:
jobs:
- default/build-php
- build-php:
name: build-php-<< matrix.version >>
matrix:
parameters:
version: ['7.4', '7.3', '7.2']
28 changes: 24 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
![Build Status](https://img.shields.io/circleci/build/github/Log1x/sage-html-forms?style=flat-square)
![Total Downloads](https://img.shields.io/packagist/dt/log1x/sage-html-forms?style=flat-square)

This is a simple package for [HTML Forms](https://github.com/ibericode/html-forms) that allows you to easily render forms using a corresponding Blade view (if one is present) with Sage 10.
This is a simple package for the plugin [HTML Forms](https://github.com/ibericode/html-forms) that allows you to easily render forms using a corresponding Blade view (if one is present) with Sage 10.

A few additional opinionated tweaks include:

Expand Down Expand Up @@ -35,7 +35,13 @@ You can leave the "Form code" blank as it will not be used if a corresponding Bl

### Creating a View

Once your form is created, create a Blade view in `views/forms/contact-us.blade.php` where `contact-us` is equal to your form's slug.
Once your form is created, simply generate a view using the slug assigned to your form:

```bash
$ wp acorn make:form contact-us
```

You will find the generated form view in `resources/views/forms/contact-us.blade.php` containing a simple form component:

```php
<x-html-forms :form="$form" class="my-form">
Expand All @@ -47,7 +53,7 @@ Once your form is created, create a Blade view in `views/forms/contact-us.blade.
>

<input
name="cool_email"
name="emailAddress"
type="email"
placeholder="Email Address"
required
Expand All @@ -60,7 +66,21 @@ Once your form is created, create a Blade view in `views/forms/contact-us.blade.
</x-html-forms>
```

...and you're done. Form action variables simply reference the input `name` so `[NAME]` and `[COOL_EMAIL]` will now be readily available.
When HTML Forms processes "Form Actions" – it simply fetches each input name to create the usable variables.

That being said, the default view would provide `[NAME]` and `[EMAILADDRESS]`.

#### Error Messages

Outside of defining your error messages on the options page, you can optionally provide them to the `<x-html-forms />` component directly:

```php
<x-html-forms
:form="$form"
:messages="['success' => 'Thank you!', 'error' => 'Yikes! Try again.']"
class="my-form"
/>
```

## Bug Reports

Expand Down
2 changes: 1 addition & 1 deletion resources/views/components/html-forms.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<div style="display: none;">
<input type="hidden" name="_hf_form_id" value="{{ $form->ID }}" />
<input type="text" name="_hf_h{{ $form->ID }}" value="" />
{!! $hiddenFields !!}
{!! $hidden !!}
</div>

{!! $slot !!}
Expand Down
148 changes: 148 additions & 0 deletions src/Console/FormMakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?php

namespace Log1x\HtmlForms\Console;

use Illuminate\Support\Str;
use Roots\Acorn\Console\Commands\GeneratorCommand;

class FormMakeCommand extends GeneratorCommand
{
/**
* The console command signature.
*
* @var string
*/
protected $signature = 'make:form {name* : The form slug.}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new form view for the HTML Forms plugin.';

/**
* The view stub used when generated.
*
* @var string|bool
*/
protected $view = 'default';

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$this->task("Generating {$this->getViewName()}", function () {
if (! $this->files->exists($this->getViewPath())) {
$this->files->makeDirectory($this->getViewPath());
}

if ($this->files->exists($this->getView())) {
return;
}

$this->files->put($this->getView(), $this->files->get($this->getViewStub()));
});

return $this->summary();
}

/**
* Return the full view destination.
*
* @return string
*/
public function getView()
{
return Str::finish($this->getViewPath(), $this->getViewName());
}

/**
* Return the view destination filename.
*
* @return string
*/
public function getViewName()
{
return Str::finish(
str_replace('.', '/', Str::slug(Str::snake($this->getNameInput()))),
'.blade.php'
);
}

/**
* Return the view destination path.
*
* @return string
*/
public function getViewPath()
{
return Str::finish($this->getPaths(), '/forms/');
}

/**
* Get the view stub file for the generator.
*
* @return string
*/
protected function getViewStub()
{
return __DIR__ . "/stubs/views/{$this->view}.stub";
}

/**
* Return the applications view path.
*
* @param string $name
* @return void
*/
protected function getPaths()
{
$paths = $this->app['view.finder']->getPaths();

if (count($paths) === 1) {
return head($paths);
}

return $this->choice('Where do you want to create the view(s)?', $paths, head($paths));
}

/**
* Return the block creation summary.
*
* @return void
*/
protected function summary()
{
$this->line('');
$this->line('<fg=blue;options=bold>Form View Created</>');
$this->line(" ⮑ <fg=blue>{$this->shortenPath($this->getView(), 4)}</>");
}

/**
* Returns a shortened path.
*
* @param string $path
* @param int $i
* @return string
*/
protected function shortenPath($path, $i = 3)
{
return collect(
explode('/', $path)
)->slice(-$i, $i)->implode('/');
}

/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
//
}
}
20 changes: 20 additions & 0 deletions src/Console/stubs/views/default.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<x-html-forms :form="$form" class="my-form">
<input
name="name"
type="text"
placeholder="Full Name"
required
>

<input
name="emailAddress"
type="email"
placeholder="Email Address"
required
>

<input
type="submit"
value="Submit"
/>
</x-html-forms>
5 changes: 5 additions & 0 deletions src/HtmlFormsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Roots\Acorn\ServiceProvider;
use Illuminate\View\Compilers\BladeCompiler;
use Log1x\HtmlForms\HtmlForms;
use Log1x\HtmlForms\Console\FormMakeCommand;
use Log1x\HtmlForms\View\Components\HtmlForms as HtmlFormsComponent;

class HtmlFormsServiceProvider extends ServiceProvider
Expand All @@ -28,6 +29,10 @@ public function register()
*/
public function boot()
{
$this->commands([
FormMakeCommand::class,
]);

$this->loadViewsFrom(__DIR__ . '/../resources/views', 'HtmlForms');

$this->callAfterResolving(BladeCompiler::class, function ($view) {
Expand Down
2 changes: 2 additions & 0 deletions src/View/Components/HtmlForms.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public function __construct(
$hidden = null
) {
$this->form = hf_get_form($form);
$this->hidden = $hidden;

$this->form->attributes = collect($this->form->messages)->merge(
collect($messages)->keyBy(function ($value, $key) {
return Str::snake($key);
Expand Down

0 comments on commit 4532745

Please sign in to comment.