URL input validation rule (#223)
This commit is contained in:
parent
25506f5d57
commit
e6905b7bb4
|
@ -12,6 +12,7 @@ use Illuminate\Validation\Rule;
|
|||
use Illuminate\Http\Request;
|
||||
use App\Rules\ValidHCaptcha;
|
||||
use App\Rules\ValidPhoneInputRule;
|
||||
use App\Rules\ValidUrl;
|
||||
|
||||
class AnswerFormRequest extends FormRequest
|
||||
{
|
||||
|
@ -171,7 +172,7 @@ class AnswerFormRequest extends FormRequest
|
|||
$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'])){
|
||||
|
|
|
@ -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.';
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue