F
Filamentā€¢15mo 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ā€¢15mo ago
ahm, just update the value of the variable $this->textAreaNamedVariable=xy; Aka $this->description='description' anyhow have a full code?
BuggerSee
BuggerSeeā€¢15mo 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ā€¢15mo ago
what are you trying to achive?
BuggerSee
BuggerSeeā€¢15mo ago
pressing a button and just kinda filling in the textarea with that
szucs996#1
szucs996#1ā€¢15mo ago
do you have an image of the "form"?
BuggerSee
BuggerSeeā€¢15mo ago
szucs996#1
szucs996#1ā€¢15mo ago
gotcha
BuggerSee
BuggerSeeā€¢15mo ago
Press Translate to SQL button and i wanna fill the SQL text area
szucs996#1
szucs996#1ā€¢15mo ago
it's a static form? because in that case just create a variable
BuggerSee
BuggerSeeā€¢15mo ago
kinda ye
szucs996#1
szucs996#1ā€¢15mo ago
$public $sqlText=null;
$public $sqlText=null;
BuggerSee
BuggerSeeā€¢15mo ago
in the specific Resource.php
szucs996#1
szucs996#1ā€¢15mo ago
and in the function sec, let me put a code together
BuggerSee
BuggerSeeā€¢15mo ago
thanks man
szucs996#1
szucs996#1ā€¢15mo 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
BuggerSeeā€¢15mo 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ā€¢15mo ago
send me the code of the form text area do you have installed debugbar?
BuggerSee
BuggerSeeā€¢15mo ago
xdebug imma go and debug a bit šŸ˜„
szucs996#1
szucs996#1ā€¢15mo 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ā€¢15mo 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
BuggerSeeā€¢15mo ago
still trying hehe
szucs996#1
szucs996#1ā€¢15mo 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ā€¢15mo ago
You probably need to set $this->data['sql_text'] instead of the property
BuggerSee
BuggerSeeā€¢15mo ago
omg this works lol
szucs996#1
szucs996#1ā€¢15mo ago
but then u are using $data to store all the values
BuggerSee
BuggerSeeā€¢15mo ago
also added reactive() like u said
Dennis Koch
Dennis Kochā€¢15mo ago
It's inside the admin panel I guess.
szucs996#1
szucs996#1ā€¢15mo ago
and not hard-coded variables
Dennis Koch
Dennis Kochā€¢15mo ago
Admin panel pages always use $data
szucs996#1
szucs996#1ā€¢15mo ago
ah well my bad with that
BuggerSee
BuggerSeeā€¢15mo ago
yes im in the admin panel, should have made that clear i guess
Dennis Koch
Dennis Kochā€¢15mo ago
But close enough šŸ˜…
szucs996#1
szucs996#1ā€¢15mo ago
I'm mostly using outside šŸ˜„
BuggerSee
BuggerSeeā€¢15mo ago
thanks for all your help guys
Dennis Koch
Dennis Kochā€¢15mo ago
You could have selected the "admin panel" tag for the question šŸ˜…
szucs996#1
szucs996#1ā€¢15mo ago
and using $data in case I have a dynamic form šŸ˜„ welcome šŸ™‚