dongliaojing0554 2015-03-26 20:23
浏览 35
已采纳

Cakephp,手动验证多个模型

I have a form that produces the following array upon submition (see below). I am using this data in my controller to perform several operations, after which I save each one individually. (Saving them all at once is not an option). What I would need to do is to find a way to validate each of this models. I have tried already:

$this->Model->set($pertinentData);
$this->Model2->set($pertinentData);
if($this->Model->validates() && $this->Model2->validates()){
    //Do whatever
}

This produces unaccurate results, says it validates when I can see it doesn't and viceversa.

Anybody has any idea of a viable option? Ain't there a way to create a tableless model where I can define validation rules for these fields like:

Order.package_id
User.first_name
etc...

Any idea is appreciated. Below the array that the form produces. Thanks.

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
            )

    )

)
  • 写回答

1条回答 默认 最新

  • doudao1369 2015-03-26 20:39
    关注

    You can usea tableless model by defining $useTable= false in the model. Like this

    public $useTable = false;
    

    Define all your custom validation and, of course, your schema (since your model has no table you have to define manually the model schema). Then in your controller, you must first indicate that it has no model, and then declare the $model variable. This is to avoid the automatic model-controller binding of cakePHP, your controller would look like this

    public $useModel = false;
    $model = ClassRegistry::init('ContactOperation');
    

    Now your model is related to your controller as you want, and you can easily make your custom validation, previously defined.

    $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.
        }
    

    You can find more detail from here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测