Reputation: 17
I would like to pass request-data to this form-class to validate generic information. I've implemented a component (outsourced for reusability) to bring address data into required model format.
namespace App\Form;
use Cake\Form\Form;
use Cake\Form\Schema;
use Cake\Validation\Validator;
class Foo extends Form {
protected function _buildSchema(Schema $schema) {
return $schema
->addField('foo', 'string')
->addField('bar', 'string');
}
[...]
protected function _execute(array $data) {
// How is it possible to use component-method here?
// e.g. $this->MyAddressComponent->saveData($data);
return true;
}
}
Has anyone an idea what I have to do?
Thanks in advance!
Upvotes: 1
Views: 342
Reputation: 9614
Make your form accept the component in the constructor:
namespace App\Form;
use App\Controller\Component\MyAddressComponent;
class Foo extends Form {
private $addressComponent;
public function __constructor(MyAddressComponent $addressComponent)
{
$this->addressComponenent = $addressComponent;
}
...
protected function _execute(array $data) {
$this->addressComponent->saveData($data);
return true;
}
}
Then instantiate the form from the controller as follows:
public function doStuff()
{
$form = new Foo($this->MyAddressComponent);
...
}
Upvotes: 1