Last active
December 10, 2021 11:36
-
-
Save calvez/4ac444ea63f2e99060c57a597f077311 to your computer and use it in GitHub Desktop.
livewire example of emit throw errors
This file contains hidden or 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
<?php | |
/** | |
* | |
* Save domain or wildcard | |
* regex from this crazy: | |
* wildcard: *.wilddomain.hu | |
* https://www.regextester.com/102477 | |
* | |
* validator: | |
* https://laravel-livewire.com/docs/2.x/input-validation | |
*/ | |
public function save() | |
{ | |
try { | |
$regex_full = '/^((\*)|((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)|((\*\.)?([a-zA-Z0-9-]+\.){0,5}[a-zA-Z0-9-][a-zA-Z0-9-]+\.[a-zA-Z]{2,63}?))$/'; | |
$this->validate([ | |
'form.domain' => ['required', 'regex:' . $regex_full], | |
]); | |
} catch (ValidationException $e) { | |
$validator = $e->validator; | |
//dd($validator); | |
$type = 'error'; | |
$message = 'A domain formátuma érvénytelen.'; | |
$this->emitTo('kiddomain.notification-component', 'flash_message', $type, $message); | |
throw $e; | |
} | |
$info['success'] = ''; | |
DB::beginTransaction(); | |
try { | |
$query = [ | |
"kid_id" => $this->kid->id, | |
"domain" => $this->form["domain"], | |
"is_white" => $this->form["is_white"], | |
"is_wildcard" => $this->form["is_wildcard"], | |
]; | |
$condition = [ | |
"kid_id" => $this->kid->id, | |
"domain" => $this->form["domain"] | |
]; | |
$info["domain"] = KidDomain::updateOrCreate($condition, $query); | |
DB::commit(); | |
$info['success'] = true; | |
} catch (\Exception $e) { | |
DB::rollback(); | |
$info['error'] = $e; | |
} | |
if ($info['success']) { | |
$type = 'success'; | |
$message = 'Új domain hozzáadva.'; | |
} else { | |
$type = 'error'; | |
$message = 'Valami hiba történt a mentés közben.'; | |
} | |
$this->emitTo('kiddomain.notification-component', 'flash_message', $type, $message); | |
$this->emitTo('kiddomain.list-component', 'load_list'); | |
} |
This file contains hidden or 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
<?php | |
namespace App\Http\Livewire\Kiddomain; | |
use Livewire\Component; | |
use App\KidDomain; | |
class ListComponent extends Component | |
{ | |
public $kidDomains = []; | |
public $kid; | |
public $listeners = [ | |
"load_list" => "loadList" | |
]; | |
public function mount() | |
{ | |
$this->loadList(); | |
} | |
public function loadList() | |
{ | |
$kidDomains = KidDomain::where('kid_id', $this->kid->id)->orderBy('created_at', 'desc')->get(); | |
$this->kidDomains = $kidDomains; | |
} | |
public function render() | |
{ | |
return view('livewire.kiddomain.list-component'); | |
} | |
} |
This file contains hidden or 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
<?php | |
namespace App\Http\Livewire\Kiddomain; | |
use Livewire\Component; | |
class NotificationComponent extends Component | |
{ | |
public $listeners = [ | |
"flash_message" => "flashMessage" | |
]; | |
public function flashMessage($type, $msg) | |
{ | |
session()->flash($type, $msg); | |
} | |
public function render() | |
{ | |
return view('livewire.kiddomain.notification-component'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment