Search code examples
laravellaravel-nova

Render a field based on boolean without package


I am trying to make a Text field visible when a boolean is toggled true WITHOUT having to install a dependency. Does anyone know how to implement this? Is there a way to watch the request for changes to a particular field and conditionally render based on that?

Boolean::make('Blocked', 'isBlocked')
            ->hideFromIndex(),

Textarea::make('Reason', 'blockedReason')
            ->withMeta([
                'extraAttributes' => [
                    'placeholder' => 'Make it less than 255 characters',
                ]
            ])
            ->rules('required', 'max:255')
            ->rows(3)
            ->hideFromIndex(),
            

Solution

  • Bad solution

    One solution you could use is to update the resource with the boolean to true or false. Once the page is reloaded depending on the boolean the text field is displayed. This, however, would invalidate Nova's dynamism, I strongly advise against it.

    Probably a bad solution

    Another solution could be to create an event emitter in the change of the boolean value. Once the event is fired change the readonly value of the text field.

    Probably the best choice

    You haven't specified the reason why you can't use a package, but if you need to use the nova-dependecy-container package.