Welcome Guest, Not a member yet? Register   Sign In
How to validate an API REST?
#1

(This post was last modified: 04-17-2020, 10:37 AM by MatheusCastro.)

Hello, 

I'm extending Resource Controller

In situations that are passed as GET I can validate this way:

PHP Code:
public function index()
    {
        // this
        if (!$this->validate([
            'page'    => 'required|is_natural_no_zero|max_length[11]',
            'city_id' => 'required|is_natural_no_zero|max_length[11]',
            'search'  => 'permit_empty|max_length[50]'

        ])) return $this->fail($this->validator->getErrors());

      // continue
    

However, when it is necessary to pass the parameter through the function, how to solve this?


PHP Code:
    public function show($id null)
    {
        
// error
        if (!$this->validate([
            $id => 'required|is_natural_no_zero|max_length[11]',
            
        
])) return $this->fail($this->validator->getErrors());

       // continue
    



Can anyone help?
Reply
#2

(This post was last modified: 04-17-2020, 01:01 PM by jreklund.)

There are a hidden function named setGlobal (hidden as it's not in the documentation yet). That allow you to set get, post etc.

PHP Code:
public function show($id null)
    {
        
$this->request->setGlobal('get', ['page' => $id]);

        
// error
        
if (!$this->validate([
            
'page' => 'required|is_natural_no_zero|max_length[11]',
            
        ])) return 
$this->fail($this->validator->getErrors());

       
// continue
    


Haven't tested it out myself.
Reply
#3

(This post was last modified: 04-17-2020, 02:23 PM by MatheusCastro.)

(04-17-2020, 12:59 PM)jreklund Wrote: There are a hidden function named setGlobal (hidden as it's not in the documentation yet). That allow you to set get, post etc.

PHP Code:
    public function show($id null)
    {
        $this->request->setGlobal('get', ['page' => $id]);

        // error
        if (!$this->validate([
            'page' => 'required|is_natural_no_zero|max_length[11]',
            
        
])) return $this->fail($this->validator->getErrors());

       // continue
    

Haven't tested it out myself.


Hello, interesting function.

I ran some tests with it separately and I think I found some bugs.

Try using:

PHP Code:
$this->request->setGlobal('post', ['id' => $id]);

print_r($this->request->getVar());

// response: array () 

When using getVar, the declaration is not recognized.

However, if you use getPost or getGet, the parameter will be recognized.

PHP Code:
 $this->request->setGlobal('post', ['id' => $id]);
 print_r($this->request->getPost());

// response: Array ( [id] => 30 )

 
$this->request->setGlobal('get', ['id' => $id]);
 
print_r($this->request->getGet());

// response: Array ( [id] => 30 ) 

And the $this->validate function within it has the withRequest that the request is getVar()

PHP Code:
public function withRequest(RequestInterface $request): ValidationInterface
{
     if (in_array($request->getMethod(), ['put''patch''delete'])) {

        
$this->data $request->getRawInput();
     } else {

        // this
    
$this->data $request->getVar() ?? [];
    }

   return 
$this;


Solved using:

PHP Code:
    public function show($id null)
    {

        $this->request->setGlobal('request', ['id' => $id]);

        if (!$this->validate([
            'id' => 'required|is_natural_no_zero|max_length[11]',

        ])) return $this->fail($this->validator->getErrors());


        // continue
    

Since withRequest identifies only the getVar that receives $ _REQUEST, I created the variable of this type.

However, an issue addressed in the post above is still open.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB