2

I perform validation in my put REST api but somehow it keeps showing false

<?php

use Restserver\Libraries\REST_Controller;

defined('BASEPATH') or exit('No direct script access allowed');

require APPPATH . 'libraries/REST_Controller.php';
require APPPATH . 'libraries/Format.php';

class EventApi extends REST_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->model('Model_basic');
        $this->load->library('form_validation');
        $this->load->helper('form');
    }

    public function Event_get()
    {
        $id = $this->get('id');
        if ($id === null) {
            $event = $this->Model_basic->select_all('px_events');
        } else {
            $event = $this->Model_basic->select_where('px_events', 'id', $id)->result_array();
        }

        if ($event) {
            $this->response([
                'status' => true,
                'message' => $event
            ], REST_Controller::HTTP_OK);
        } else {
            $this->response([
                'status' => False,
                'message' => 'Id Not Found'
            ], REST_Controller::HTTP_NOT_FOUND);
        }
    }

    public function Event_post()
    {
        //post
    }

    public function Event_put()
    {
        $id = $this->put('id');

        if($id === null){
            $this->response([
                'status' => False,
                'message' => 'Please Provide an Id'
            ], REST_Controller::HTTP_BAD_REQUEST);
        }elseif($id ===""){
            $this->response([
                'status' => False,
                'message' => 'Please Provide an Id'
            ], REST_Controller::HTTP_BAD_REQUEST);
        }else{

            $this->form_validation->set_rules('golf_course_id', 'golf_course_id', 'trim|required');
            $this->form_validation->set_rules('title', 'title', 'trim|required');
            $this->form_validation->set_rules('holes', 'holes', 'numeric|required');
            $this->form_validation->set_rules('prizel_pool', 'prize_pool', 'trim|required|numeric');

                $data = [
                    'golf_course_id'    => $this->put('golf_course_id'),
                    'title'             => $this->put('title'),
                    'date_start'        => $this->put('date_start'),
                    'date_end'          => $this->put('date_end'),
                    'holes'             => $this->put('holes'),
                    'prize_pool'        => $this->put('prize_pool'),
                    'date_created'      => $this->put('date_created'),
                    'date_modified'     => $this->put('date_modified')
                ];

            if($this->form_validation->run() === false) {
                $this->response([
                    'status' => False,
                    'message' => 'Please List The Field'
                ], REST_Controller::HTTP_BAD_REQUEST);
            }else{
                if ($this->Model_basic->update('px_events', $data, 'id', $id) > 0) {
                    $this->response([
                        'status' => True,
                        'message' => $data,
                    ], REST_Controller::HTTP_NO_CONTENT);
                } else {
                    $this->response([
                        'status' => false,
                        'message' => 'Update Fail',
                    ], REST_Controller::HTTP_BAD_REQUEST);
                }
            }
        }
    }

    public function Event_delete()
    {

       //Delete
}

1 Answer 1

3

Form Validation uses per default POST Data. You have to fill it with your PUT data. The following should work:

public function Event_put()
{

    try
    {
        $this->form_validation->set_data($this->put());
        $this->form_validation->set_rules('id', 'Id', 'trim|required');
        $this->form_validation->set_rules('golf_course_id', 'golf_course_id', 'trim|required');
        $this->form_validation->set_rules('title', 'title', 'trim|required');
        $this->form_validation->set_rules('holes', 'holes', 'numeric|required');
        $this->form_validation->set_rules('prizel_pool', 'prize_pool', 'trim|required|numeric');

        if(!$this->form_validation->run()) throw new Exception(validation_errors());

        $data = [
            'golf_course_id'    => $this->put('golf_course_id'),
            'title'             => $this->put('title'),
            'date_start'        => $this->put('date_start'),
            'date_end'          => $this->put('date_end'),
            'holes'             => $this->put('holes'),
            'prize_pool'        => $this->put('prize_pool'),
            'date_created'      => $this->put('date_created'),
            'date_modified'     => $this->put('date_modified')
        ];

        if ($this->Model_basic->update('px_events', $data, 'id', $id) > 0) {
            $this->response([
                'status' => True,
                'message' => $data,
            ], REST_Controller::HTTP_NO_CONTENT);
        } else {
            throw new Exception('Update fail');
        }

    }
    catch(\Throwable $e)
    {
        $this->response([
            'status' => False,
            'message' => $e->getMessage(),
        ], REST_Controller::HTTP_BAD_REQUEST);

    }
}           
Sign up to request clarification or add additional context in comments.

2 Comments

thank's man i've been troubleshoot my problem for 1 day and you help me but some code it doesn't work on my code so i must edit some code but really thanks man
Just for completeness, the PUT variable name is "prizel_pool". Should be "prize_pool"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.