2

Im creating some small API with which i can create blog posts through postman. Im using laravel 5 and i encoutered a problem.

In the API call, i can specify a user id, so the post is written by someone else than whoever makes the API call right now. My currently logged in user is specified with a token in postman.

So my problem is now, when i create the API call and specify my user_id as empty string "userID": ""

it will throw an error, because i specified the id to be an int like so

'userID' => ['integer']

The error is

"Type error: Argument passed to construct() must be of the type integer or null, string given",

Why does it accept an empty string? How can i validate that correctly? Note that the userID doesn't have to be specified in the post request. If not specified, it will just take the one from the user you are currently logged in with. (Specified in the token)

0

3 Answers 3

5

Using two validation rules together will fix the issue. required and integer

and as you said, you dont want to make it mandatory:

use these validation rules combination:

nullable|integer
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry for not specifying it earlier, but they dont have to parse the userid, if not parsed, it will take the user id from the user you are currently logged in with.
Your welcome. Thank you so much for accepting my answer. Can you vote it up?
2

I had this problem. So, I found this description in the laravel docs:

"By default, when an attribute being validated is not present or contains an empty string, normal validation rules, including custom extensions, are not run."

So... when you have an attribute with empty string data, the normal validation rules are not executed.

So... I had an idea to solve this problem which was override the function setAttribute in my models:

public function setAttribute($key, $value)
{
   parent::setAttribute($key, $value);

    if (is_string($value))
    {
        $this->attributes[$key] = empty(trim($value)) ? null : $value;
    }
}

So... All times that will have to save de model in your database, the empty values will be converted in a null values.

Therefore, if the data is null or integer, use the "nullable | integer" rule to use the validation rule. If the data is an empty string, the validation rule will not be considered, but the data will be converted to an empty string before being saved to the database.

I created a my BaseModel(abstract class) with the method setAttribute and all my models inherit this class.

abstract class BaseModel extends Model
{

   public function setAttribute($key, $value)
   {
      parent::setAttribute($key, $value);

       if (is_string($value))
       {
           $this->attributes[$key] = empty(trim($value)) ? null : $value;
       }
   } ...

Comments

-1

You need to make userId field required as this:

'userId' => 'required|integer'

1 Comment

Sorry for my terrible clarificaiton, i added my initial post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.