我有一个表单,在submition时生成以下数组(见下文).
我在我的控制器中使用这些数据来执行几个操作,之后我单独保存每个操作. (一次保存所有这些都不是一种选择).
我需要做的是找到一种方法来验证每个模型.
我已经尝试过了:
$this->Model->set($pertinentData);
$this->Model2->set($pertinentData);
if($this->Model->validates() && $this->Model2->validates()){
//Do whatever
}
这会产生不准确的结果,它表示当我看到它没有时会验证,反之亦然.
有人知道可行的选择吗?是不是有办法创建无表格模型,我可以为这些字段定义验证规则,如:
Order.package_id
User.first_name
etc...
任何想法都表示赞赏.在表单生成的数组下面.
谢谢.
Array
(
[Order] => Array
(
[id] => 15
[package_id] => 1743
[tariff_id] => 5470
[tarjeta] => 332
[numero_tarjeta] => 121204045050
[vencimiento_tarjeta] => 10/20
[cod_tarjeta] => 170
[titular_tarjeta] => JESUS CRISTO
[tarjeta_nacimiento] => 22/04/1988
[tarjeta_pais] => Argentina
[tarjeta_provincia] => Buenos Aires
[tarjeta_ciudad] => Ciudad
[tarjeta_cp] => 1428
[tarjeta_calle] => Calle
[tarjeta_numero] => 1477
[tarjeta_piso] => 2
)
[User] => Array
(
[id] =>
[email] => bla8@gmail.com
[phone] => 1568134449
[first_name] => Jesus
[last_name] => Something
[documento_tipo] => dni
[dni] => 335556666
[nacionalidad] => argentino
[birthdate] => 22/04/2019
)
[OrdersCompanion] => Array
(
[1] => Array
(
[first_name] => Chango
[last_name] => Mas
[documento_tipo] => dni
[dni] => 445556666
[nacionalidad] => argentino
[birthdate] => 30/02/2010
)
[1] => Array
(
[first_name] => Chango
[last_name] => Mas
[documento_tipo] => dni
[dni] => 445556666
[nacionalidad] => argentino
[birthdate] => 30/02/2010
)
)
)
最佳答案 您可以通过在模型中定义$useTable = false来使用无表格模型.像这样
public $useTable = false;
定义所有自定义验证,当然还有架构(因为您的模型没有表,您必须手动定义模型架构).然后在您的控制器中,您必须首先指示它没有模型,然后声明$model变量.这是为了避免cakePHP的自动模型控制器绑定,你的控制器看起来像这样
public $useModel = false;
$model = ClassRegistry::init('ContactOperation');
现在您的模型与您的控制器相关,您可以轻松地进行之前定义的自定义验证.
$model->set($this->request->data);
if($model->validates()) {
$this->Session->setFlash(_('Thank you!'));
// do email sending and possibly redirect
// elsewhere for now, scrub the form
// redirect to root '/'.
unset($this->request->data);
$this->redirect('/');
} else {
$this->Session->setFlash(_('Errors occurred.'));
// display the form with errors.
}
您可以在here找到更多细节