@@ -52,6 +52,14 @@ abstract class Ardent extends Model {
5252 */
5353 public static $ customAttributes = array ();
5454
55+ /**
56+ * The validator object in case you need it externally (say, for a form builder).
57+ *
58+ * @see getValidator()
59+ * @var \Illuminate\Validation\Validator
60+ */
61+ protected $ validator ;
62+
5563 /**
5664 * The message bag instance containing validation error messages
5765 *
@@ -535,8 +543,8 @@ public function validate(array $rules = array(), array $customMessages = array()
535543 $ data = $ this ->getAttributes (); // the data under validation
536544
537545 // perform validation
538- $ validator = static ::makeValidator ($ data , $ rules , $ customMessages , $ customAttributes );
539- $ success = $ validator ->passes ();
546+ $ this -> validator = static ::makeValidator ($ data , $ rules , $ customMessages , $ customAttributes );
547+ $ success = $ this -> validator ->passes ();
540548
541549 if ($ success ) {
542550 // if the model is valid, unset old errors
@@ -545,7 +553,7 @@ public function validate(array $rules = array(), array $customMessages = array()
545553 }
546554 } else {
547555 // otherwise set the new ones
548- $ this ->validationErrors = $ validator ->messages ();
556+ $ this ->validationErrors = $ this -> validator ->messages ();
549557
550558 // stash the input to the current session
551559 if (!self ::$ externalValidator && Input::hasSession ()) {
@@ -789,7 +797,8 @@ protected function buildUniqueExclusionRules(array $rules = array()) {
789797
790798 foreach ($ ruleset as &$ rule ) {
791799 if (strpos ($ rule , 'unique ' ) === 0 ) {
792- $ params = explode (', ' , $ rule );
800+ // Stop splitting at 4 so final param will hold optional where clause
801+ $ params = explode (', ' , $ rule , 4 );
793802
794803 $ uniqueRules = array ();
795804
@@ -808,7 +817,9 @@ protected function buildUniqueExclusionRules(array $rules = array()) {
808817
809818 if (isset ($ this ->primaryKey )) {
810819 $ uniqueRules [3 ] = $ this ->{$ this ->primaryKey };
811- $ uniqueRules [4 ] = $ this ->primaryKey ;
820+
821+ // If optional where rules are passed, append them otherwise use primary key
822+ $ uniqueRules [4 ] = isset ($ params [3 ]) ? $ params [3 ] : $ this ->primaryKey ;
812823 }
813824 else {
814825 $ uniqueRules [3 ] = $ this ->id ;
@@ -899,4 +910,12 @@ public function newQuery($excludeDeleted = true) {
899910
900911 return $ builder ;
901912 }
913+
914+ /**
915+ * Returns the validator object created after {@link validate()}.
916+ * @return \Illuminate\Validation\Validator
917+ */
918+ public function getValidator () {
919+ return $ this ->validator ;
920+ }
902921}
0 commit comments