Skip to content

Commit

Permalink
URL input validation rule (#223)
Browse files Browse the repository at this point in the history
  • Loading branch information
formsdev authored Oct 17, 2023
1 parent 25506f5 commit e6905b7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
3 changes: 2 additions & 1 deletion app/Http/Requests/AnswerFormRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Illuminate\Http\Request;
use App\Rules\ValidHCaptcha;
use App\Rules\ValidPhoneInputRule;
use App\Rules\ValidUrl;

class AnswerFormRequest extends FormRequest
{
Expand Down Expand Up @@ -171,7 +172,7 @@ private function getPropertyRules($property): array
$this->requestRules[$property['id'].'.*'] = [new StorageFile($this->maxFileSize, [], $this->form)];
return ['array'];
}
return ['url'];
return [new ValidUrl];
case 'files':
$allowedFileTypes = [];
if(!empty($property['allowed_file_types'])){
Expand Down
33 changes: 33 additions & 0 deletions app/Rules/ValidUrl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class ValidUrl implements Rule
{
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
// Define the regular expression to match the desired URL patterns
$pattern = '/^(https?:\/\/)?(www\.)?[\w.-]+\.\w+(:\d+)?(\/[^\s]*)?$/';

return preg_match($pattern, $value);
}

/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'The :attribute format is invalid.';
}
}

0 comments on commit e6905b7

Please sign in to comment.