Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Problem with Request::forge
  • Hello everybody!

    i have a Controller_Rest and in side it i validate all Input::post. when i call it via a browser, it will validation all data .

    and I have a function which call to that controller and it seem to skip all validation.

    Sorry my english is not good.  And below is my function detail.

    $data = array(
                'post_id' => '1',
                'content' => ' '
            );

            $response =Request::forge('comment/post',false)
                ->set_method('post')
                ->execute($data)
                ->response();

            $assertValue = $response->status;
            $this->assertSame(401, $assertValue);


    Thanks for your reading.

  • and this is my Controller Rest 

    public function action_post(){

            $post_id=Input::post('post_id');
            $content=trim(Input::post('content'));

                $error=array();

                $validation = \Fuel\Core\Validation::forge('comment');
                $validation->add_callable(new MyRuleSet());
                $validation->add('content')->add_rule('required')
                    ->add_rule('trim')
                    ->add_rule('not_empty')
                    ->add_rule('min_length', 3);
                $validation->add('post_id')->add_rule('required')
                    ->add_rule('valid_string',array('numeric'));

                if($validation->run(
                    array(
                        'content'=>$content,
                        'post_id'=>$post_id
                    )
                )){

                    $result=Model_Posts::isExistPost((int)$post_id);
                    if($result==false){

                        $error['status']='403';
                        $error['message']='Post not found';
                        $this->response->set_status(403);
                    }else{
                        $comment=new Model_Comment(array(
                            'post_id'=>$result->get('post_id'),
                            'content'=>$content,
                            'create_date'=>date('y-m-d h:m:s')
                        ));
                        $result=Model_Comment::saveComment($comment);
                        $error['status']=$result;
                        if($result=='200'){
                            $error['message']='Normal';
                            $this->response->set_status(200);
                        }else{
                            $error['message']='Internal Server Error';
                            $this->response->set_status(500);
                        }
                    }

                }else{
                    $error['status']='401';
                    $error['message']='invalid argument';
                    $this->response->set_status(401);
                }
                $response['error']=$error;
                $this->response->set_header('Content-type','application/xml');
                return $this->response->body(\Fuel\Core\Format::forge($response)->to_xml(null,null,"response"));

        }
  • HarroHarro
    Accepted Answer
    In Fuel v1, Input is global, which means it isn't used in secondary requests. Insteaad, data passed to execute() will be passed as parameters to the controller method called (as if it where URi parameters).

    So try something like:

    public function action_post($post_id = null, $content = null) {

            $post_id = $post_id ?: Input::post('post_id');
            $content = $content ?: trim(Input::post('content'));
            ....

    If you want to support both main requests and HMVC calls to this controller.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion