[英]Change placeholder on Laravel Nova text field
I want to change the placeholder of a Laravel Nova Text field.我想更改 Laravel Nova Text 字段的占位符。 Currently this is being overwritten by the $attribute in the model:
目前这被 model 中的 $attribute 覆盖:
protected $attributes = [
'name' => 'Member Name',
];
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?有没有办法可以更改占位符文本/覆盖 $attribute 值?
Secondly, any ideas why the lastpass icon appears on this field?其次,任何想法为什么 lastpass 图标出现在这个字段上? Thanks
谢谢
To set a default value on a Nova field you can use:要在 Nova 字段上设置默认值,您可以使用:
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.占位符方法将其作为元传递给 vue 字段。 In v3 this is its implementation:
在 v3 中,这是它的实现:
/**
* 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.通过在 model 上设置默认的 $attribute 值,它将该值传递给 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:
如果您想在 model 上保留该默认值,但在 CMS 中放置一个占位符(我不明白您为什么要这样做),您可以执行以下操作:
Text::make('Name')
->rules('required', 'string')
->placeholder('Some placeholder text')
->withMeta([
'value' => $this->name === 'Member Name' ? null : $this->name
])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.