使用表单中的组件

时间:2015-06-22 14:14:27

标签: components cakephp-3.0

我想将请求数据传递给此表单类以验证通用信息。我已经实现了一个组件(外包为可重用性),以便将地址数据转换为所需的模型格式。

Where "some data" is not a date.
------------------------------------------------
| p_id  |   pfield_id  |  user_id  | Profile_value |
|   1   |      1       |    1      |  20-12-2000   |
|   2   |      2       |    1      |  some data    |
|   3   |      1       |    2      |  15-07-2000   |
|   4   |      3       |    2      |  some data    |
|   5   |      4       |    2      |  some data    |
|   6   |      1       |    3      |  25-10-2000   |
|   7   |      2       |    2      |  some data    |

有谁知道我必须做什么?

提前致谢!

1 个答案:

答案 0 :(得分:1)

让表单接受构造函数中的组件:

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

然后从控制器实例化表单,如下所示:

public function doStuff()
{
    $form = new Foo($this->MyAddressComponent);
    ...
}