【问题标题】:Validation rest api in codeigniter keep return falsecodeigniter中的验证rest api保持返回false
【发布时间】:2020-01-21 10:28:46
【问题描述】:

我在我的put REST api 中执行验证,但不知何故它一直显示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
}

【问题讨论】:

    标签: php rest codeigniter


    【解决方案1】:

    表单验证默认使用POST 数据。您必须用您的 PUT 数据填充它。 以下应该有效:

    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);
    
        }
    }           
    

    【讨论】:

    • 谢谢,我已经解决我的问题 1 天了,你帮助了我,但有些代码在我的代码上不起作用,所以我必须编辑一些代码,但真的谢谢你
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多