PHP code example of itxshakil / laravel-form-shield
1. Go to this page and download the library: Download itxshakil/laravel-form-shield library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
itxshakil / laravel-form-shield example snippets
use Itxshakil\FormShield\Concerns\HasSpamVerdict;
class Inquiry extends Model
{
use HasSpamVerdict;
}
use Itxshakil\FormShield\Facades\FormShield;
public function store(Request $request)
{
$data = $request->validate([
'name' => ['ct($request, 'contact'); // after validation
if ($verdict->isExpired) {
return back()->withInput()->withErrors(['message' => __('form-shield::messages.expired')]);
}
$inquiry = Inquiry::createWithVerdict($data, $verdict);
if (! $verdict->isSpam) {
Mail::to('[email protected]')->queue(new NewInquiry($inquiry));
}
return to_route('contact.thanks'); // the same response either way
}
$verdict = FormShield::inspect($request, 'contact');
FormShield::record($request, $verdict); // or set FORM_SHIELD_STORE=true to log every inspection
if (! $verdict->isSpam) {
Mail::to('[email protected]')->queue(new ContactMessage($data));
}
use Itxshakil\FormShield\Enums\VerdictStatus;
return match ($verdict->status) {
VerdictStatus::Clean, VerdictStatus::Spam => to_route('contact.thanks'),
VerdictStatus::Expired => back()->withInput()->withErrors(['message' => __('form-shield::messages.expired')]),
VerdictStatus::RateLimited => abort(429),
};
$inquiry->markAsHam(auth()->user()); // a false positive
$inquiry->markAsSpam(auth()->user()); // a miss
use Itxshakil\FormShield\Rules\NoSpamContent;
'message' => ['
Route::post('/contact', ContactController::class)->middleware('form-shield:contact');
// in the controller
$verdict = $request->spamVerdict();
use Itxshakil\FormShield\Livewire\WithFormShield;
class ContactForm extends Component
{
use WithFormShield;
public string $email = '';
public string $message = '';
public function save(): void
{
$verdict = $this->inspectFormShield('contact', ['email' => $this->email, 'message' => $this->message]);
// ...
}
}
use Itxshakil\FormShield\Testing\InteractsWithFormShield;
class ContactTest extends TestCase
{
use InteractsWithFormShield;
public function test_a_visitor_can_send_a_message(): void
{
$this->post('/contact', [...$data, ...$this->formShieldFields()])->assertRedirect('/thanks');
}
public function test_spam_is_quarantined(): void
{
FormShield::fake()->flagAll();
$this->post('/contact', $data);
Mail::assertNothingQueued();
}
}