Support the ongoing development of Laravel.io →
Eloquent Validation Packages
Last updated 2 years ago.
0

At first I used exceptions with saveOrFail() which worked, but I kept accidentally using save() which didn't. So I switched to enabling $throwValidationExceptions so that I'm always alerted when validation fails. I also had trouble with $rules and $messages so switched to using $rulesets and $validationMessages. Here is an example that works for me, hope this helps someone:

class User extends Eloquent implements UserInterface, RemindableInterface {
	
	use UserTrait, RemindableTrait, ValidatingTrait;
	
	protected $throwValidationExceptions = true;	// always throw validation exceptions
	
	/*protected $rules = [
		'username'	=> 'unique'
	];*/
	
	protected $rulesets = [
		'creating' => [
			'username'	=> 'required|unique'
		],
		'updating' => [
			'id'		=> 'required'
		],
		'deleting' => [
			'id'		=> 'required'
		]
	];
	
	protected $validationMessages = [
		'username.unique' => "That username is not available."
	];
	
	// ...
}
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Albert221 albert221 Joined 13 Apr 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.