{"_id":"5772f824dceb570e003a922e","category":{"_id":"576ec7b7560eef0e00cd3096","version":"576ebdb79c84a31900958abd","__v":0,"project":"576ebdb79c84a31900958aba","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2016-06-25T18:04:39.266Z","from_sync":false,"order":3,"slug":"deployment","title":"Basics"},"version":{"_id":"576ebdb79c84a31900958abd","project":"576ebdb79c84a31900958aba","__v":10,"createdAt":"2016-06-25T17:21:59.854Z","releaseDate":"2016-06-25T17:21:59.854Z","categories":["576ebdb79c84a31900958abe","576ebfc59c84a31900958ac4","576ec32f52f96619007cfb9a","576ec7b7560eef0e00cd3096","576ed4249c84a31900958add","576ed429560eef0e00cd30a3","576ed43a52f96619007cfbb5","576ed44d5a8c72170082b794","577212f20da40019004f0816","57725c7e0a6d610e00de9e4c"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"3.3.0","version":"3.3"},"__v":14,"parentDoc":null,"project":"576ebdb79c84a31900958aba","user":"576ebd239c84a31900958ab9","updates":[],"next":{"pages":[],"description":""},"createdAt":"2016-06-28T22:20:20.182Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":6,"body":"`Orchestra\\Support\\Validator` is a class based on \"Validation as a Service\" concept. It is however enhanced to support scenarios where you can define different ruleset for different actions throughout your application.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Basic Usage\"\n}\n[/block]\nTo define a basic rules, all you need to do is create a validation class, e.g:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<?php \\n\\nnamespace App\\\\Validations;\\n\\nuse Orchestra\\\\Support\\\\Validator;\\n\\nclass User extends Validator\\n{\\n\\tprotected $rules = [\\n\\t\\t'email' => ['required', 'email'],\\n\\t];\\n}\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nTo run validation for `App\\Validations\\User`, all you need to do is:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<?php\\n\\nnamespace App\\\\Http\\\\Controllers;\\n\\nuse Illuminate\\\\Http\\\\Request;\\nuse App\\\\Validations\\\\User as UserValidator;\\n\\nclass UserController extends Controller\\n{\\n\\t/* ... */\\n  \\n  public function store(Request $request, UserValidator $validator)\\n  {\\n    $validation = $validator->with($request->all());\\n    \\n    if ($validation->fails()) {\\n      return redirect()->back()->withInput()->withErrors($validation);\\n    }\\n    \\n    // Proceed with storing the user.\\n  }\\n}\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n\n[block:callout]\n{\n  \"type\": \"info\",\n  \"body\": \"`$validation` would return an instance of `Illuminate\\\\Validation\\\\Validator` so you can utilise it as you normally would do using the default Validation on Laravel.\"\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Scenario based Rules\"\n}\n[/block]\nTo allow the validation to run a scenario based ruleset you can just add the following `on()` method in the declaration:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<?php\\n\\nnamespace App\\\\Http\\\\Controllers;\\n\\nuse App\\\\User;\\nuse Illuminate\\\\Http\\\\Request;\\nuse App\\\\Validations\\\\User as UserValidator;\\n\\nclass UserController extends Controller\\n{\\n\\t/* ... */\\n  \\n  public function update(Request $request, UserValidator $validator, User $user)\\n  {\\n    $validation = $validator->on('update')->with($request->all());\\n    \\n    if ($validation->fails()) {\\n      return redirect()->back()->withInput()->withErrors($validation);\\n    }\\n    \\n    // Proceed with updating the user.\\n  }\\n}\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nand to add the additional ruleset simply append `App\\Validations\\User` with `onUpdate()` method such as below:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<?php \\n\\nnamespace App\\\\Validations;\\n\\nuse Orchestra\\\\Support\\\\Validator;\\n\\nclass User extends Validator\\n{\\n\\t/* ... */\\n\\n\\tpublic function onUpdate()\\n\\t{\\n\\t\\t$this->rules['password'] = ['required'];\\n\\t}\\n}\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Parameter Bindings on Rules\"\n}\n[/block]\nThere could be time when a rules would require parameter from the request, for example the if you need to check and ensure that email address is unique, but should skip if user is updating own account.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<?php \\n\\nnamespace App\\\\Validations;\\n\\nuse Orchestra\\\\Support\\\\Validator;\\n\\nclass User extends Validator\\n{\\n\\t/* ... */\\n\\n\\tpublic function onUpdate()\\n\\t{\\n\\t\\t$this->rules['email'] = ['required', 'unique:users,email,{id}'];\\n\\t}\\n}\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nAnd you can just bind the `{id}` (or any other parameters) using:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<?php\\n\\nnamespace App\\\\Http\\\\Controllers;\\n\\nuse App\\\\User;\\nuse Illuminate\\\\Http\\\\Request;\\nuse App\\\\Validations\\\\User as UserValidator;\\n\\nclass UserController extends Controller\\n{\\n\\t/* ... */\\n  \\n  public function update(Request $request, UserValidator $validator, User $user)\\n  {\\n    $validation = $validator->on('update')->bind(['id' => $user->id])->with($request->all());\\n    \\n    if ($validation->fails()) {\\n      return redirect()->back()->withInput()->withErrors($validation);\\n    }\\n    \\n    // Proceed with updating the user.\\n  }\\n}\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]","excerpt":"","slug":"validation","type":"basic","title":"Validation"}
`Orchestra\Support\Validator` is a class based on "Validation as a Service" concept. It is however enhanced to support scenarios where you can define different ruleset for different actions throughout your application. [block:api-header] { "type": "basic", "title": "Basic Usage" } [/block] To define a basic rules, all you need to do is create a validation class, e.g: [block:code] { "codes": [ { "code": "<?php \n\nnamespace App\\Validations;\n\nuse Orchestra\\Support\\Validator;\n\nclass User extends Validator\n{\n\tprotected $rules = [\n\t\t'email' => ['required', 'email'],\n\t];\n}", "language": "php" } ] } [/block] To run validation for `App\Validations\User`, all you need to do is: [block:code] { "codes": [ { "code": "<?php\n\nnamespace App\\Http\\Controllers;\n\nuse Illuminate\\Http\\Request;\nuse App\\Validations\\User as UserValidator;\n\nclass UserController extends Controller\n{\n\t/* ... */\n \n public function store(Request $request, UserValidator $validator)\n {\n $validation = $validator->with($request->all());\n \n if ($validation->fails()) {\n return redirect()->back()->withInput()->withErrors($validation);\n }\n \n // Proceed with storing the user.\n }\n}", "language": "php" } ] } [/block] [block:callout] { "type": "info", "body": "`$validation` would return an instance of `Illuminate\\Validation\\Validator` so you can utilise it as you normally would do using the default Validation on Laravel." } [/block] [block:api-header] { "type": "basic", "title": "Scenario based Rules" } [/block] To allow the validation to run a scenario based ruleset you can just add the following `on()` method in the declaration: [block:code] { "codes": [ { "code": "<?php\n\nnamespace App\\Http\\Controllers;\n\nuse App\\User;\nuse Illuminate\\Http\\Request;\nuse App\\Validations\\User as UserValidator;\n\nclass UserController extends Controller\n{\n\t/* ... */\n \n public function update(Request $request, UserValidator $validator, User $user)\n {\n $validation = $validator->on('update')->with($request->all());\n \n if ($validation->fails()) {\n return redirect()->back()->withInput()->withErrors($validation);\n }\n \n // Proceed with updating the user.\n }\n}", "language": "php" } ] } [/block] and to add the additional ruleset simply append `App\Validations\User` with `onUpdate()` method such as below: [block:code] { "codes": [ { "code": "<?php \n\nnamespace App\\Validations;\n\nuse Orchestra\\Support\\Validator;\n\nclass User extends Validator\n{\n\t/* ... */\n\n\tpublic function onUpdate()\n\t{\n\t\t$this->rules['password'] = ['required'];\n\t}\n}", "language": "php" } ] } [/block] [block:api-header] { "type": "basic", "title": "Parameter Bindings on Rules" } [/block] There could be time when a rules would require parameter from the request, for example the if you need to check and ensure that email address is unique, but should skip if user is updating own account. [block:code] { "codes": [ { "code": "<?php \n\nnamespace App\\Validations;\n\nuse Orchestra\\Support\\Validator;\n\nclass User extends Validator\n{\n\t/* ... */\n\n\tpublic function onUpdate()\n\t{\n\t\t$this->rules['email'] = ['required', 'unique:users,email,{id}'];\n\t}\n}", "language": "php" } ] } [/block] And you can just bind the `{id}` (or any other parameters) using: [block:code] { "codes": [ { "code": "<?php\n\nnamespace App\\Http\\Controllers;\n\nuse App\\User;\nuse Illuminate\\Http\\Request;\nuse App\\Validations\\User as UserValidator;\n\nclass UserController extends Controller\n{\n\t/* ... */\n \n public function update(Request $request, UserValidator $validator, User $user)\n {\n $validation = $validator->on('update')->bind(['id' => $user->id])->with($request->all());\n \n if ($validation->fails()) {\n return redirect()->back()->withInput()->withErrors($validation);\n }\n \n // Proceed with updating the user.\n }\n}", "language": "php" } ] } [/block]