Skip to content

Commit

Permalink
Update demo to use CSRF form (#30)
Browse files Browse the repository at this point in the history
* Update demo to use CSRF form
Update CSRF data handler to only hydrate value on generateInput, preventing null CSRF on hydrate been automatically corrected

* version bump
  • Loading branch information
MrEssex authored May 24, 2023
1 parent 9bf5aa5 commit 7cba1ac
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
8 changes: 5 additions & 3 deletions demo/index.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Packaged\Form\Csrf\CsrfForm;
use Packaged\Form\DataHandlers\BooleanDataHandler;
use Packaged\Form\DataHandlers\EmailDataHandler;
use Packaged\Form\DataHandlers\EnumDataHandler;
Expand All @@ -11,7 +12,6 @@
use Packaged\Form\DataHandlers\ReadOnlyDataHandler;
use Packaged\Form\DataHandlers\SecureTextDataHandler;
use Packaged\Form\DataHandlers\TextDataHandler;
use Packaged\Form\Form\Form;
use Packaged\Helpers\Arrays;
use Packaged\SafeHtml\SafeHtml;
use Packaged\Validate\Validators\ConfirmationValidator;
Expand All @@ -23,7 +23,7 @@

require('../vendor/autoload.php');

class DemoForm extends Form
class DemoForm extends CsrfForm
{
/**
* @var TextDataHandler
Expand Down Expand Up @@ -76,6 +76,8 @@ class DemoForm extends Form

protected function _initDataHandlers()
{
parent::_initDataHandlers();

$this->name = TextDataHandler::i()->addValidator(new StringValidator(2, 20));
$this->email = EmailDataHandler::i()
->setAutocomplete('email');
Expand Down Expand Up @@ -120,7 +122,7 @@ protected function _configureDataHandlers()
}

$data = array_merge($_POST, $_FILES);
$form = new DemoForm();
$form = new DemoForm('secret');
if(!empty($data))
{
$errors = $form->hydrate($data);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@packaged/form",
"version": "5.0.0",
"version": "5.1.0",
"main": "/index.js",
"repository": "[email protected]:packaged/form",
"author": "Tom Kay <[email protected]>",
Expand Down
27 changes: 27 additions & 0 deletions php/src/Csrf/CsrfDataHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Packaged\Form\Csrf;

use Packaged\Form\DataHandlers\HiddenDataHandler;
use Packaged\Ui\Html\HtmlElement;

class CsrfDataHandler extends HiddenDataHandler
{
Expand Down Expand Up @@ -86,4 +87,30 @@ protected function _setupValidators()
{
$this->addValidator(new CsrfValidator($this->_generatePassword(), $this->_expiryMins));
}

public function getValue($default = null, $withDefault = false, $formatValue = true)
{
return parent::getValue($default, $withDefault, $formatValue);
}

protected function _generateInput(): HtmlElement
{
$ele = $this->_createBaseElement();
$ele->addAttributes(
[
'name' => $this->getName(),
'id' => $this->getId(),
'value' => $this->formatValue($this->getValue(null, true)),
'placeholder' => $this->getPlaceholder(),
]
);

$autocomplete = $this->getAutocomplete();
if(!empty($autocomplete))
{
$ele->setAttribute('autocomplete', $autocomplete);
}

return $ele;
}
}

0 comments on commit 7cba1ac

Please sign in to comment.