Text

# Make

The text field includes all the basic methods.

use MoonShine\Fields\Text;
 
//...
 
public function fields(): array
{
return [
Text::make('Title')
];
}
 
//...

# Default value

You can use the default() method if you need to specify a default value for a field.

default(mixed $default)
use MoonShine\Fields\Text;
 
//...
 
public function fields(): array
{
return [
Text::make('Title')
->default('-')
];
}
 
//...

# Only for reading

If the field is read-only, then you must use the readonly() method.

readonly(Closure|bool|null $condition = null)
use MoonShine\Fields\Text;
 
//...
 
public function fields(): array
{
return [
Text::make('Title')
->readonly()
];
}
 
//...
)

# Mask

The mask() method is used to add a mask to a field.

mask(string $mask)
use MoonShine\Fields\Text;
 
//...
 
public function fields(): array
{
return [
Text::make('Title')
->mask('7 (999) 999-99-99')
];
}
 
//...

# Placeholder

The placeholder() method allows you to set placeholder attribute on the field.

placeholder(string $value)
use MoonShine\Fields\Text;
 
//...
 
public function fields(): array
{
return [
Text::make('Country', 'country')
->nullable()
->placeholder('Country')
];
}
 
//...

# Extensions

There are several extensions available for the Text field:

+ ability to copy a value using a button

copy(string $value = '{{value}}')
  • {{value}} - field value.
use MoonShine\Fields\Text;
 
//...
 
public function fields(): array
{
return [
Text::make('Title')
->copy(),
Text::make('Token')
->copy('https://domain.com?token={{value}}')
];
}
 
//...

+ lock with change blocking

locked()

+ disable value display

eye()

+ format hint

expansion(string $ext)
use MoonShine\Fields\Text;
 
//...
 
public function fields(): array
{
return [
Text::make('Title')
->copy()
->locked()
->expansion('kg')
->eye()
];
}
 
//...

The copy method uses the Clipboard API which is only available for HTTPS or localhost

You can use custom extensions, To do this, they must be added to the field via the extension() method.

extension(InputExtension $extension)
use MoonShine\Fields\Text;
 
//...
 
public function fields(): array
{
return [
Text::make('Title')
->extension(new InputCustomExtension())
];
}
 
//...

# Tags

The tags() method allows you to enter data in the form of tags.

tags(?int $limit = null)
  • $limit - number of available tags, unlimited by default.
use MoonShine\Fields\Text;
 
//...
 
public function fields(): array
{
return [
Text::make('Keywords')
->tags()
];
}
 
//...

# Editing in preview

The updateOnPreview() method allows you to edit the Text field in preview mode.

updateOnPreview(?Closure $url = null, ?ResourceContract $resource = null, mixed $condition = null)
  • $url - (optional) url for processing an asynchronous request,
  • $resource - (optional) model resource if the field is outside the resource
  • $condition - (optional) method execution condition.

The settings are not required and must be passed if the field is running out of resource.

use MoonShine\Fields\Text;
 
//...
 
public function fields(): array
{
return [
Text::make(Country)
->updateOnPreview()
];
}
 
//...

# Special characters

By default, the Text field and its descendants convert special characters into HTML entities when outputting values.

The unescape() method allows you to undo the conversion of special characters in the HTML entity when outputting values.

use MoonShine\Fields\Text;
 
//...
 
public function fields(): array
{
return [
Text::make('Title')
->unescape()
];
}
 
//...