64robots/nova-fields

readOnly doesn't pass NovaRequest to the callback

Closed this issue · 2 comments

I'm not sure if nova changed this recently, but the callback passed to Laravel\Nova\Fields\Field::readOnly should receive an instance of Laravel\Nova\Http\Requests\NovaRequest.

It seems that R64\NovaFields\Configurable::readOnly is just adding some meta if the callback is true, but Nova already does this in Laravel\Nova\Fields\Field::isReadonly.

$this->withMeta(['extraAttributes' => ['readonly' => true]]);

So it seems this package can just read from that instead of setting it's own meta, but the quickest fix is to just replace the override for readOnly with isReadOnly like so

- public function readOnly($callback = null)
- {
-     if (!$callback || (is_callable($callback) && call_user_func($callback))) {
-         return $this->withMeta(['readOnly' => true]);
-     }
-     return $this;
- }

+ public function isReadOnly(Laravel\Nova\Http\Requests\NovaRequest $request)
+ {
+     if ($isReadOnly = parent::isReadOnly()) {
+         $this->withMeta(['readOnly' => true]);
+     }
+.    return $this;
+ }

Workaround

->readonly(function () {
    $request = app(NovaRequest::class);
    return $request->user()->cannot('some permission');
}),

in the code I wonder if this can be fixed by just removing the readOnly override on the php side and better utilize the already built functionality to set a field readonly so we don't need as much code since readOnly and readonly on the php side are just doing the exact same thing....Basically just only override where needed instead of creating a confusing situation of readonly in normal fields and readOnly on R64 fields.

PS...working on potentially making this happen....

Fix available on 0.17.0