-
-
Notifications
You must be signed in to change notification settings - Fork 86
Trix
tanthammar edited this page Oct 19, 2020
·
9 revisions
There are two versions of the Trix field, with and without attachments.
Requires you to have @stack('styles')
in head,
and @stack('scripts')
after Livewire and Alpine script tags
Extends BaseField
- Push external (cdn-links) for required scripts and styles to the layout
- Omit, if you import the scripts manually
- Only pushed once, even if you add multiple Trix fields to the same form.
Use this method to set the initial trix input value.
Use $this->model
to access existing model values.
Trix::make('Trix')
->default('The initial value of the trix input') // Example: $this->model->foo
->includeExternalScripts() //will only be included once if multiple Trix fields.
->rules('nullable|string')
Add the custom()
option if you need to format the trix-input (html) before saving to database.
Read more about custom data ->
Please sponsor this package in order to get access to this field.
- same as for simple Trix field, above
- add a uniquely named component property for each trix field in the form, where you accept attachments.
- add the
deleteTrixUpload()
function to your form component (see below) -
Remove 'onKeyDownEnter' property in the
mount()
method, otherwise the user can't create new lines in the Trix input - read the Livewire documentation about how to store file uploads
Extends BaseField
- Same as simple Trix input.
-
$componentPropertyName
= Form component property name, required by Livewire file uploads - You must add a uniquely named component property for each trix field in the form, where you accept attachments.
- Max allowed file size in KB.
- You still have to validate in backend, this is just a simple frontend validation
- If the js is tampered in frontend, the
sizeLimitAlert()
message is alerted but, the file will STILL BE VISIBLE as a PREVIEW in the trix input BUT NOT SAVED in the final trix html - Combine with
sizeLimitAlert()
- This message will be displayed in a simple JS alert,
if the uploaded file is larger than
maxKB()
. - Combine with
maxKb()
public $trixUpload; //unique key for each trix field in the form that accepts attachements
public function fields()
{
return [
$this->trix(),
];
}
public function trix()
{
// Remove keyDown enter in mount() otherwise you cannot create new lines in the Trix field
return Trix::make('Trix')
->allowAttachments('trixUpload') //unique key for each trix field in the form
->maxKB(1024)
->default('The initial value of the trix input') // $this->model->foo
->includeExternalScripts() //will only be included once if multiple Trix fields.
->rules('nullable|string');
}
-
Important: name the
updatedFoo()
method based on the field key, see Event Hooks wiki -
Important !!!!! suffix the dispatched browser event name with a slug cased version of the field
name
, in the blade view AlpineJS listens to an event named: attachment-saved-{{ Str::slug($field->name, '-') }}
public function updatedTrixUpload($file)
{
// If the js is tampered in frontend, the validation error is alerted but,
// the file will STILL BE VISIBLE as a PREVIEW in the trix input but NOT SAVED in the final trix html
$this->validate([
'trixUpload' => 'image|max:1024', // Example 1MB
]);
$path = filled($file) ? $this->trixUpload->store('photos') : null; //'photos' = the storage path
//Suffix the dispatched browser event, name, with the slug-cased-version-of-the-field-name. In this example the field is named "trix", Trix:make('Trix')
if(filled($path)) $this->dispatchBrowserEvent("attachment-saved-trix", ['path' => asset($path)]);
//now the user can upload another file
//don't forget to schedule temp dir cleanup, see Livewire docs
}
- Add
deleteTrixUpload()
to your form component - You must add this function to your form component, if you add a Trix field that allows attachments.
- This method is called, when the user deletes an attachment in the trix field
- You will receive two variables, a full url (including filename) and the filename.
public function deleteTrixUpload($url, $filename)
{
//dd($url, $filename);
//delete the file
}
<x-tall-trix
:field="$field"
:temp-key="$temp_key"
:value="data_get($this, $temp_key)"
/>
Extend Blade component (or override in config file)
Tanthammar\TallForms\Components\Trix::class
- Installation
- Requirements
- v5 Upgrade Guide
- v6 Upgrade Guide
- v7 Upgrade Guide
- Support
- Quickstart
- Manual installation
- Optional
- Form component
- Field
- Field types
- Example Form
- Blade Components
- Notifications