Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Validation with request raw body. #3694

Closed
agungsugiarto opened this issue Sep 29, 2020 · 1 comment
Closed

Bug: Validation with request raw body. #3694

agungsugiarto opened this issue Sep 29, 2020 · 1 comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them

Comments

@agungsugiarto
Copy link
Contributor

agungsugiarto commented Sep 29, 2020

Describe the bug
Capture
When request with raw body, this validation didn't capture request, and will be missing input.

<?php

namespace App\Controllers;

use CodeIgniter\Controller

class AuthenticationController extends Controller
{
    /**
     * Register New Users from POST Requests to /api/users.
     * 
     * @return \CodeIgniter\HTTP\Response
     */
    public function register()
    {
        if (! $this->validate(static::rules())) {
            return $this->response->setJSON([
                'errors' => $this->validator->getErrors()
            ])
            ->setStatusCode(422);
        }
    }

    protected static function rules()
    {
        return [
            'email'    => 'required|is_unique[users.email]|valid_email',
            'username' => 'required|is_unique[users.username]|alpha_numeric_space',
            'password' => 'required|min_length[5]',
        ];
    }
}

CodeIgniter 4 version
v4.0.4

Affected module(s)
CodeIgniter\Validation\Validation

Context

  • OS: Windows 10
  • PHP version 7.4
@agungsugiarto agungsugiarto added the bug Verified issues on the current code behavior or pull requests that will fix them label Sep 29, 2020
@agungsugiarto
Copy link
Contributor Author

Oh im fix me problem with change validate() with my validateCustom() to base controller.

BaseController:

/**
 * A shortcut to performing validation on input data. If validation
 * is not successful, a $errors property will be set on this class.
 *
 * @param array|string $rules
 * @param array|string $data
 * @param array        $messages An array of custom error messages
 *
 * @return boolean
 */
protected function validateCustom($rules, $data, array $messages = []): bool
{
    $this->validator = Services::validation();
    // If you replace the $rules array with the name of the group
    if (is_string($rules)) {
        $validation = config('Validation');

        // If the rule wasn't found in the \Config\Validation, we
        // should throw an exception so the developer can find it.
        if (!isset($validation->$rules)) {
            throw ValidationException::forRuleNotFound($rules);
        }

        // If no error message is defined, use the error message in the Config\Validation file
        if (!$messages) {
            $errorName = $rules . '_errors';
            $messages  = $validation->$errorName ?? [];
        }

        $rules = $validation->$rules;
    }
    
    return $this->validator
        ->setRules($rules, $messages)
        ->run($data);
}

My own controller:

<?php

namespace App\Controllers;

use CodeIgniter\Controller

class AuthenticationController extends Controller
{
    /**
     * Register New Users from POST Requests to /api/users.
     * 
     * @return \CodeIgniter\HTTP\Response
     */
    public function register()
    {
        $request = $this->request->getJSON();

        if (! $this->validateCustom(static::rules(), (array) $request->user))) {
            return $this->response->setJSON([
                'errors' => $this->validator->getErrors()
            ])
            ->setStatusCode(422);
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

No branches or pull requests

1 participant