Merge pull request #4046 from mryoaz/contactform-fix

Fix Contactform UX and validation (fixes #2239 and #2240)
This commit is contained in:
daniel 2023-01-08 12:32:02 -07:00 committed by GitHub
commit 61987da3ea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 2 deletions

View file

@ -6,6 +6,7 @@ use Illuminate\Http\Request;
use Auth; use Auth;
use App\Contact; use App\Contact;
use App\Jobs\ContactPipeline\ContactPipeline; use App\Jobs\ContactPipeline\ContactPipeline;
use App\Rules\MaxMultiLine;
class ContactController extends Controller class ContactController extends Controller
{ {
@ -21,7 +22,7 @@ class ContactController extends Controller
abort_if(!Auth::check(), 403); abort_if(!Auth::check(), 403);
$this->validate($request, [ $this->validate($request, [
'message' => 'required|string|min:5|max:500', 'message' => ['required', 'string', 'min:5', new MaxMultiLine('500')],
'request_response' => 'string|max:3' 'request_response' => 'string|max:3'
]); ]);

View file

@ -0,0 +1,34 @@
<?php
namespace App\Rules;
use Illuminate\Support\Str;
use Illuminate\Contracts\Validation\InvokableRule;
class MaxMultiLine implements InvokableRule
{
private $maxCharacters;
public function __construct($maxCharacters)
{
$this->maxCharacters = $maxCharacters;
}
/**
* Run the validation rule.
*
* @param string $attribute
* @param mixed $value
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
* @return void
*/
public function __invoke($attribute, $value, $fail)
{
$realCount = Str::length($value) - Str::substrCount($value, "\r\n");
if($realCount > $this->maxCharacters)
{
$fail('validation.max.string')->translate(['max' => $this->maxCharacters]);
}
}
}

View file

@ -24,7 +24,7 @@
@csrf @csrf
<div class="form-group"> <div class="form-group">
<label for="input1" class="font-weight-bold">Message</label> <label for="input1" class="font-weight-bold">Message</label>
<textarea class="form-control" id="input1" name="message" rows="6" placeholder=""></textarea> <textarea class="form-control" id="input1" name="message" rows="6" placeholder="" maxlength="500" required>{{old('message')}}</textarea>
<span class="form-text text-muted text-right msg-counter">0/500</span> <span class="form-text text-muted text-right msg-counter">0/500</span>
</div> </div>
<div class="form-group form-check"> <div class="form-group form-check">