how to pass $form->values from component?

thcom
Backer | 94
+
0
-

hi, can you please help me

i have form as component created with a factory

evertyhig work fine

but now i need to pass some parametr to redirect

something like
$this->redirect(‘Cities:seznam’, $form->values->parameter);

but how to access values ? when i use onSave[] function

<?php
   protected function createComponentCityForm()	{

        $cityForm = $this->cityFormFactory->create( $this->currentCity);

        $cityForm->onSave[] = function () {
            $this->redirect('Cities:seznam');
        };

        return $cityForm;

?>

thank you !

MajklNajt
Member | 498
+
+1
-

hello, you can pass the value in callback:

in create:

protected function saveFrom(Form $form)
{
	$this->onSave($form->values->parameter);
}

in presenter:

protected function createComponentCityForm()
{
	$cityForm = $this->cityFormFactory->create( $this->currentCity);
	$cityForm->onSave[] = function (int $parameter) {
		$this->redirect('Cities:seznam', $parameter);
	};
	return $cityForm;
}
thcom
Backer | 94
+
0
-

yes, but i need values submitttd by this form

MajklNajt
Member | 498
+
0
-

Can you paste here your code of form factory?

thcom
Backer | 94
+
0
-

enough ?

<?php


namespace App\Components;


interface CityFormFactory
{

    public function create($currentCity = null): CityForm;


}


?>
MajklNajt
Member | 498
+
0
-

I meant class where you create and proccess the form…

Toanir
Member | 57
+
0
-

Look into your CityForm class and wherever $this->onSave() is called, all of the parameters that are passed to the call can be used in the callback in presenter. That means if there is somethign like

...
$formValues = ['gps' => 'here&there', 'name' => 'Kocourkov'];
$this->onSave($id, $formValues);
...

in your presenter you get to access that data by doing something like

$cityForm = $this->cityFormFactory->create( $this->currentCity);
$cityForm->onSave[] = function ($cityId, array $data) {
	$this->alert("City $data[name] has been updated");
	$this->redirect('Cities:detail', $cityId);
};

Take a note that only the order of parameter must match, their names are merely informative here

Last edited by Toanir (2020-03-02 09:15)

MajklNajt
Member | 498
+
0
-

@Toanir I wrote the same solution, but it probably doesn't solve his problem

Toanir
Member | 57
+
0
-

Huh, I see. I didn't quite get what you meant by the part In create: ... and I thought your inquiries about the factory wouldn't lead thcom to the solution quite as concisely.

I believe we are all talking about a form-wrapping component so I also tried to push thcom to examine the code “behind” the callbacks and make sense out of it for themselves.