Laravel-validation
- Abnormal Form Verification
Reasonable Use of exception-based programming methods can greatly improve code maintainability.
Preparations before use
Declare the dependency in the composer. JSON file:
"five-say/laravel-validation": "1.*"
In/app/config/app.phpSet "alias" in"
'Aliases' => array (... 'validation '=> 'fivesay \ laravelvalidation \ facade', // exception form verification ),
Usage
Try {validation: Make (Array ('name' => 'requestred',); // other operations after verification ...} catch (fivesay \ validationexception $ e) {return redirect: Back ()-> witherrors ($ e-> errors );}Custom verification message
Validation: Make (Array ('name' => 'required',), array ('name. required' => 'custom verification message ',));Special usage (throwing an exception directly)
Validation::throwIt(‘name‘, ‘test error message.‘);
```phpValidation::throwIt(array( ‘name‘ => ‘test error message.‘, ‘email‘ => ‘test error message.‘,));
It's time to introduce exception-based programming!
We can write the entire business logic without interference, and then refine the error handling for captured exceptions. You will feel that the Code has never been written smoothly, the readability of the entire code is also greatly improved.
/*** Create * @ return response */Public Function store () {try {# Form validation: Make (Array ('account' => 'required |: 3, 50 | unique: Users ', 'Password' => 'required | between: 5, 32', 'password _ confirm' => 'required | same: password ', 'name' => 'required | min: 2', 'mobiles' => 'multi _ mobile',); # create a user account $ user = User :: create (Array ('activated' => true) // force activation + input: only ('account', 'Password', 'name ')) -> setgroupto ('employee exception'); # create employee information $ Staff = Staff: Create (Array ('user _ id' => $ user-> ID, 'model' => 'demotion ',) + input: only ('name', 'mobiles'); # create a foreground $ demotion = demotion :: create (Array ('user _ id' =>$ user-> ID, 'staff _ id' =>$ staff-> ID,) + input :: only ('name'); # Return redirect: Route ('home')-> withsuccess ('Operation successfully');} catch (fivesay \ validationexception $ E) {return redirect: Back ()-> witherrors ($ e-> errors);} catch (usersavefailexception $ e) {return redirect: Back () -> witherror ('account information writing failed');} catch (staffsavefailexception $ e) {return redirect: Back ()-> witherror ('employee information writing failed ');} catch (exceptionsavefailexception $ e) {return redirect: Back ()-> witherror ('front-end information writing failed ');}}Model exception
In the above exampleUserSaveFailExceptionThis is the result of model exception. There are only two steps to achieve this effect. Let's startUserFor example:
1. InheritanceFiveSay\Model(Don't worry, ourFiveSay\ModelInheritedEloquent):
class User extends FiveSay\Model{ ...
2.User.phpThree exception classes are defined at the bottom of the file:
class User extends FiveSay\Model{ ...}class UserNotFindException extends Exception {}class UserSaveFailException extends Exception {}class UserDeleteFailException extends Exception {}Additional support-model observer (Model event monitoring)
InheritedFiveSay\ModelWhen the corresponding "Model observer class "(XxxxObserver) Is automatically loaded.
SimilarlyUserFor example: When you define a model anywhere in the systemUserObserverThis class will be automatically registered as "Model observer ".
class UserObserver { public function saving($model) { // } public function saved($model) { // }}Project address
5-say/laravel-validation
It's time to introduce exception-based programming!