-
-
Notifications
You must be signed in to change notification settings - Fork 86
Custom Field
You do not have to create code for labels, help or error messages when creating custom fields. They are handled by this package. All fields are automatically wrapped with the default field slots.
If you are looking to change the styling of an existing field it is enough to extend the fields blade component. See Blade Components.
- Name your class in a non-conflicting and descriptive way
- add the
$type
property, naming rule = slug casing the class name.
namespace YourNameSpace;
class MyField extends BaseField
{
public $specialProp; //example adding your own properties
// to avoid IDE errors, all properties that exists in BaseField or its traits should be set in the overrides() method.
// please put this method first, for code readability
public function overrides(): self
{
$this->type = 'my-field'; //required property!
return $this;
}
//now your field has access to all BaseField methods, you can override them or add more
public function specialProp($prop): self
{
$this->specialProp = $prop;
return $this;
}
}
- Minimum requirements is to pass the
$field
use Illuminate\View\View;
use Illuminate\View\Component;
use YourNameSpace\MyField as Field;
use Tanthammar\TallForms\Traits\Helpers;
class MyField extends Component
{
use Helpers;
public Field $field;
public function __construct(Field $field)
{
$this->field = $field;
}
public function render(): View
{
return view('components.my-field'); //observe the file name
}
If you want to share your field, it is required that you use the same method naming as all other fields.
- Apply the
options()
,class()
anderror()
methods.
public function options(): array
{
$custom = $this->field->getAttr('input');
$default = [
$this->field->wire => $this->field->key,
'type' => 'text', //example when using an input field
'name' => $this->field->key,
'class' => $this->class()
];
return array_merge($default, $custom);
}
public function class()
{
$class = "form-input my-1 w-full "; //example class from a default input field
$class .= $this->field->class;
return Helpers::unique_words($class);
}
public function error()//example class from a default input field
{
return Helpers::unique_words($this->class()." border-red-300 text-red-900 placeholder-red-300 focus:border-red-300 focus:shadow-outline-red");
}
-
resources/views/components/my-field.blade.php
. Name the view the same as the field$type
. - You have access to the
$field
and all component attributes in your blade view.
Example using an input field based on MyField
blade component class:
<div>
<div>{{ $field->specialProp }}</div>
<input
value="{{ old($field->key) }}"
@foreach($options() as $key => $value) {{$key}}="{{$value}}" @endforeach
{{ $attributes->merge(['class' => $errors->has($field->key) ? $error() : $class() ]) }} />
</div>
-
resources/views/vendor/tall-forms/fields/my-field.blade.php
. Name the view the same as the field$type
. - use the blade component you just created
<x-my-field :field="$field" />
MyField::make(...)
If you are going to share your field with the community. Please move (applicable) classes to tall-theme.css
in order for other users to easily be able to redesign the field. At the moment there are two theme files, tailwind 1.x/2.x. Please consider compatibility with both versions. This will change in the future when this package only supports TW2.x.
If you run into errors where you get a message that the property or method does not exist in Livewire\LivewireViewCompilerEngine
, you are probably trying to access {{$this->someComponentMethod()}}
or {{$this->someComponentProperty}}
or {{$someComponentProperty}}
.
To get around it, you have to pass the property from the field blade view
to the blade component
.
If you are having trouble with @entangle
I recommend reviewing existing @entangle
issues
If you make a new field. Please share it! Make a PR or paste the code in an issue, and I'll add it to the package.
- 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