[英]Change placeholder on Laravel Nova text field
我想更改 Laravel Nova Text 字段的占位符。 目前这被 model 中的 $attribute 覆盖:
protected $attributes = [
'name' => 'Member Name',
];
下面的代码有效,但不是占位符文本,它实际上填充了该字段。
Text::make('Name')
->rules('required', 'string')
->resolveUsing(function () {
return 'Name';
})
有没有办法可以更改占位符文本/覆盖 $attribute 值?
其次,任何想法为什么 lastpass 图标出现在这个字段上? 谢谢
要在 Nova 字段上设置默认值,您可以使用:
Text::make('Name')
->rules('required', 'string')
->withMeta([
'value' => 'Real Value -- Not placeholder'
])
如果字段接受,则设置占位符(文本字段):
Text::make('Name')
->rules('required', 'string')
->placeholder('Some placeholder text')
占位符方法将其作为元传递给 vue 字段。 在 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;
}
通过在 model 上设置默认的 $attribute 值,它将该值传递给 Nova。 如果您想在 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.