CodeIgniter Forums
form data validation and data value variable - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: form data validation and data value variable (/thread-76062.html)



form data validation and data value variable - cilux - 04-11-2020

I have this controller for insert data:

PostController:
PHP Code:
    public function store(){
        if($this->request->getMethod('POST'))
        {

            $rules = [
                'title' => ['label' => lang('Posts.title'), 'rules' => 'required|min_length[10]|max_length[500]'],
                'summary' => ['label' => lang('Posts.summary'), 'rules' => 'required|min_length[10]|max_length[250]'],
                'category_id' => ['label' => lang('Posts.category'), 'rules' => 'required|integer'],
            ];

            if (! $this->validate($rules))
            {
                return redirect()->back()->withInput()->with('errors'$this->validator->getErrors());
            }

            $image $this->request->getPost('post_image_id');
            $imageModel = new ImageModel();
            $imagePath $imageModel->select('original_image')->find($image);

            $data = array(
                'title' => $this->request->getPost('title'),
                'content' => $this->request->getPost('content'),
                'meta_keywords' => $this->request->getPost('meta_keywords'),
                'meta_description' => $this->request->getPost('meta_description'),
                'reference' => $this->request->getPost('reference'),
                'reference_link' => $this->request->getPost('reference_link'),
                'image' => $imagePath,
                'image_caption' => $this->request->getPost('image_caption'),
                'image_external_url' => $this->request->getPost('image_external_url'),
                'category_id' => $this->request->getPost('category_id'),
                'post_type' => 'post',
                'author_id' => '1',
                'status' => $this->request->getPost('status'),
            );

            $insertPost $this->postModel->insert($data);
         } 

First Question: this method is true for handle form data Or I need to add validation into Model(protected $validationRules) and create function in model for insert data?!

Seconds Question: data form value variable in Controller? Or in Model? example:
PHP Code:
            $data = array(
                'title' => $this->request->getPost('title'),
                'content' => $this->request->getPost('content'),
                'meta_keywords' => $this->request->getPost('meta_keywords'),
                'meta_description' => $this->request->getPost('meta_description'),
                'reference' => $this->request->getPost('reference'),
                'reference_link' => $this->request->getPost('reference_link'),
                'image' => $imagePath,
                'image_caption' => $this->request->getPost('image_caption'),
                'image_external_url' => $this->request->getPost('image_external_url'),
                'category_id' => $this->request->getPost('category_id'),
                'post_type' => 'post',
                'author_id' => '1',
                'status' => $this->request->getPost('status'),
            ); 

My Mean is what is best way to work with form data value.


RE: form data validation and data value variable - includebeer - 04-12-2020

In my opinion, data validation and getting data form values should be done in the controller, and keep the model only for database operations. But it would make sense to define the validation rules in the model so you can use the same validation rules in different controller functions.