GithubHelp home page GithubHelp logo

KeyValue not supported? about nova-page HOT 2 CLOSED

whitecube avatar whitecube commented on May 28, 2024
KeyValue not supported?

from nova-page.

Comments (2)

Padocia avatar Padocia commented on May 28, 2024

the KeyValue field does not convert the result to array. Therefore it's returning a string which cause this issue & since we're not using a model so we can't cast it !

My solution is to extends the KeyValue field & create a new KeyValueJson field to use it instead of the KeyValue field

<?php

namespace App\Nova\Fields;

use Laravel\Nova\Http\Requests\NovaRequest;
use Laravel\Nova\Fields\KeyValue;

class KeyValueJson extends KeyValue
{
    /**
     * Prepare the field element for JSON serialization.
     *
     * @return array
     */
    public function jsonSerialize()
    {
        $parent = parent::jsonSerialize();
        $value = $parent['value'];
        unset($parent['value']);
        return array_merge($parent, [
            'keyLabel' => $this->keyLabel ?? __('Key'),
            'valueLabel' => $this->valueLabel ?? __('Value'),
            'actionText' => $this->actionText ?? __('Add row'),
            'readonlyKeys' => $this->readonlyKeys(app(NovaRequest::class)),
            'canAddRow' => $this->canAddRow,
            'canDeleteRow' => $this->canDeleteRow,
            'value' => is_array($value) ? $value : json_decode($value, true),
        ]);
    }
}

from nova-page.

toonvandenbos avatar toonvandenbos commented on May 28, 2024

Hi,

Layout classes have a casts attribute you can add, meaning they can act just as models. I think it is safer to cast the attributes to an array rather than creating an extended KeyValue field, which could cause compatibility issues in the future.

Please take a look at the full docs if you wonder how to create custom layout classes.

from nova-page.

Related Issues (20)

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.