• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
form data validation and data value variable

#1
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.
Reply


Messages In This Thread
form data validation and data value variable - by cilux - 04-11-2020, 02:02 PM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.