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

[Bug]: Componente Money não está atualizandocorretamente os valores no Banco - Filament V3. #9

Open
mclgms opened this issue Aug 24, 2023 · 7 comments
Assignees
Labels
bug Something isn't working

Comments

@mclgms
Copy link

mclgms commented Aug 24, 2023

What happened?

O componente Money ao salvar o registro durante a criação está salvando o dado corretamente.
Na edição, ele acrescenta "0" no valor original.

***Exemplo:

  • Na criação do registro: R$ 200,00.
  • Ao editar, o registro é salvo como R$ 2.000,00.

How to reproduce the bug

  • Crie um registro qualquer com o valor R$ 200,00
  • Edite o valor sem alterar nada, apenas salve o registro, o valor do campo será gravado com R$ 2.000,00.

Package Version

3.0.0

PHP Version

8.2

Laravel Version

10.20.0

Which operating systems does with happen with?

Linux

Notes

No banco o campo foi definido como: decimal(10,2)

@mclgms mclgms added the bug Something isn't working label Aug 24, 2023
@mliell
Copy link

mliell commented Aug 24, 2023

O bug também acontece uilizando MoneyCast (https://filamentphp.com/docs/3.x/panels/getting-started#casting-the-price-to-an-integer) e salvando como integer no banco. Segue vídeo com teste:

Gravacao.de.tela.de.2023-08-24.14-27-46.webm

@mtbossa
Copy link

mtbossa commented Aug 25, 2023

Eu queria saber como adicionar uma rule de valor mínimo. Tentei utilizar o 'min:1' do Laravel, ou o min() do Filament, mas nenhum funciona (nada acontece). A única forma que eu encontrei foi fazer um minLength() com o valor mínimo de caracteres.

Tentei adicionar uma custom rule com Closure, mas a variável $value as vezes vem estranha, exemplo: R$ 200,00 no input, a variável $rule está como '0,200' na Closure.

@leandrocfe leandrocfe self-assigned this Aug 29, 2023
@leandrocfe
Copy link
Owner

@mclgms , pode por favor verificar a ultima atualização e dizer se o problema resolveu?
Obrigado!

@mclgms
Copy link
Author

mclgms commented Aug 31, 2023 via email

@leandrocfe
Copy link
Owner

Eu queria saber como adicionar uma rule de valor mínimo. Tentei utilizar o 'min:1' do Laravel, ou o min() do Filament, mas nenhum funciona (nada acontece). A única forma que eu encontrei foi fazer um minLength() com o valor mínimo de caracteres.

Tentei adicionar uma custom rule com Closure, mas a variável valueasvezesvemestranha,exemplo:R 200,00 no input, a variável $rule está como '0,200' na Closure.

@mtbossa
Por favor, abra uma nova issue pra gente verificar. Obrigado!

@AdrianoMauricio1994
Copy link

Estou com o mesmo problema do @mclgms , é sempre adicionado um 0 a cada edit feito no registro

@hjJunior
Copy link

hjJunior commented Apr 8, 2024

Eu fiz algumas atualizacoes locais e me parece estar tudo ok, para quem quiser criar manualmente o field:

<?php

namespace App\Forms\Components;

use Filament\Forms\Components\TextInput;
use Illuminate\Support\Str;

class Money extends TextInput
{
    protected string|int|float|null $initialValue = '0,00';

    protected function setUp(): void
    {
        $this
            ->prefix('R$')
            ->maxLength(13)
            ->extraAlpineAttributes([
                'x-mask:dynamic' => '$money($input, ",", ".", 2)'
            ])
            ->prefix('R$')
            ->maxLength(13)
            ->dehydrateMask()
            ->default(0.00)
            ->formatStateUsing(fn($state) => $state ? number_format(floatval($state), 2, ',', '.') : $this->initialValue);
    }

    public function dehydrateMask(bool|\Closure $condition = true): static
    {

        if ($condition) {
            $this->dehydrateStateUsing(
                fn($state): ?float => $state ?
                floatval(
                    Str::of($state)
                        ->replace('.', '')
                        ->replace(',', '.')
                        ->toString()
                ) :
                null
            );
        } else {
            $this->dehydrateStateUsing(null);
        }

        return $this;
    }

    public function initialValue(null|string|int|float|\Closure $value = '0,00'): static
    {
        $this->initialValue = $value;

        return $this;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

6 participants