Last active
April 9, 2017 07:54
-
-
Save paanblogger/32d51d522a56d3255cef277cff29f728 to your computer and use it in GitHub Desktop.
Cakephp 3 : Dynamic Validation Rules
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class UsersController extends AppController | |
{ | |
$user->type = "order"; | |
/* Success save */ | |
if($this->Users->save($user)) | |
{ | |
} | |
/* Failed save */ | |
else | |
{ | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Users extends Table | |
{ | |
public function buildRules(RulesChecker $rules) | |
{ | |
$check = function(user){ | |
if($user->type == "order") | |
{ | |
return false; | |
} | |
else | |
{ | |
return true; | |
} | |
}; | |
if($check) | |
$rules->add($rules->isUnique(['email' , 'prospect_groups_id'])); | |
return $rules; | |
} | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Users extends Table | |
{ | |
public function buildRules(RulesChecker $rules) | |
{ | |
$check = function($user) { | |
return $user->price < 100 && $user->shipping_mode === 'free'; | |
}; | |
$rules->add($check, [ | |
'errorField' => 'shipping_mode', | |
'message' => 'No free shipping for orders under 100!' | |
]); | |
return $rules; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment