Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Input Masking return empty string instead of null #751

Closed
devfaysal opened this issue Nov 26, 2024 · 3 comments · Fixed by alpinejs/alpine#4481
Closed

Input Masking return empty string instead of null #751

devfaysal opened this issue Nov 26, 2024 · 3 comments · Fixed by alpinejs/alpine#4481
Assignees

Comments

@devfaysal
Copy link

devfaysal commented Nov 26, 2024

Flux Version v1.0.24
Laravel Version 11.33.2

    #[Validate('nullable')]
    public $phone_number;

    public function update()
    {
        $this->validate();
        dd($this->phone_number);
    }
<form wire:submit="update">
          <flux:input wire:model="phone_number" label="Phone Number" mask="99999-999999" placeholder="Your phone number" />
          <flux:button type="submit" variant="primary" class="w-full">Update</flux:button>
  </form>

The phone number is an empty string when submitted without writing anything in the input
"" // app/Livewire/Test.php:16

If I remove the mask then it returns null
null // app/Livewire/Test.php:16

As Laravel uses convertEmptyStringsToNull by default, the null should be the correct value if the input does not have anything.

But the masking is doing something wrong.

@joshhanley
Copy link
Member

Here's a Volt component to make testing this easier. The first input has the mask and the second doesn't. If you click the update button you will see phone_number_masked is an empty string and phone_number is null.

<?php

use Livewire\Volt\Component;

new class extends Component {
    public $phone_number_masked;
    public $phone_number;

    public function update()
    {
        dd('masked', $this->phone_number_masked, 'not masked', $this->phone_number);
    }
};

?>

<flux:main>
    <form wire:submit="update">
        <flux:input wire:model="phone_number_masked" label="Phone Number" mask="99999-999999" placeholder="Your phone number masked" />
        <flux:input wire:model="phone_number" label="Phone Number" placeholder="Your phone number" />
        <flux:button type="submit" variant="primary" class="w-full">Update</flux:button>
    </form>
</flux:main>

@joshhanley
Copy link
Member

@devfaysal thanks for reporting! Turns out this wasn't an issue in Flux but instead it was a problem in Alpine's x-mask plugin. I've submitted a PR to Alpine with a fix alpinejs/alpine#4481. So going to close this issue.

@devfaysal
Copy link
Author

Thank you very much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants