-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[develop]:+ Form example including rules and messages
- Loading branch information
Showing
4 changed files
with
104 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php declare(strict_types=1); | ||
/** | ||
* Copyright © Willem Poortman 2021-present. All rights reserved. | ||
* | ||
* Please read the README and LICENSE files for more | ||
* details on copyrights and license information. | ||
*/ | ||
|
||
namespace Magewirephp\MagewireExamples\Magewire; | ||
|
||
class Form extends \Magewirephp\Magewire\Component\Form | ||
{ | ||
// Nested properties. | ||
public $customer = [ | ||
'credentials' => [ | ||
'firstname' => '', | ||
'lastname' => '', | ||
'email' => '', | ||
] | ||
]; | ||
|
||
// Rules for nested properties. | ||
protected $rules = [ | ||
'customer.credentials.firstname' => 'required|min:2', | ||
'customer.credentials.lastname' => 'required|min:2', | ||
'customer.credentials.email' => 'required|email', | ||
]; | ||
|
||
// :attribute & :value are available within each message. | ||
protected $messages = [ | ||
'customer.credentials.firstname:required' => 'Your firstname is required', | ||
'customer.credentials.firstname:min' => 'Your firstname minimum is 2', | ||
|
||
'customer.credentials.lastname:required' => 'Your lastname is required', | ||
'customer.credentials.lastname:min' => 'Your lastname minimum is 2', | ||
|
||
'customer.credentials.email:required' => 'Your email is required', | ||
'customer.credentials.email:email' => 'Your email is not valid email', | ||
]; | ||
|
||
public function save() | ||
{ | ||
$this->validate(); | ||
// Will only dispatch when all customer credentials are valid. | ||
$this->dispatchSuccessMessage('Validation success!'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php declare(strict_types=1); | ||
/** | ||
* Copyright © Willem Poortman 2021-present. All rights reserved. | ||
* | ||
* Please read the README and LICENSE files for more | ||
* details on copyrights and license information. | ||
*/ | ||
|
||
/** @var \Magewirephp\MagewireExamples\Magewire\Form $magewire */ | ||
$magewire = $block->getMagewire(); | ||
?> | ||
<div id="form" class="pb-72"> | ||
<h2 class="text-xl font-bold mb-3 pb-2 border-b"> | ||
<?= __('Form') ?> | ||
</h2> | ||
|
||
<form wire:submit.prevent="save"> | ||
<div class="flex flex-col mb-6 gap-4"> | ||
<input type="text" | ||
class="form-input flex-grow disabled:opacity-50" | ||
placeholder="<?= __('Firstname') ?>" | ||
wire:model.defer="customer.credentials.firstname"/> | ||
<?php if ($magewire->hasError('customer.credentials.firstname')): ?> | ||
<span class="text-red-800"><?= $magewire->getError('customer.credentials.firstname') ?></span> | ||
<?php endif ?> | ||
|
||
<input type="text" | ||
class="form-input flex-grow disabled:opacity-50" | ||
placeholder="<?= __('Lastname') ?>" | ||
wire:model.defer="customer.credentials.lastname"/> | ||
<?php if ($magewire->hasError('customer.credentials.lastname')): ?> | ||
<span class="text-red-800"><?= $magewire->getError('customer.credentials.lastname') ?></span> | ||
<?php endif ?> | ||
|
||
<input type="text" | ||
class="form-input flex-grow disabled:opacity-50" | ||
placeholder="<?= __('Email') ?>" | ||
wire:model.defer="customer.credentials.email"/> | ||
<?php if ($magewire->hasError('customer.credentials.email')): ?> | ||
<span class="text-red-800"><?= $magewire->getError('customer.credentials.email') ?></span> | ||
<?php endif ?> | ||
</div> | ||
|
||
<button type="submit" class="btn btn-primary text-center float-right"> | ||
<?= __('Validate') ?> | ||
</button> | ||
</form> | ||
</div> |