Skip to content

Commit

Permalink
Merge branch 'release/2.0.8'
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster committed Dec 13, 2016
2 parents 07f1d12 + 9cf1cc8 commit fe62dc9
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 32 deletions.
17 changes: 16 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
# v2.0.8
## 12/13/2016

1. [](#new)
* RC released as stable
* Added a new `honeypot` field for form anti-spam protection

# v2.0.8-rc.1
## 11/26/2016

1. [](#bugfix)
* Fixed Forms 2.0 changes for registration form [#101](https://github.com/getgrav/grav-plugin-form/issues/101)
* Fixed errant reference to Grav DI container in Form#getPagePathFromToken [#105](https://github.com/getgrav/grav-plugin-form/issues/105)
* Fixed issue with spacer fields being displayed first, not in order [#104](https://github.com/getgrav/grav-plugin-form/issues/104)

# v2.0.7
## 11/17/2016

1. [](#improved)
* Added method to set all data in a form
* Added params to form action URL
* Added ability to add ids to buttons and to set them disabled
* Added ability to add ids to buttons and to set them disabled
1. [](#bugfix)
* Moved Files Upload GC logic to function in front-end only

Expand Down
4 changes: 2 additions & 2 deletions blueprints.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Form
version: 2.0.7
version: 2.0.8
description: Enables the forms handling
icon: check-square
author:
Expand All @@ -12,7 +12,7 @@ bugs: https://github.com/getgrav/grav-plugin-form/issues
license: MIT

dependencies:
- { name: grav, version: '>=1.1.4' }
- { name: grav, version: '>=1.1.9' }

form:
validation: strict
Expand Down
53 changes: 29 additions & 24 deletions classes/form.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ public function __construct(Page $page, $name = null, $form = null)
if ($form) {
$this->items = $form;
} else {
$this->items = $header->form; // for backwards compatibility
if (isset($header->form)) {
$this->items = $header->form; // for backwards compatibility
}
}

// Add form specific rules.
Expand Down Expand Up @@ -183,33 +185,35 @@ public function reset()
$grav = Grav::instance();

// Fix naming for fields (presently only for toplevel fields)
foreach ($this->items['fields'] as $key => $field) {
// default to text if not set
if (!isset($field['type'])) {
$field['type'] = 'text';
}
if (isset($this->items['fields'])) {
foreach ($this->items['fields'] as $key => $field) {
// default to text if not set
if (!isset($field['type'])) {
$field['type'] = 'text';
}

$types = $grav['plugins']->formFieldTypes;
$types = $grav['plugins']->formFieldTypes;

// manually merging the field types
if ($types !== null && key_exists($field['type'], $types)) {
$field += $types[$field['type']];
}
// manually merging the field types
if ($types !== null && key_exists($field['type'], $types)) {
$field += $types[$field['type']];
}

// BC for old style of array style field definitions
if (is_numeric($key) && isset($field['name'])) {
unset($this->items['fields'][$key]);
$key = $field['name'];
}
// BC for old style of array style field definitions
if (is_numeric($key) && isset($field['name'])) {
array_splice($this->items['fields'], $key);
$key = $field['name'];
}

// Add name based on key if not already set
if (!isset($field['name'])) {
$field['name'] = $key;
}
// Add name based on key if not already set
if (!isset($field['name'])) {
$field['name'] = $key;
}

// set any modifications back on the fields array
$this->items['fields'][$key] = $field;
// set any modifications back on the fields array
$this->items['fields'][$key] = $field;

}
}

$items = $this->items;
Expand Down Expand Up @@ -600,6 +604,7 @@ public function post()

public function getPagePathFromToken($path)
{
$grav = Grav::instance();
$path_parts = pathinfo($path);

$basename = '';
Expand All @@ -619,12 +624,12 @@ public function getPagePathFromToken($path)
// page@
$parts = explode(':', $path);
$route = $parts[1];
$page = $this->grav['page']->find($route);
$page = $grav['page']->find($route);
} elseif ($matches[3]) {
// theme@
$parts = explode(':', $path);
$route = $parts[1];
$theme = str_replace(ROOT_DIR, '', $this->grav['locator']->findResource("theme://"));
$theme = str_replace(ROOT_DIR, '', $grav['locator']->findResource("theme://"));

return $theme . $route . $basename;
}
Expand Down
37 changes: 32 additions & 5 deletions form.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Grav\Plugin;

use Grav\Common\Data\ValidationException;
use Grav\Common\Page\Page;
use Grav\Common\Page\Pages;
use Grav\Common\Plugin;
Expand Down Expand Up @@ -68,9 +69,10 @@ public function onPluginsInitialized()
}

$this->enable([
'onPageProcessed' => ['onPageProcessed', 0],
'onPagesInitialized' => ['onPagesInitialized', 0],
'onTwigInitialized' => ['onTwigInitialized', 0],
'onPageProcessed' => ['onPageProcessed', 0],
'onPagesInitialized' => ['onPagesInitialized', 0],
'onTwigInitialized' => ['onTwigInitialized', 0],
'onFormValidationProcessed' => ['onFormValidationProcessed', 0],
]);

// Get and set the cache of forms if it exists
Expand Down Expand Up @@ -156,8 +158,8 @@ public function onPagesInitialized()
// Create form
$this->form = new Form($page);
$this->enable([
'onFormProcessed' => ['onFormProcessed', 0],
'onFormValidationError' => ['onFormValidationError', 0]
'onFormProcessed' => ['onFormProcessed', 0],
'onFormValidationError' => ['onFormValidationError', 0]
]);
$this->form->post();
}
Expand Down Expand Up @@ -200,6 +202,10 @@ public function onPagesInitialized()
$form->post();
$submitted = true;
}
} elseif (isset($this->grav['page']->header()->form)) {
$form = new Form($this->grav['page']);
$form->post();
$submitted = true;
}
}

Expand Down Expand Up @@ -445,6 +451,24 @@ public function onFormProcessed(Event $event)
}
}

/**
* Custom field logic can go in here
*
* @param Event $event
*/
public function onFormValidationProcessed(Event $event)
{
$form = $event['form'];

foreach ($form->fields as $key => $field) {
if ($field['type'] == 'honeypot') {
if (!empty($field['value'])) {
throw new ValidationException('Are you a bot?');
}
}
}
}

/**
* Handle form validation error
*
Expand Down Expand Up @@ -596,6 +620,9 @@ public function getForm($data = null)
$forms = $this->forms[$page_route];
$first_form = array_shift($forms);
$form_name = $first_form['name'];
} else {
//No form on this route. Try looking up in the current page first
return new Form($this->grav['page']);
}
}

Expand Down
1 change: 1 addition & 0 deletions templates/forms/fields/honeypot/honeypot.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<input type="text" style="display:none;" name="{{ (scope ~ field.name)|fieldName }}" value="{{ value|join(', ') }}" />

0 comments on commit fe62dc9

Please sign in to comment.