Enhance redirect URL handling and MentionParser functionality (#639)
- Updated the `redirect_url` validation in `UserFormRequest` to accept strings instead of just max length. - Modified `MentionParser` to include a `urlFriendlyOutput` method, allowing for URL encoding of special characters in parsed values. - Adjusted the `PublicFormController` to utilize the new `urlFriendlyOutput` method for redirect URLs. - Created a migration to change the `redirect_url` field type in the database from string to text, accommodating longer URLs. - Added tests to ensure proper handling of long redirect URLs and the functionality of the new URL-friendly output feature in `MentionParser`. This update improves the flexibility and robustness of form handling and URL processing.
This commit is contained in:
@@ -116,7 +116,7 @@ class PublicFormController extends Controller
|
||||
return ['id' => $key, 'value' => $value];
|
||||
})->values()->all();
|
||||
|
||||
$redirectUrl = ($form->redirect_url) ? (new MentionParser($form->redirect_url, $formattedData))->parseAsText() : null;
|
||||
$redirectUrl = ($form->redirect_url) ? (new MentionParser($form->redirect_url, $formattedData))->urlFriendlyOutput()->parseAsText() : null;
|
||||
|
||||
if ($redirectUrl && !filter_var($redirectUrl, FILTER_VALIDATE_URL)) {
|
||||
$redirectUrl = null;
|
||||
|
||||
@@ -55,7 +55,7 @@ abstract class UserFormRequest extends \Illuminate\Foundation\Http\FormRequest
|
||||
're_fillable' => 'boolean',
|
||||
're_fill_button_text' => 'string|min:1|max:50',
|
||||
'submitted_text' => 'string|max:2000',
|
||||
'redirect_url' => 'nullable|max:255',
|
||||
'redirect_url' => 'nullable|string',
|
||||
'database_fields_update' => 'nullable|array',
|
||||
'max_submissions_count' => 'integer|nullable|min:1',
|
||||
'max_submissions_reached_text' => 'string|nullable',
|
||||
|
||||
@@ -9,6 +9,7 @@ class MentionParser
|
||||
{
|
||||
private $content;
|
||||
private $data;
|
||||
private $urlFriendly = false;
|
||||
|
||||
public function __construct($content, $data)
|
||||
{
|
||||
@@ -16,6 +17,12 @@ class MentionParser
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
public function urlFriendlyOutput(bool $enable = true): self
|
||||
{
|
||||
$this->urlFriendly = $enable;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function parse()
|
||||
{
|
||||
$doc = new DOMDocument();
|
||||
@@ -40,7 +47,7 @@ class MentionParser
|
||||
$value = $this->getData($fieldId);
|
||||
|
||||
if ($value !== null) {
|
||||
$textNode = $doc->createTextNode(is_array($value) ? implode(', ', $value) : $value);
|
||||
$textNode = $doc->createTextNode(is_array($value) ? implode($this->urlFriendly ? ',+' : ', ', $value) : $value);
|
||||
$element->parentNode->replaceChild($textNode, $element);
|
||||
} elseif ($fallback) {
|
||||
$textNode = $doc->createTextNode($fallback);
|
||||
@@ -127,7 +134,13 @@ class MentionParser
|
||||
$value = collect($this->data)->firstWhere('id', $fieldId)['value'] ?? null;
|
||||
|
||||
if (is_object($value)) {
|
||||
return (array) $value;
|
||||
$value = (array) $value;
|
||||
}
|
||||
|
||||
if ($this->urlFriendly && $value !== null) {
|
||||
return is_array($value)
|
||||
? array_map('urlencode', $value)
|
||||
: urlencode($value);
|
||||
}
|
||||
|
||||
return $value;
|
||||
|
||||
Reference in New Issue
Block a user