The only solution I've found to this problem, is to not use the 'unique' and 'confirmed' validation rules. I will handle validation for these upon form submission only.
I'm actually not very happy with this solution, so I came up with a slightly different approach:
// Validation rules for fields in this entity
public static $rules = array(
'username' => 'required|alpha_dash|min:4|max:40',
'password' => 'required|alpha_dash|min:6|max:20',
'password_confirmation' => ''
);
// Extra validation for user creation
public static $onCreateRules = array(
'username' => 'required|alpha_dash|min:4|max:40|unique:users',
'password' => 'required|alpha_dash|min:6|max:20|confirmed'
);
Here I split the validation rules into two static arrays, and instead of extending the class from Ardent directly I extend it from a custom class:
use LaravelBook\Ardent\Ardent;
class ModelBase extends Ardent {
public function save(array $rules = array(), array $customMessages = array(), array $options = array(), Closure $beforeSave = null, Closure $afterSave = null) {
if (!$this->exists && isset(static::$onCreateRules)) {
$rules = array_merge($rules, static::$onCreateRules);
}
return parent::save($rules, $customMessages, $options, $beforeSave, $afterSave);
}
}
This overrides the Ardent save method and applies the $onCreateRules for validation ONLY if this entity does not previously exist. :-)
I still think Eloquents validation engine is broken. A call to validate() should only validate dirty fields for one. Secondly it should automatically exclude the current entity id from a unique check. So in any case the solution I present here is a workaround.
I hope someone from Laravel sees this and finds it in their heart to fix it.