Skip to content

Instantly share code, notes, and snippets.

@alistaircol
Last active April 6, 2022 08:28
Show Gist options
  • Save alistaircol/72a7b1988fb0f8aed041f5e498bd31c9 to your computer and use it in GitHub Desktop.
Save alistaircol/72a7b1988fb0f8aed041f5e498bd31c9 to your computer and use it in GitHub Desktop.
Fuck Livewire

Some stupid annoying things I've encountered with Livewire.

In some code examples, imagine we are building some property management platform.

mount

Want to pass some nullable property? Is it a model? Well you better not type-hint it.

Wrong!:

# user manage their property, but maybe they don't have one
# on the system, so instead we can show find/claim instead of manage..
public function mount(User $user, ?Property $property)

For some inexplicable reason it decides to throw an exception.

ImplicitlyBoundMethod::getImplicitBinding

Correct:

public function mount(User $user, $property)

Component Validation

Customising the value in the validator message is kinda ugly (not entirely Livewire's doing).

$after = Reminders::getMinDate(now()->toImmutable());

$rules['reminder_date'][] = 'after_or_equal:' . $after->format('Y-m-d');

// custom property (or function) livewire looks for to call `setCustomValues` or something on the validator instance
$this->validationCustomValues = [
   'reminder_date' => [$after->format('Y-m-d') => $after->format('d/m/Y')],
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment