Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions docs/methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,21 @@ if (kirby()->request()->is('POST')) {
}
}
```

## withoutCSRF()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to call this disableCsrfProtection() to make sure that you are disabling an important protection mechanism.


Don't validate CSRF.

Return: `Form`

Allow the form to be processed without validating CSRF:

```php
use Uniform\Form;

$form = new Form;
if (kirby()->request()->is('POST')) {
$form->withoutCSRF()
->emailAction([/* action options */]);
}
```
23 changes: 22 additions & 1 deletion src/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ class Form extends BaseForm
* @var boolean
*/
protected $shouldValidate;

/**
* Indicates whether the validation should validate CSRF token
*
* @var boolean
*/
protected $shouldValidateCSRF;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rename to $shouldValidateCsrfToken.


/**
* Indicates whether any guards should still be executed
Expand Down Expand Up @@ -72,6 +79,7 @@ function __construct($rules = [], $sessionKey = null)
{
parent::__construct($rules, $sessionKey);
$this->shouldValidate = true;
$this->shouldValidateCSRF = true;
$this->shouldCallGuard = true;
$this->shouldRedirect = true;
$this->shouldFlash = true;
Expand Down Expand Up @@ -114,6 +122,19 @@ public function withoutFlashing()

return $this;
}

/**
* Don't validate CSRF.
* ⚠️ Not recommended — know what you're doing
*
* @return Form
*/
public function withoutCSRF()
{
$this->shouldValidateCSRF = false;

return $this;
}

/**
* Check if the form was executed successfully.
Expand All @@ -134,7 +155,7 @@ public function validate()
{
$this->shouldValidate = false;

if (parent::validates()) {
if (parent::validates($this->shouldValidateCSRF)) {
$this->success = true;
} else {
$this->fail();
Expand Down