F
Filamentā€¢2y ago
BuggerSee

Set value of Component TextArea in FormAction

Hello, how can i change the value of a TextArea when a button is pressed? Currently have this code in the class that extends CreateRecord but the ->value() function doesnt work: $forms = $this->getForms(); $form = Arr::first($forms); $sqlComponent = $form->getComponents()[0]; $textArea = $sqlComponent->getChildComponents()[1]; $translatedSql = "translated"; $textArea->value($translatedSql);
36 Replies
szucs996#1
szucs996#1ā€¢2y ago
ahm, just update the value of the variable $this->textAreaNamedVariable=xy; Aka $this->description='description' anyhow have a full code?
BuggerSee
BuggerSeeOPā€¢2y ago
class CreateSqlQuery extends CreateRecord
{
protected static string $resource = SqlQueryResource::class;

protected function getTranslateToSqlFormAction(): Action
{
return Action::make('translateToSql')
->label(__('Translate to SQL via AI'))
->action('translateToSql')
->keyBindings(['mod+shift+t'])
->color('secondary');
}

public function translateToSql(): void
{
$record = $this->record;
$forms = $this->getForms();
$form = Arr::first($forms);
$sqlComponent = $form->getComponents()[0];
$textArea = $sqlComponent->getChildComponents()[1];
$translatedSql = "translated";
}

protected function getFormActions(): array
{
return array_merge(
[$this->getCreateFormAction()],
[$this->getCreateAnotherFormAction()],
[$this->getTranslateToSqlFormAction()],
[$this->getCancelFormAction()],
);
}
}
class CreateSqlQuery extends CreateRecord
{
protected static string $resource = SqlQueryResource::class;

protected function getTranslateToSqlFormAction(): Action
{
return Action::make('translateToSql')
->label(__('Translate to SQL via AI'))
->action('translateToSql')
->keyBindings(['mod+shift+t'])
->color('secondary');
}

public function translateToSql(): void
{
$record = $this->record;
$forms = $this->getForms();
$form = Arr::first($forms);
$sqlComponent = $form->getComponents()[0];
$textArea = $sqlComponent->getChildComponents()[1];
$translatedSql = "translated";
}

protected function getFormActions(): array
{
return array_merge(
[$this->getCreateFormAction()],
[$this->getCreateAnotherFormAction()],
[$this->getTranslateToSqlFormAction()],
[$this->getCancelFormAction()],
);
}
}
szucs996#1
szucs996#1ā€¢2y ago
what are you trying to achive?
BuggerSee
BuggerSeeOPā€¢2y ago
pressing a button and just kinda filling in the textarea with that
szucs996#1
szucs996#1ā€¢2y ago
do you have an image of the "form"?
BuggerSee
BuggerSeeOPā€¢2y ago
szucs996#1
szucs996#1ā€¢2y ago
gotcha
BuggerSee
BuggerSeeOPā€¢2y ago
Press Translate to SQL button and i wanna fill the SQL text area
szucs996#1
szucs996#1ā€¢2y ago
it's a static form? because in that case just create a variable
BuggerSee
BuggerSeeOPā€¢2y ago
kinda ye
szucs996#1
szucs996#1ā€¢2y ago
$public $sqlText=null;
$public $sqlText=null;
BuggerSee
BuggerSeeOPā€¢2y ago
in the specific Resource.php
szucs996#1
szucs996#1ā€¢2y ago
and in the function sec, let me put a code together
BuggerSee
BuggerSeeOPā€¢2y ago
thanks man
szucs996#1
szucs996#1ā€¢2y ago
class CreateSqlQuery extends CreateRecord
{
protected static string $resource = SqlQueryResource::class;
public string $sqlText=null;

protected function getTranslateToSqlFormAction(): Action
{
return Action::make('translateToSql')
->label(__('Translate to SQL via AI'))
->action(fn()=>$this->translateToSql())
->keyBindings(['mod+shift+t'])
->color('secondary');
}

public function translateToSql(): void
{
$this->sqlText='translated';
}

protected function getFormActions(): array
{
return array_merge(
[$this->getCreateFormAction()],
[$this->getCreateAnotherFormAction()],
[$this->getTranslateToSqlFormAction()],
[$this->getCancelFormAction()],
);
}
}
class CreateSqlQuery extends CreateRecord
{
protected static string $resource = SqlQueryResource::class;
public string $sqlText=null;

protected function getTranslateToSqlFormAction(): Action
{
return Action::make('translateToSql')
->label(__('Translate to SQL via AI'))
->action(fn()=>$this->translateToSql())
->keyBindings(['mod+shift+t'])
->color('secondary');
}

public function translateToSql(): void
{
$this->sqlText='translated';
}

protected function getFormActions(): array
{
return array_merge(
[$this->getCreateFormAction()],
[$this->getCreateAnotherFormAction()],
[$this->getTranslateToSqlFormAction()],
[$this->getCancelFormAction()],
);
}
}
and add
->reactive()
->reactive()
to the textform do you have the mount function ?
BuggerSee
BuggerSeeOPā€¢2y ago
like where ? i made the changes and get no errors but it doesnt update the value somehow, so i cant see text in the textarea
szucs996#1
szucs996#1ā€¢2y ago
send me the code of the form text area do you have installed debugbar?
BuggerSee
BuggerSeeOPā€¢2y ago
xdebug imma go and debug a bit šŸ˜„
szucs996#1
szucs996#1ā€¢2y ago
GitHub
GitHub - barryvdh/laravel-debugbar: Debugbar for Laravel (Integrate...
Debugbar for Laravel (Integrates PHP Debug Bar). Contribute to barryvdh/laravel-debugbar development by creating an account on GitHub.
szucs996#1
szucs996#1ā€¢2y ago
Forms\Components\Textarea::make('xy')
->reactive()
Forms\Components\Textarea::make('xy')
->reactive()
and add like that the reactive to your textarea made it? šŸ™‚
BuggerSee
BuggerSeeOPā€¢2y ago
still trying hehe
szucs996#1
szucs996#1ā€¢2y ago
should be working the one I sent and if you install the debugbar you can see the livewire properties from the browser
Dennis Koch
Dennis Kochā€¢2y ago
You probably need to set $this->data['sql_text'] instead of the property
BuggerSee
BuggerSeeOPā€¢2y ago
omg this works lol
szucs996#1
szucs996#1ā€¢2y ago
but then u are using $data to store all the values
BuggerSee
BuggerSeeOPā€¢2y ago
also added reactive() like u said
Dennis Koch
Dennis Kochā€¢2y ago
It's inside the admin panel I guess.
szucs996#1
szucs996#1ā€¢2y ago
and not hard-coded variables
Dennis Koch
Dennis Kochā€¢2y ago
Admin panel pages always use $data
szucs996#1
szucs996#1ā€¢2y ago
ah well my bad with that
BuggerSee
BuggerSeeOPā€¢2y ago
yes im in the admin panel, should have made that clear i guess
Dennis Koch
Dennis Kochā€¢2y ago
But close enough šŸ˜…
szucs996#1
szucs996#1ā€¢2y ago
I'm mostly using outside šŸ˜„
BuggerSee
BuggerSeeOPā€¢2y ago
thanks for all your help guys
Dennis Koch
Dennis Kochā€¢2y ago
You could have selected the "admin panel" tag for the question šŸ˜…
szucs996#1
szucs996#1ā€¢2y ago
and using $data in case I have a dynamic form šŸ˜„ welcome šŸ™‚
Want results from more Discord servers?
Add your server