Coder Social home page Coder Social logo

nova-json's Introduction

nova-json

Software License Total Downloads Tests

The JSON field wrapper allows you to specify multiple fields which will be resolved into a single model attribute. This allows you to validate every information you store inside a json column seperately.

JSON::make('Author', [
    Text::make('Name')->rules(['string', 'required', 'min:3']),
    Text::make('Email')->rules(['email', 'required']),
])

The above will be resolved into a single author attribute on the model.

// prequesite: the 'author' attribute needs to casted into a json castable type
// e.g. object, array, ...
['author' => ['name' => '', 'email' => '']]

Install & setup

composer require naoray/nova-json

Add the column's name, you want to use in the JSON field, to your $casts array on the resource's model!

Usage

You can destructure one JSON column into multiple Nova fields and apply unique rules to each of the key-value pairs.

use Naoray\NovaJson\JSON;

// within your nova resource
public function fields()
{
    return [
        //...
        JSON::make('Some Json Column Name', [
            Text::make('First Field'),
            Text::make('Second Field'),
            Text::make('Third Field'),
        ]);
    ]
}

FillUsing callbacks

The ->fillUsing() callbacks are normally used to fill the models attribute directly. With this package, it's not necessary to fill the model's attribute, but instead you should return the value you want to save on the model itself.

JSON::make('Address', 'address', [
    Text::make('Street')->fillUsing(fn ($request, $model, $attribute, $requestAttribute) => $request[$requestAttribute] . ' Foo'),
]);

The above example is rather silly than useful, but it demonstrates the concept. The _ Foo_ value will be apended to every address->street value within nova.

Fill at once

When using data transfer objects (which works well with castable dto's) you don't want each field to be filled seperately, because than the dto's validation is useless. With the fillAtOnce() method a Hidden field will be added and the filling of single fields will be avoided. Instead all values will be filled at once via the Hidden field.

JSON::make('Address', 'address', [
    Text::make('Street'),
    Text::make('City'),
])->fillAtOnce();

The fillOnce() method accepts a Callback which can be used to modify the data structure before it is added to the model.

// given these fields:
JSON::make('Address', 'address', [
    Text::make('Street'),
    Text::make('City'),
])->fillAtOnce(function ($request, $requestValues, $model, $attribute, $requestAttribute) {
    return ['nested' => $requestValues];
});

// and a request with ['address->street' => 'test str. 5', 'address->city' => 'test city']

// we will get
$requestValues = ['street' => 'test str. 5', 'city' => 'test city'];

// which will be pased into the fillAtOnce callback leading to the following in our db:
['address' => ['nested' => ['street' => 'test str. 5', 'city' => 'test city']]];

Nullable Fields

As with other fields you can call nullable() and nullValues() on the JSON field directly to make all fields contained nullable and specify which values are treated as null

JSON::make('Address', 'address', [
    Text::make('Street'),
    Text::make('City'),
])->nullable()->nullValues(['_', 0])

Labels and Attributes

By default the first argument you provide the JSON field will be considered its name. If you don't provide a second string argument the attribute of the field will be guessed e.g. 'Some Json Column Name' => 'some_json_column_name'. If you want your field name to be different from your attribute you can provide the field with a second argument and provide the fields as the third argument: JSON::make('Some Name', 'column_name', [])

Nested Structures

The JSON field can also be nested by itself to display nested JSON structures:

JSON::make('Meta', [
    Text::make('Street'),

    JSON::make('Location', [
        Text::make('Latitude'),
        Text::make('Longitude'),
    ]),
]);

Use inside Panels

In order to use JSON column inside Nova Panel you need to get 'data' property of the top level JSON field.

Examples

  1. JSON is the only field inside Panel
new Panel('Brand Settings', 
    JSON::make('brand_settings', [
        Image::make('Logo')->disk('public'),
        Color::make('Primary Color')->swatches(),
        Color::make('Secondary Color')->swatches(),
    ])->data,
),
  1. if you need other fields inside the Panel you can use splat operator like this:
new Panel('Brand Settings', [
    Text::make('Some Field'),
    ...JSON::make('brand_settings', [
        Image::make('Logo')->disk('public'),
        Color::make('Primary Color')->swatches(),
        Color::make('Secondary Color')->swatches(),
    ])->data,
]),

Testing

Run the tests with:

vendor/bin/phpunit

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Credits

Security

If you discover any security-related issues, please email [email protected] instead of using the issue tracker.

License

The MIT License (MIT). Please see License File for more information.

nova-json's People

Contributors

elijahworkz avatar gisostallenberg avatar naoray avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

nova-json's Issues

Nested resources don't get the JSON prefix

Aparently, the following setup loses the JSON prefix, resulting in manual prefix addition, see as follows:

use Naoray\NovaJson\JSON;
use Epartment\NovaDependencyContainer\NovaDependencyContainer;
use Laravel\Nova\Fields\Heading;

NovaDependencyContainer::make([
    Heading::make('Theme Options'),
    ...JSON::make('Options', [ // *Here the prefix is added*
            Select::make('Type', 'example_type')
                ->options([ /* ... */ ])
                ->nullable()
                ->rules([
                    Rule::requiredIf(fn () => request()->has('options.example'))
                ]),
            NovaDependencyContainer::make([
                // Somehow, the JSON prefix is lost along the way...
                Text::make('URL', 'options->example') // *You need to manually add the prefix again*
                    ->nullable(),
            ])->dependsOn('example_type', 'redirect'),
            NovaDependencyContainer::make([
                Code::make('Code', 'options->example') //  *You need to manually add the prefix again*
                    ->language('javascript')
                    ->nullable(),
            ])->dependsOn('example_type', 'javascript'),
        ])->data,
])
->dependsOn('theme', $someTheme)

If the prefix are not added, they are sent without any prefix to laravel, resulting in the wrong information. The code above, results in the correct functioning. The prefix needs to end with a '->' due form encoding.

Is it even possible to access such deep data considering we could not know that the Nova Dependency Container even exists? I mean, my fix works but feels wrong.

Not compatible with laravel <8

Not sure, but I think you package isn't compatible with laravel <8?

Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Root composer.json requires naoray/nova-json ^1.1 -> satisfiable by naoray/nova-json[v1.1.0].
    - naoray/nova-json v1.1.0 requires illuminate/http ^8.0 -> found illuminate/http[v8.0.0, ..., 8.x-dev] but it conflicts with another require.

Package not working with Nova ^4.0 and Laravel 9

Hello, i want report an issue dicovered when i tried to install this package inside my Nova ^4.0 with Laravel 9 as Framework version.

Error: ` Problem 1
- Root composer.json requires naoray/nova-json ^2.0 -> satisfiable by naoray/nova-json[v2.0.0].
- naoray/nova-json v2.0.0 requires laravel/nova ^3.0 -> found laravel/nova[dev-master, v3.0.0, ..., 3.x-dev (alias of dev-master)] but it conflicts with your root composer.json require (~4.0).

Use the option --with-all-dependencies (-W) to allow upgrades, downgrades and removals for packages currently locked to specific versions.`

Any suggest? Thank you!

Unexpected bool (T_STRING) JSON.php while loading Form view

Laravel 8+
Nova 3.25

I can't even load the Form view with this code in place:

\Naoray\NovaJson\JSON::make('Options', [
    Text::make('Name'),
    Text::make('Description')
])->nullable()
protected $casts = [
    'options' => 'json'
];

local.ERROR: syntax error, unexpected 'bool' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST) {"userId":1,"exception":"[object] (ParseError(code: 0): syntax error, unexpected 'bool' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST) at \vendor\naoray\nova-json\src\JSON.php:60

It's crashing on this line:
public bool $fillAtOnce = false;

I noticed there's a method with the same name; not sure if that's a problem.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.