nova-money-field icon indicating copy to clipboard operation
nova-money-field copied to clipboard

Make 'null' the default value but allow overwriting it

Open therealcljohn opened this issue 5 years ago • 4 comments

This pull request makes 'null' the default value of the money field and thus allows to null the field by default which can be usefull in some cases.

Any default value (including 0) can be set using:

            Money::make('Price', 'EUR')
                ->storedInMinorUnits()
                ->withMeta($this->price ? [] : ["value" => 'Your default value'])

This seems to be the default behaviour for most Nova fields as described here: https://github.com/laravel/nova-issues/issues/58#issuecomment-419754713

If you like the pull request please consider a merge. If you have any recommendations for me before merging just let me know in the comments.

therealcljohn avatar Feb 06 '19 16:02 therealcljohn

Hello. I guess the logic should be backwards. By default it should be "0". If you want to allow null, lets consider nullable method which will set true to public $nullable = false?

vyuldashev avatar Feb 06 '19 21:02 vyuldashev

@vyuldashev I'm not shure how to implement this. But I made some other changes to preserve the default behaviour on setting the value to 0 by default.

Price can now be set to null by default by forcing an empty value:

            Money::make('Price', 'EUR')
                ->storedInMinorUnits()
                ->withMeta(isset($this->price) ? [] : ["value" => ''])

Does this go closer to what we want?

therealcljohn avatar Feb 07 '19 10:02 therealcljohn

I guess money should be always numeric value. Null is an exceptional case. It should not be by default and required to be enabled explicitly.

vyuldashev avatar Feb 07 '19 11:02 vyuldashev

A money field should be nullable if it's a business requirement. Look a at the Coupon example, there are two money fields: amount_off and percentage_off. One of them has a value and the other is null, depending if your coupon use a fixed amount discount or a percentage. Storing 0 instead of null could be confusing

mydnic avatar Sep 04 '19 14:09 mydnic