Skip to content

Commit d3dffd8

Browse files
jonwaldsteinJon Waldstein
and
Jon Waldstein
authored
Feature: add SecurityChallenge Field to Fields API (#7865)
Co-authored-by: Jon Waldstein <[email protected]>
1 parent 589e978 commit d3dffd8

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/DonationForms/DataTransferObjects/ValidationRouteData.php

+13-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use Give\DonationForms\Models\DonationForm;
88
use Give\Framework\FieldsAPI\Actions\CreateValidatorFromFormFields;
99
use Give\Framework\FieldsAPI\Exceptions\NameCollisionException;
10+
use Give\Framework\FieldsAPI\Field;
11+
use Give\Framework\FieldsAPI\SecurityChallenge;
1012
use Give\Framework\Http\Response\Types\JsonResponse;
1113
use Give\Framework\Support\Contracts\Arrayable;
1214
use WP_Error;
@@ -44,6 +46,7 @@ public static function fromRequest(array $requestData): self
4446
* compares the request against the individual fields,
4547
* their types and validation rules.
4648
*
49+
* @unreleased updated to exclude security challenge fields during pre-validation
4750
* @since 3.22.0 added additional validation for form validity, added givewp_donation_form_fields_validated action
4851
* @since 3.0.0
4952
*
@@ -60,8 +63,8 @@ public function validate(): JsonResponse
6063
throw new DonationFormForbidden();
6164
}
6265

63-
$formFields = array_filter($form->schema()->getFields(), static function ($field) use ($request) {
64-
return array_key_exists($field->getName(), $request);
66+
$formFields = array_filter($form->schema()->getFields(), function ($field) use ($request) {
67+
return array_key_exists($field->getName(), $request) && !$this->isSecurityChallengeField($field);
6568
});
6669

6770
$validator = (new CreateValidatorFromFormFields())($formFields, $request);
@@ -135,4 +138,12 @@ public function toArray(): array
135138
{
136139
return get_object_vars($this);
137140
}
141+
142+
/**
143+
* @unreleased
144+
*/
145+
protected function isSecurityChallengeField(Field $field): bool
146+
{
147+
return is_subclass_of($field, SecurityChallenge::class);
148+
}
138149
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Give\Framework\FieldsAPI;
4+
5+
6+
/**
7+
* Security challenge fields are a special snowflake.
8+
* They can typically only be validated once on the server.
9+
* Extending this abstract field will ensure that the field is not validated on the server
10+
* before the form is fully submitted, avoiding pre-validation conflicting endpoints.
11+
*
12+
* @unreleased
13+
*/
14+
abstract class SecurityChallenge extends Field
15+
{
16+
}

0 commit comments

Comments
 (0)