Skip to content

Commit

Permalink
Merge pull request #35 from garygreen/remove-formbuilder-macro
Browse files Browse the repository at this point in the history
Remove formbuilder macro, use the facade.
  • Loading branch information
msurguy committed May 14, 2015
2 parents 6d31740 + 9764efe commit 7dd8a95
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 33 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,23 @@ Next, add this line to 'providers' section of the app config file in `app/config

'Msurguy\Honeypot\HoneypotServiceProvider',

Add the honeypot facade:

'Honeypot' => 'Msurguy\Honeypot\HoneypotFacade'

At this point the package is installed and you can use it as follows.

## Usage :

Add the hidden DIV containing honeypot fields to your form by inserting `Form::honeypot` macro like this:
Add the honeypot catcher to your form by inserting `Honeypot::generate(..)` like this:

{{ Form::open('contact') }}
...
{{ Form::honeypot('my_name', 'my_time') }}
{{ Honeypot::generate('my_name', 'my_time') }}
...
{{ Form::close() }}

When the page containing `Form::honeypot` macro is rendered, the following HTML markup will be present (my_time field will contain an encrypted timestamp):
The `generate` method will output the following HTML markup (`my_time` field will contain an encrypted timestamp):

<div id="my_name_wrap" style="display:none;">
<input name="my_name" type="text" value="" id="my_name">
Expand Down
4 changes: 2 additions & 2 deletions src/Msurguy/Honeypot/Honeypot.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
class Honeypot {

/**
* Get the honey pot form HTML
* Generate a new honeypot and return the form HTML
* @param string $honey_name
* @param string $honey_time
* @return string
*/
public function getFormHTML($honey_name, $honey_time)
public function generate($honey_name, $honey_time)
{
// Encrypt the current time
$honey_time_encrypted = $this->getEncryptedTime();
Expand Down
28 changes: 0 additions & 28 deletions src/Msurguy/Honeypot/HoneypotServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php namespace Msurguy\Honeypot;

use Illuminate\Html\FormBuilder;
use Illuminate\Foundation\Application;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -52,9 +51,6 @@ public function boot()
// Add honeypot and honeytime custom validation rules
$validator->extend('honeypot', 'Msurguy\Honeypot\HoneypotValidator@validateHoneypot', $translator->get('honeypot::validation.honeypot'));
$validator->extend('honeytime', 'Msurguy\Honeypot\HoneypotValidator@validateHoneytime', $translator->get('honeypot::validation.honeytime'));

// Register the honeypot form macros
$this->registerFormMacro($this->isLaravelVersion(['4.0', '4.1']) ? $app['form'] : null);
});
}

Expand All @@ -68,30 +64,6 @@ public function provides()
return array('honeypot');
}

/**
* Register the honeypot form macro
*
* @param Illuminate\Html\FormBuilder|null $form
* @return void
*/
public function registerFormMacro(FormBuilder $form = null)
{
$honeypotMacro = function($honey_name, $honey_time) {
$honeypot = new Honeypot();
return $honeypot->getFormHTML($honey_name, $honey_time);
};

// Add a custom honeypot macro to Laravel's form
if ($form)
{
$form->macro('honeypot', $honeypotMacro);
}
else
{
FormBuilder::macro('honeypot', $honeypotMacro);
}
}

/**
* Determine if laravel starts with any of the given version strings
*
Expand Down

0 comments on commit 7dd8a95

Please sign in to comment.