nilportugues / php-json-api

JSON API transformer outputting valid (PSR-7) API Responses.
http://nilportugues.com
MIT License
71 stars 35 forks source link

Validation and Hidden Fields #49

Closed apburton84 closed 8 years ago

apburton84 commented 8 years ago

If I had the following validation rule

protected $rules = [
    'password'  =>  'required|valid_password|min:8'
];

And if I wished to hide that field from the response

protected $hidden = ['password'];

My password setter is:

public function setPasswordAttribute($value)
{
    $validator = \Validator::make(['password'=>$value], $this->getRules(), $this->getMessages());

    if ($validator->fails()) {
        $this->attributes['password'] = $value;
        return $this;
    }

    $this->attributes['password'] = password_hash($value, PASSWORD_BCRYPT);

    return $this;
}

Then when testing this functionality with the following

$user = new User();
$user->password = 'xxx'

$user->isValid();

dd($user->getErrors());

I get the following error

Illuminate\Support\MessageBag {#328
    #messages: array:1 [
        "password" => array:1 [
            0 => "The password field is required."
        ]
     ]
     #format: ":message"
 }

If I comment out the

protected $hidden = ['password'];

Everything works as expected

:-)

apburton84 commented 8 years ago

Should this issue be in https://github.com/nilportugues/laravel5-jsonapi ? I can move it if so

nilportugues commented 8 years ago

@apburton84 yeah this belongs to laravel5-jsonapi.

nilportugues commented 8 years ago

@apburton84 you can hide it from the response using the mapping file from the serializer :+1:

apburton84 commented 8 years ago

@nilportugues Thankyou