How do you perform Form validation without a model in Cakephp?

后端 未结 6 963
Happy的楠姐
Happy的楠姐 2021-02-13 00:06

I need to perform some validation. I don\'t have the model in the application.

Does anyone know how to do the validation without a model? Can you show me using a small s

6条回答
  •  名媛妹妹
    2021-02-13 00:35

    Honestly, I'd create a model just for the validation. You can create a model that doesn't use a table by adding

    var $useTable = false;
    

    And then create a validation array with rules for each field you want to validate:

    var $validate = array('login' => 'alphaNumeric','email' => 'email','born' => 'date');
    

    Then, in your controller, do something like:

    $this->MyModel->set($this->data);
    if($this->MyModel->validates()){
        // do stuff with valid data
    }
    

    If you really, really can't use a model, then you'll have to simply loop over each value in $this->data in your controller action and validate it against a regular expression or use the Validation::[rule]() stuff, like:

    if(Validation::email($someThingThatMightBeAnEmailAddress)){
        // do stuff with valid email address.
    }
    

提交回复
热议问题