mirror of
https://github.com/filamentphp/filament.git
synced 2026-09-24 15:42:09 +08:00
wip forms
This commit is contained in:
@@ -148,6 +148,11 @@ class Component
|
||||
return $this->label;
|
||||
}
|
||||
|
||||
public function getLivewire()
|
||||
{
|
||||
return $this->getForm()->getLivewire();
|
||||
}
|
||||
|
||||
public function getModel()
|
||||
{
|
||||
return $this->getForm()->getModel();
|
||||
@@ -158,11 +163,6 @@ class Component
|
||||
return $this->parent;
|
||||
}
|
||||
|
||||
public function getRecord()
|
||||
{
|
||||
return $this->getForm()->getRecord();
|
||||
}
|
||||
|
||||
public function getRules($field = null)
|
||||
{
|
||||
if ($field !== null) {
|
||||
@@ -189,10 +189,8 @@ class Component
|
||||
|
||||
public function getSubform()
|
||||
{
|
||||
return Form::make()
|
||||
->context($this->getContext())
|
||||
return Form::for($this->getLivewire())
|
||||
->model($this->getModel())
|
||||
->record($this->getRecord())
|
||||
->schema($this->getSchema());
|
||||
}
|
||||
|
||||
@@ -327,12 +325,8 @@ class Component
|
||||
$callback = fn ($component) => $component->visible();
|
||||
}
|
||||
|
||||
if ($this->getRecord() === null) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
try {
|
||||
$shouldExecuteCallback = $condition($this->getRecord());
|
||||
$shouldExecuteCallback = $condition($this->getLivewire());
|
||||
} catch (\Exception $exception) {
|
||||
$shouldExecuteCallback = false;
|
||||
}
|
||||
|
||||
@@ -196,6 +196,11 @@ class Field extends Component
|
||||
return $this->validationAttribute;
|
||||
}
|
||||
|
||||
public function getValue()
|
||||
{
|
||||
return $this->getLivewire()->getPropertyValue($this->getName());
|
||||
}
|
||||
|
||||
public function helpMessage($message)
|
||||
{
|
||||
$this->configure(function () use ($message) {
|
||||
|
||||
@@ -183,19 +183,9 @@ class FileUpload extends Field
|
||||
|
||||
public function getUploadedFileUrl()
|
||||
{
|
||||
$record = $this->getRecord();
|
||||
$path = $this->getValue();
|
||||
|
||||
if ($record === null) return;
|
||||
|
||||
$name = $this->getName();
|
||||
|
||||
if (Str::of($name)->contains('.')) {
|
||||
$name = (string) Str::of($name)->after('.');
|
||||
}
|
||||
|
||||
$path = $record->{$name};
|
||||
|
||||
if (! $path) {
|
||||
if ($path === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
+18
-22
@@ -10,12 +10,10 @@ class Form
|
||||
|
||||
protected $columns = 1;
|
||||
|
||||
protected $context;
|
||||
protected $livewire;
|
||||
|
||||
protected $model;
|
||||
|
||||
protected $record;
|
||||
|
||||
protected $rules = [];
|
||||
|
||||
protected $schema = [];
|
||||
@@ -24,6 +22,11 @@ class Form
|
||||
|
||||
protected $validationAttributes = [];
|
||||
|
||||
public function __construct($livewire)
|
||||
{
|
||||
$this->livewire($livewire);
|
||||
}
|
||||
|
||||
public function columns($columns)
|
||||
{
|
||||
$this->columns = $columns;
|
||||
@@ -31,11 +34,9 @@ class Form
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function context($context)
|
||||
public static function for($livewire)
|
||||
{
|
||||
$this->context = $context;
|
||||
|
||||
return $this;
|
||||
return new static($livewire);
|
||||
}
|
||||
|
||||
public function getColumns()
|
||||
@@ -45,7 +46,7 @@ class Form
|
||||
|
||||
public function getContext()
|
||||
{
|
||||
return $this->context;
|
||||
return get_class($this->getLivewire());
|
||||
}
|
||||
|
||||
public function getDefaultValues()
|
||||
@@ -70,16 +71,16 @@ class Form
|
||||
return $schema;
|
||||
}
|
||||
|
||||
public function getLivewire()
|
||||
{
|
||||
return $this->livewire;
|
||||
}
|
||||
|
||||
public function getModel()
|
||||
{
|
||||
return $this->model;
|
||||
}
|
||||
|
||||
public function getRecord()
|
||||
{
|
||||
return $this->record;
|
||||
}
|
||||
|
||||
public function getRules()
|
||||
{
|
||||
$rules = $this->rules;
|
||||
@@ -114,9 +115,11 @@ class Form
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
public static function make()
|
||||
public function livewire($component)
|
||||
{
|
||||
return new static();
|
||||
$this->livewire = $component;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function model($model)
|
||||
@@ -126,13 +129,6 @@ class Form
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function record($record)
|
||||
{
|
||||
$this->record = (object) $record;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function rules($rules)
|
||||
{
|
||||
$this->rules = value($rules);
|
||||
|
||||
@@ -55,6 +55,11 @@ trait HasForm
|
||||
}
|
||||
}
|
||||
|
||||
protected function form()
|
||||
{
|
||||
return Form::for($this);
|
||||
}
|
||||
|
||||
public function getPropertyDefaults()
|
||||
{
|
||||
return $this->getForm()->getDefaultValues();
|
||||
|
||||
@@ -23,8 +23,7 @@ class Login extends Component
|
||||
|
||||
public function getForm()
|
||||
{
|
||||
return Form::make()
|
||||
->context(static::class)
|
||||
return Form::for($this)
|
||||
->schema([
|
||||
Components\TextInput::make('email')
|
||||
->label('filament::auth/login.form.email.label')
|
||||
|
||||
@@ -19,8 +19,7 @@ class RequestPassword extends Component
|
||||
|
||||
public function getForm()
|
||||
{
|
||||
return Form::make()
|
||||
->context(static::class)
|
||||
return Form::for($this)
|
||||
->schema([
|
||||
Components\TextInput::make('email')
|
||||
->label('filament::auth/request-password.form.email.label')
|
||||
|
||||
@@ -27,8 +27,7 @@ class ResetPassword extends Component
|
||||
|
||||
public function getForm()
|
||||
{
|
||||
return Form::make()
|
||||
->context(static::class)
|
||||
return Form::for($this)
|
||||
->schema([
|
||||
Components\TextInput::make('email')
|
||||
->label('filament::auth/reset-password.form.email.label')
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
namespace Filament\Http\Livewire;
|
||||
|
||||
use Filament\Filament;
|
||||
use Filament\Forms\HasForm;
|
||||
use Filament\Resources\Forms\HasForm;
|
||||
use Filament\Resources\Forms\Form;
|
||||
use Filament\Resources\Pages\Page;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
@@ -21,10 +21,8 @@ class EditAccount extends Page
|
||||
public function getForm()
|
||||
{
|
||||
return static::getResource()::form(
|
||||
Form::make()
|
||||
->context(static::class)
|
||||
Form::for($this)
|
||||
->model(static::getModel())
|
||||
->record($this->record)
|
||||
->submitMethod('save'),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -4,5 +4,5 @@ namespace Filament\Resources\Forms\Components;
|
||||
|
||||
class Component extends \Filament\Forms\Components\Component
|
||||
{
|
||||
//
|
||||
use Concerns\CanBeDependentOnResourceRecord;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace Filament\Resources\Forms\Components\Concerns;
|
||||
|
||||
trait CanBeDependentOnResourceRecord
|
||||
{
|
||||
public function when($condition, $callback = null)
|
||||
{
|
||||
$this->configure(function () use ($callback, $condition) {
|
||||
if (! $callback) {
|
||||
$this->hidden();
|
||||
|
||||
$callback = fn ($component) => $component->visible();
|
||||
}
|
||||
|
||||
try {
|
||||
$shouldExecuteCallback = $condition((object) $this->getLivewire()->record);
|
||||
} catch (\Exception $exception) {
|
||||
$shouldExecuteCallback = false;
|
||||
}
|
||||
|
||||
if ($shouldExecuteCallback) {
|
||||
$callback($this);
|
||||
}
|
||||
});
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace Filament\Resources\Forms\Components\Concerns;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
trait CanBeUnique
|
||||
{
|
||||
use \Filament\Forms\Components\Concerns\CanBeUnique;
|
||||
@@ -11,8 +13,10 @@ trait CanBeUnique
|
||||
$this->configure(function () use ($column, $except, $table) {
|
||||
$rule = "unique:$table,$column";
|
||||
|
||||
if ($except) {
|
||||
$rule .= ',{{ record }}';
|
||||
$record = $this->getLivewire()->record;
|
||||
|
||||
if ($except && $record instanceof Model) {
|
||||
$rule .= ",{$record->getKey()}";
|
||||
}
|
||||
|
||||
$this->addRules([$this->getName() => [$rule]]);
|
||||
|
||||
@@ -2,55 +2,8 @@
|
||||
|
||||
namespace Filament\Resources\Forms\Components\Concerns;
|
||||
|
||||
use Filament\Forms\Components\Field;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
trait InteractsWithResource
|
||||
{
|
||||
public function getRules($field = null)
|
||||
{
|
||||
if ($field !== null) {
|
||||
return $this->rules[$field] ?? null;
|
||||
}
|
||||
|
||||
if ($this->isHidden()) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$rules = $this instanceof Field ? $this->rules : [];
|
||||
|
||||
foreach ($rules as $field => $conditions) {
|
||||
$rules[$field] = $this->transformConditions($conditions);
|
||||
}
|
||||
|
||||
foreach ($this->getSubform()->getRules() as $field => $conditions) {
|
||||
$rules[$field] = array_merge($rules[$field] ?? [], $this->transformConditions($conditions));
|
||||
}
|
||||
|
||||
return $rules;
|
||||
}
|
||||
|
||||
public static function make($name)
|
||||
{
|
||||
return new static("record.{$name}");
|
||||
}
|
||||
|
||||
public function requiredWith($field)
|
||||
{
|
||||
return parent::requiredWith("record.{$field}");
|
||||
}
|
||||
|
||||
protected function transformConditions($conditions)
|
||||
{
|
||||
return collect($conditions)
|
||||
->map(function ($condition) {
|
||||
if (! is_string($condition)) {
|
||||
return $condition;
|
||||
}
|
||||
|
||||
return (string) Str::of($condition)->replace('{{ record }}', $this->getRecord() instanceof Model ? $this->getRecord()->getKey() : '');
|
||||
})
|
||||
->toArray();
|
||||
}
|
||||
use CanBeDependentOnResourceRecord;
|
||||
use ManipulatesResourceRecord;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace Filament\Resources\Forms\Components\Concerns;
|
||||
|
||||
trait ManipulatesResourceRecord
|
||||
{
|
||||
public static function make($name)
|
||||
{
|
||||
return new static("record.{$name}");
|
||||
}
|
||||
|
||||
public function requiredWith($field)
|
||||
{
|
||||
return parent::requiredWith("record.{$field}");
|
||||
}
|
||||
}
|
||||
@@ -4,5 +4,5 @@ namespace Filament\Resources\Forms\Components;
|
||||
|
||||
class Fieldset extends \Filament\Forms\Components\Fieldset
|
||||
{
|
||||
//
|
||||
use Concerns\CanBeDependentOnResourceRecord;
|
||||
}
|
||||
|
||||
@@ -4,5 +4,5 @@ namespace Filament\Resources\Forms\Components;
|
||||
|
||||
class Grid extends \Filament\Forms\Components\Grid
|
||||
{
|
||||
//
|
||||
use Concerns\CanBeDependentOnResourceRecord;
|
||||
}
|
||||
|
||||
@@ -4,5 +4,5 @@ namespace Filament\Resources\Forms\Components;
|
||||
|
||||
class Group extends \Filament\Forms\Components\Group
|
||||
{
|
||||
//
|
||||
use Concerns\CanBeDependentOnResourceRecord;
|
||||
}
|
||||
|
||||
@@ -4,5 +4,5 @@ namespace Filament\Resources\Forms\Components;
|
||||
|
||||
class Section extends \Filament\Forms\Components\Section
|
||||
{
|
||||
//
|
||||
use Concerns\CanBeDependentOnResourceRecord;
|
||||
}
|
||||
|
||||
@@ -4,5 +4,5 @@ namespace Filament\Resources\Forms\Components;
|
||||
|
||||
class Tab extends \Filament\Forms\Components\Tab
|
||||
{
|
||||
//
|
||||
use Concerns\CanBeDependentOnResourceRecord;
|
||||
}
|
||||
|
||||
@@ -4,5 +4,5 @@ namespace Filament\Resources\Forms\Components;
|
||||
|
||||
class Tabs extends \Filament\Forms\Components\Tabs
|
||||
{
|
||||
//
|
||||
use Concerns\CanBeDependentOnResourceRecord;
|
||||
}
|
||||
|
||||
@@ -4,5 +4,5 @@ namespace Filament\Resources\Forms\Components;
|
||||
|
||||
class View extends \Filament\Forms\Components\View
|
||||
{
|
||||
//
|
||||
use Concerns\CanBeDependentOnResourceRecord;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace Filament\Resources\Forms;
|
||||
|
||||
trait HasForm
|
||||
{
|
||||
use \Filament\Forms\HasForm;
|
||||
|
||||
protected function form()
|
||||
{
|
||||
return Form::for($this);
|
||||
}
|
||||
}
|
||||
@@ -3,8 +3,7 @@
|
||||
namespace Filament\Resources\Pages;
|
||||
|
||||
use Filament\Filament;
|
||||
use Filament\Forms\HasForm;
|
||||
use Filament\Resources\Forms\Form;
|
||||
use Filament\Resources\Forms\HasForm;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class CreateRecord extends Page
|
||||
@@ -52,10 +51,8 @@ class CreateRecord extends Page
|
||||
public function getForm()
|
||||
{
|
||||
return static::getResource()::form(
|
||||
Form::make()
|
||||
->context(static::class)
|
||||
$this->form()
|
||||
->model(static::getModel())
|
||||
->record($this->record)
|
||||
->submitMethod('create'),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -3,8 +3,7 @@
|
||||
namespace Filament\Resources\Pages;
|
||||
|
||||
use Filament\Filament;
|
||||
use Filament\Forms\HasForm;
|
||||
use Filament\Resources\Forms\Form;
|
||||
use Filament\Resources\Forms\HasForm;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
@@ -60,10 +59,8 @@ class EditRecord extends Page
|
||||
public function getForm()
|
||||
{
|
||||
return static::getResource()::form(
|
||||
Form::make()
|
||||
->context(static::class)
|
||||
$this->form()
|
||||
->model(static::getModel())
|
||||
->record($this->record)
|
||||
->submitMethod('save'),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -3,9 +3,9 @@
|
||||
namespace Filament\Resources\Pages;
|
||||
|
||||
use Filament\Filament;
|
||||
use Filament\Resources\Tables\HasTable;
|
||||
use Filament\Resources\Tables\RecordActions;
|
||||
use Filament\Resources\Tables\Table;
|
||||
use Filament\Tables\HasTable;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class ListRecords extends Page
|
||||
|
||||
@@ -45,8 +45,7 @@ class AttachRecord extends Component
|
||||
|
||||
public function getForm()
|
||||
{
|
||||
return Form::make()
|
||||
->context(static::class)
|
||||
return Form::for($this)
|
||||
->submitMethod('attach')
|
||||
->schema([
|
||||
Select::make('related')
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace Filament\Resources\RelationManager;
|
||||
|
||||
use Filament\Forms\HasForm;
|
||||
use Filament\Resources\Forms\HasForm;
|
||||
use Filament\Resources\Forms\Form;
|
||||
use Livewire\Component;
|
||||
|
||||
@@ -49,10 +49,8 @@ class CreateRecord extends Component
|
||||
public function getForm()
|
||||
{
|
||||
return $this->manager::form(
|
||||
Form::make()
|
||||
->context(static::class)
|
||||
Form::for($this)
|
||||
->model(get_class($this->owner->{$this->getRelationship()}()->getModel()))
|
||||
->record($this->record)
|
||||
->submitMethod('create'),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
namespace Filament\Resources\RelationManager;
|
||||
|
||||
use Filament\Filament;
|
||||
use Filament\Forms\HasForm;
|
||||
use Filament\Resources\Forms\HasForm;
|
||||
use Filament\Resources\Forms\Form;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Livewire\Component;
|
||||
@@ -30,8 +30,7 @@ class EditRecord extends Component
|
||||
|
||||
public function getForm()
|
||||
{
|
||||
$form = Form::make()
|
||||
->context(static::class)
|
||||
$form = Form::for($this)
|
||||
->model(get_class($this->owner->{$this->getRelationship()}()->getModel()))
|
||||
->submitMethod('save');
|
||||
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace Filament\Resources\Tables;
|
||||
|
||||
trait HasTable
|
||||
{
|
||||
use \Filament\Tables\HasTable;
|
||||
}
|
||||
Reference in New Issue
Block a user