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

Integer validator skip validating empty object and trying to insert value to database #12026

Closed
piotr-grzelka opened this issue Jul 27, 2016 · 3 comments
Labels

Comments

@piotr-grzelka
Copy link

What steps will reproduce the problem?

class Task extends \yii\db\ActiveRecord
{
    ....
    function rules() {
        return [
            [['name'], 'required'],
            [['deadline'], 'integer']
        ];
    }
    ....
}
class TaskController extends \yii\rest\ActiveController 
{

    function actionCreate() {
        $model = new Task();
        $model->load(Yii::$app->getRequest()->post(), '');
        $model->save();
        return $model;
    }

}

my api request

POST /api/v1/project/tasks HTTP/1.1
Host: icp.pl
Accept: application/json
DNT: 1
Content-Type: application/json;charset=utf-8
Authorization: Bearer ...
Content-Length: 37
Connection: close

{
"deadline":{},
"name":"..."
}

What is the expected result?

HTTP/1.1 422 Data Validation Failed.

[
{
"field": "deadline",
"message": "Deadline must be an integer."
}
]

What do you get instead?

HTTP/1.1 500 Internal Server Error
Date: Wed, 27 Jul 2016 12:00:42 GMT
Content-Length: 2327
Connection: close
Content-Type: application/json; charset=UTF-8

{
"name": "PHP Notice",
"message": "Undefined offset: 0",
"code": 8,
"type": "yii\base\ErrorException",
"file": "/home/piotrek/workspace/icp.pl/api/app/vendor/yiisoft/yii2/db/Command.php",
"line": 330,
"stack-trace": [
"#0 /home/piotrek/workspace/icp.pl/api/app/vendor/yiisoft/yii2/db/Command.php(330): yii\base\ErrorHandler->handleError(8, 'Undefined offse...', '/home/piotrek/w...', 330, Array)",
"#1 /home/piotrek/workspace/icp.pl/api/app/vendor/yiisoft/yii2/db/Connection.php(639): yii\db\Command->bindValues(Array)",
"#2 /home/piotrek/workspace/icp.pl/api/app/vendor/yiisoft/yii2/db/pgsql/Schema.php(535): yii\db\Connection->createCommand('INSERT INTO "ta...', Array)",
"#3 /home/piotrek/workspace/icp.pl/api/app/vendor/yiisoft/yii2/db/ActiveRecord.php(457): yii\db\pgsql\Schema->insert('task', Array)",
"#4 /home/piotrek/workspace/icp.pl/api/app/vendor/yiisoft/yii2/db/ActiveRecord.php(427): yii\db\ActiveRecord->insertInternal(NULL)",
"#5 /home/piotrek/workspace/icp.pl/api/app/vendor/yiisoft/yii2/db/BaseActiveRecord.php(598): yii\db\ActiveRecord->insert(true, NULL)",
"#6 /home/piotrek/workspace/icp.pl/api/app/rest/versions/v1/modules/project/controllers/TaskController.php(295): yii\db\BaseActiveRecord->save()",
"#7 [internal function]: rest\versions\v1\modules\project\controllers\TaskController->actionCreate()",
"#8 /home/piotrek/workspace/icp.pl/api/app/vendor/yiisoft/yii2/base/InlineAction.php(55): call_user_func_array(Array, Array)",
"#9 /home/piotrek/workspace/icp.pl/api/app/vendor/yiisoft/yii2/base/Controller.php(154): yii\base\InlineAction->runWithParams(Array)",
"#10 /home/piotrek/workspace/icp.pl/api/app/vendor/yiisoft/yii2/base/Module.php(454): yii\base\Controller->runAction('create', Array)",
"#11 /home/piotrek/workspace/icp.pl/api/app/vendor/yiisoft/yii2/web/Application.php(87): yii\base\Module->runAction('v1/project/task...', Array)",
"#12 /home/piotrek/workspace/icp.pl/api/app/vendor/yiisoft/yii2/base/Application.php(375): yii\web\Application->handleRequest(Object(yii\web\Request))",
"#13 /home/piotrek/workspace/icp.pl/api/app/rest/web/index.php(29): yii\base\Application->run()",
"#14 {main}"
]
}

Additional info

Q A
Yii version 2.0.10
PHP version 7.0.8
Operating system Linux Ubuntu
@samdark
Copy link
Member

samdark commented Jul 27, 2016

It's default behavior. Set skipOnEmpty to false to change it.

@yii-bot
Copy link

yii-bot commented Jul 27, 2016

This is an automated comment, triggered by adding the label question.

Please note, that the GitHub Issue Tracker is for bug reports and feature requests only.

We are happy to help you on the support forum, on IRC (#yii on freenode), or Gitter.

Please use one of the above mentioned resources to discuss the problem.
If the result of the discussion turns out that there really is a bug in the framework, feel free to
come back and provide information on how to reproduce the issue. This issue will be closed for now.

@piotr-grzelka
Copy link
Author

When I set skipEmpty to false I can't send null values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants