Skip to content

Commit

Permalink
Userdefined form Updates (#3)
Browse files Browse the repository at this point in the history
* Userdefined form Updates

- Added timer to be configurable inside silverstripe admin
- Updated error messages to only show 1 time even if both field types are added
- Update to allow for the custom error messages to display

Co-authored-by: SnowB1 <[email protected]>
  • Loading branch information
SnowB1 and SnowB1 authored Jul 29, 2022
1 parent 6f65c52 commit 5d98fbf
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 37 deletions.
20 changes: 16 additions & 4 deletions src/Forms/EditableHoneypotField.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use SilverStripe\Forms\TextField;
use SilverStripe\UserForms\Model\EditableFormField;
use SilverStripe\ORM\UnsavedRelationList;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\Injector\Injector;

class EditableHoneypotField extends EditableFormField
{
Expand All @@ -21,11 +23,15 @@ class EditableHoneypotField extends EditableFormField
/**/
public function getFormField()
{
// Clear Any existing errors
$Request = Injector::inst()->get(HTTPRequest::class);
$Session = $Request->getSession();
$Session->clear('spam-protection-error-exists');
//
$field = HoneypotField::create($this->Name, "", null)->setFieldHolderTemplate('Form\\SpamFieldHolder');
$this->doUpdateFormField($field);
$field = HoneypotField::create($this->Name, "", null)->setFieldHolderTemplate('Form\\SpamFieldHolder');
$this->doUpdateFormField($field);
//
return $field;
return $field;
}
/**
* @param FormField $field
Expand Down Expand Up @@ -69,6 +75,12 @@ public function showInReports()
*/
protected function updateFormField($field)
{
parent::updateFormField($field);
// Add custom error
if ($this->CustomErrorMessage <> ""){
$field->setAttribute('data-custommsg', (string) $this->CustomErrorMessage);
}
//
parent::updateFormField($field);
}

}
68 changes: 48 additions & 20 deletions src/Forms/EditableTimerField.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

use Werkbot\SpamProtection\TimerField;
use SilverStripe\Forms\FormField;
use SilverStripe\Forms\TextField;
use SilverStripe\Forms\NumericField;
use SilverStripe\UserForms\Model\EditableFormField;
use SilverStripe\ORM\UnsavedRelationList;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\Injector\Injector;

class EditableTimerField extends EditableFormField
{
Expand All @@ -15,20 +17,25 @@ class EditableTimerField extends EditableFormField
private static $plural_name = 'Timer Spam Protection Fields';
private static $table_name = 'EditableTimerField';
/**/
private static $db = [];
private static $db = [
'TimeNotABot' => 'Int'
];
/**
* @var FormField
*/
protected $formField = null;
/**/
public function getFormField()
{
//
$field = TimerField::create($this->Name, $this->Title, time())->setFieldHolderTemplate('Form\\SpamFieldHolder');
$this->doUpdateFormField($field);
//
return $field;
}
public function getFormField(){
// Clear Any existing errors
$Request = Injector::inst()->get(HTTPRequest::class);
$Session = $Request->getSession();
$Session->clear('spam-protection-error-exists');
//
$field = TimerField::create($this->Name, $this->Title, time())->setFieldHolderTemplate('Form\\SpamFieldHolder');
$this->doUpdateFormField($field);
//
return $field;
}
/**
* @param FormField $field
* @return self
Expand All @@ -43,17 +50,38 @@ public function setFormField(FormField $field)
*
* {@inheritDoc}
*/
public function getCMSFields()
{
//
$fields = parent::getCMSFields();
//
if ($this->Parent()->Fields() instanceof UnsavedRelationList) {
return $fields;
}
//
return $fields;
public function getCMSFields() {
$this->beforeUpdateCMSFields(function ($fields) {
$fields->addFieldsToTab(
'Root.Main',
[
NumericField::create(
'TimeNotABot',
'Minimum amount of time to fill out the form'
)->setDescription('Minimum time in seconds it takes to fill out the form, anything less is considered a bot')
]
);
});
//
return parent::getCMSFields();
}
/**
* Updates a formfield with the additional metadata specified by this field
*
* @param FormField $field
*/
protected function updateFormField($field)
{
parent::updateFormField($field);
// Add custom time
if (is_numeric($this->TimeNotABot) && $this->TimeNotABot > 0) {
$field->setAttribute('data-rule-customtime', (int) $this->TimeNotABot);
}
// Add custom error
if ($this->CustomErrorMessage <> ""){
$field->setAttribute('data-custommsg', (string) $this->CustomErrorMessage);
}
}
/**/
public function getRequired()
{
Expand Down
32 changes: 25 additions & 7 deletions src/Forms/HoneypotField.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,35 @@ public function validate($validator)
$StringID = explode(" ", $attributes);
$ID = substr($StringID[0], strrpos($StringID[0], '_') + 1);
$ID = rtrim($ID, '"');
//
$Attributes = $this->getAttributes();
$Request = Injector::inst()->get(HTTPRequest::class);
$Session = $Request->getSession();
//
if (!empty($this->Value())) {
// Not expecting any value
$validator->validationError(
if (!$Session->get('spam-protection-error-exists')) {
$Session->set('spam-protection-error-exists', true);
// Add custom error message
if(isset($Attributes['data-custommsg']) && $Attributes['data-custommsg'] <> ""){
$validator->validationError(
$this->Name,
_t('Werkbot\SpamProtection\Honeypot.INVALID', 'There was an error submitting this form. Please try again.')
);
if (intval($ID !== 0)) {
$form->sessionMessage(_t('Werkbot\SpamProtection\Honeypot.INVALID', 'There was an error submitting this form. Please try again.'), 'bad');
$Attributes['data-custommsg']
);
if (intval($ID !== 0)) {
$form->sessionMessage($Attributes['data-custommsg'], 'bad');
}
} else {
// Not expecting any value
$validator->validationError(
$this->Name,
_t('Werkbot\SpamProtection\Honeypot.INVALID', 'There was an error submitting this form. Please try again.')
);
if (intval($ID !== 0)) {
$form->sessionMessage(_t('Werkbot\SpamProtection\Honeypot.INVALID', 'There was an error submitting this form. Please try again.'), 'bad');
}
}
return false;
}
return false;
}
//
return true;
Expand Down
28 changes: 22 additions & 6 deletions src/Forms/TimerField.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Werkbot\SpamProtection;

use SilverStripe\Forms\TextField;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\Injector\Injector;

class TimerField extends TextField
{
Expand All @@ -22,17 +24,31 @@ public function Field($properties = array())
/**/
public function validate($validator)
{
$Attributes = $this->getAttributes();
$Request = Injector::inst()->get(HTTPRequest::class);
$Session = $Request->getSession();
//
$Timer = (($this->config()->time_not_bot) ? $this->config()->time_not_bot : $this->time_not_bot);
$Timer = ((isset($Attributes['data-rule-customtime'])) ? $Attributes['data-rule-customtime'] : (($this->config()->time_not_bot) ? $this->config()->time_not_bot : $this->time_not_bot));
$CurrentTime = time();
// Compare time difference with allowed time difference
if (empty($this->Value()) || !is_numeric($this->Value()) || (($CurrentTime - $this->Value()) < $Timer)) {
// Not the expected value, set error
$validator->validationError(
if ((empty($this->Value()) || !is_numeric($this->Value()) || (($CurrentTime - $this->Value()) < $Timer))) {
if (!$Session->get('spam-protection-error-exists')) {
// Set Session
$Session->set('spam-protection-error-exists', true);
//
if(isset($Attributes['data-custommsg']) && $Attributes['data-custommsg'] <> ""){
$validator->validationError(
$this->Name,
$Attributes['data-custommsg']
);
} else {
$validator->validationError(
$this->Name,
_t('Werkbot\SpamProtection\Timer.INVALID', 'There was an error submitting this form. Please try again.')
);
return false;
);
}
}
return false;
}
//
return true;
Expand Down

0 comments on commit 5d98fbf

Please sign in to comment.