Change placeholder on Laravel Nova text field

Viewed 24

I want to change the placeholder of a Laravel Nova Text field. Currently this is being overwritten by the $attribute in the model:

protected $attributes = [
   'name' => 'Member Name',
]; 

enter image description here

The code below works, but isnt placeholder text, it actually fills the field.

 Text::make('Name')
     ->rules('required', 'string')
     ->resolveUsing(function () {
          return 'Name';
 }) 

Is there a way I can change the placeholder text/override the $attribute value?

Secondly, any ideas why the lastpass icon appears on this field? Thanks

1 Answers

To set a default value on a Nova field you can use:

    Text::make('Name')
        ->rules('required', 'string')
        ->withMeta([
            'value' => 'Real Value -- Not placeholder'
        ])

To set a placeholder if the field accepts it (Text Fields do):

    Text::make('Name')
        ->rules('required', 'string')
        ->placeholder('Some placeholder text')

The placeholder method passes it to the vue field as meta. In v3 this is its implementation:

    /**
     * Set the placeholder text for the field if supported.
     *
     * @param  string  $text
     * @return $this
     */
    public function placeholder($text)
    {
        $this->placeholder = $text;
        $this->withMeta(['extraAttributes' => ['placeholder' => $text]]);

        return $this;
    }

By setting the default $attribute value on the model, it is passing that value on to Nova. If you want to keep that default on the model, but put a placeholder in the CMS (I don't see why you would), you could do something like:

    Text::make('Name')
        ->rules('required', 'string')
        ->placeholder('Some placeholder text')
        ->withMeta([
            'value' => $this->name === 'Member Name' ? null : $this->name
        ])

Related