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

validation match[x] don't work anymore... if custom setter is used. #2006

Closed
nowackipawel opened this issue May 15, 2019 · 3 comments
Closed

Comments

@nowackipawel
Copy link
Contributor

In entity:

	public function setUsrPassword($value)
	{
//$this->usr_password 
		$this->attributes['usr_password'] = password_hash('my-password', PASSWORD_BCRYPT, ['cost' => 11]);
	}

in model:

		$this->validationRules['usr_password_1'] = 'required|matches[usr_password]';

I think it is because custom set is called now before validation... before last changes it was called before save action.

@nowackipawel
Copy link
Contributor Author

Additionally

this code in Entity:

	public function setUsrPassword($value)
	{
		$this->attributes['usr_password'] = password_hash( trim($value) . config('App')->salt, PASSWORD_BCRYPT, ['cost' => 11]);
	}

makes value $user_entity->usr_password different every time.

@lonnieezell
Copy link
Member

I'll look into the validation issue, but there's a simple fix for the second part you brought up: use a different field name. For example, in my auth stuff, I've got a field in the database called password_hash to be clear about what's being saved. Then in the Entity there is a setPassword() method which does the same as yours. However, it's setting the value of the password_hash column and that setter is only called if the password has been changed.

If you save the password field (using your setUsrPassword method) then, yes, the hash will be different every time. That's how password_hash works.

@lonnieezell
Copy link
Member

Custom sets will always be called on an Entity prior to validation if you're doing validation in the model.

For certain things, like validating two fields that must match each other, that's best done in the controller before you try to save to database.

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

No branches or pull requests

2 participants