leandrocfe / filament-ptbr-form-fields

This package provides custom form fields for Filament (>=v2.17.28) that are commonly used in Brazilian web applications, such as CPF/CNPJ validation, phone number formatting, money with currency symbol, and CEP integration with ViaCep.
MIT License
90 stars 11 forks source link

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

Open mclgms opened 10 months ago

mclgms commented 10 months ago

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:

How to reproduce the bug

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)

mliell commented 10 months ago

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:

Gravação de tela de 2023-08-24 14-27-46.webm

mtbossa commented 10 months ago

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 commented 10 months ago

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

mclgms commented 10 months ago

Leandro, bom dia.

Sim, resolveu. Muito obrigado!

Atenciosamente

Marcelo Gomes | MG Consultoria e Serviços de TI Fixo : (11) 2214-3585 / Móvel: (11) 9 8364-3775

------ Mensagem original ------ De "Leandro Costa Ferreira" @.> Para "leandrocfe/filament-ptbr-form-fields" @.> Cc "Marcelo Gomes" @.>; "Mention" @.> Data 30/08/2023 15:08:22 Assunto Re: [leandrocfe/filament-ptbr-form-fields] [Bug]: Componente Money não está atualizandocorretamente os valores no Banco - Filament V3. (Issue #9)

@mclgms https://github.com/mclgms , pode por favor verificar a ultima atualização e dizer se o problema resolveu? Obrigado!

— Reply to this email directly, view it on GitHub https://github.com/leandrocfe/filament-ptbr-form-fields/issues/9#issuecomment-1699626056, or unsubscribe https://github.com/notifications/unsubscribe-auth/AE3FR6VXF7DORIIFA53YATLXX56RNANCNFSM6AAAAAA35HMZKQ. You are receiving this because you were mentioned.Message ID: @.***>

leandrocfe commented 10 months ago

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 commented 5 months ago

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

hjJunior commented 3 months ago

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;
    }
}