MVC Question: Should I put form validation rules in the controller or model?

前端 未结 9 1618
野性不改
野性不改 2020-12-12 14:41

On one hand form validation could be seen as part of the application logic and therefore belonging in the model.

On the other hand, it deals directly with the input

9条回答
  •  悲哀的现实
    2020-12-12 15:36

    If you validate form in serverside using codeigniter then it validate in controller

    You need to include the form_validation library using autoload like this

    $autoload['libraries'] = array("form_validation") 
    

    OR directly you load in Controller

    $this->load->library('form_validation');
    

    Then you set the validation rule to each form field

    $this->form_validation->set_rules('username', 'User Name', 'required');
    $this->form_validation->set_rules('useremail', 'User Email', 'required|valid_email');
    

    If any error found after validation a form field then it catch in validate function

    if ($this->form_validation->validate()) {
        //return back to form
    } else {
        //successful validate all field 
    }
    

提交回复
热议问题