Skip to content

Question: How do i create a write-only field? #192

@preliot

Description

@preliot

The question boils down to this: How do i create a write-only field?

Right now I have a field catachall that should be write-only for a super user. But that field seems to be writable no matter what i do. Even if I force always readonly() or change the field type f.e. to Str, it still allows creation of the resource.

MailAddressSchema.php

    public function fields(): array
    {
        return [
            //..
            
            Boolean::make('catchall')
                ->sortable()
                ->readOnly(static fn ($request): bool => !$request->user()->isSuperUser()),

            //..

        ];
    }

MailAddressResource.php

    public function attributes($request): iterable
    {
        $attributes = [
            //..
            'name' => $this->name,
            //..
        ];

        if ($request->user()->isSuperUser()) {
            $attributes['catchall'] = $this->catchall;
        }

        return $attributes;
    }

MailAddressRequest.php

class MailAddressRequest extends ResourceRequest
{
    /**
     * Get the validation rules for the resource.
     *
     * @return array
     */
    public function rules(): array
    {
        return [
            //..
            'name' => ['required', 'string', 'max:255'],
            //..
            'catchall' => 'nullable|boolean',
        ];
    }
}

Policies

Policies allow all actions on this resource, which is what i want.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions