how to pass $form->values from component ?

4 months ago

thcom
Member | 55
+
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 !

4 months ago

MajklNajt
Member | 355
+
+1
-

hello, you can pass the value in callback:

in factory:

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;
}

4 months ago

thcom
Member | 55
+
0
-

yes, but i need values submitttd by this form

4 months ago

MajklNajt
Member | 355
+
0
-

Can you paste here your code of form factory?

4 months ago

thcom
Member | 55
+
0
-

enough ?

<?php


namespace App\Components;


interface CityFormFactory
{

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


}


?>

4 months ago

MajklNajt
Member | 355
+
0
-

I meant class where you create and proccess the form…

4 months ago

Toanir
Member | 51
+
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)

4 months ago

MajklNajt
Member | 355
+
0
-

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

4 months ago

Toanir
Member | 51
+
0
-

Huh, I see. I didn't quite get what you meant by the part In factory: ... 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.