Advertisement
Guest User

Untitled

a guest
Sep 16th, 2014
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. 'FelixkissUniqueWithValidatorUniqueWithValidatorServiceProvider',
  2. 'ProvidersTextareaNameValidatorServiceProvider',
  3.  
  4. $data = Input::except('submit');
  5. $data['parent'] = Input::get('parent') ?: null;
  6.  
  7.  
  8. $names = explode(PHP_EOL, trim($data['name']));
  9.  
  10. if (count($names) == 1) {
  11. $rules['name']
  12. = '
  13. required |
  14. unique_with:categories, parent = par_cat';
  15. }
  16. else {
  17. $rules['name'] = 'textareaname'; // my custom validator
  18. }
  19.  
  20.  
  21. $validator = Validator::make($data, $rules,
  22. Lang::get('forms.validation'));
  23.  
  24.  
  25. if ($validator->passes()) {
  26. // ...
  27.  
  28. <?php
  29.  
  30. namespace Providers;
  31.  
  32. use IlluminateSupportServiceProvider;
  33.  
  34. class TextareaNameValidatorServiceProvider extends ServiceProvider {
  35.  
  36. public function register(){}
  37.  
  38. public function boot()
  39. {
  40. $this->app->validator->resolver(function($translator, $data, $rules, $messages)
  41. {
  42. return new UtilsTextareaNameValidator($translator, $data, $rules, $messages);
  43. });
  44. }
  45.  
  46. }
  47.  
  48. <?php
  49.  
  50. namespace Utils;
  51.  
  52. use Validator;
  53.  
  54.  
  55. class TextareaNameValidator extends IlluminateValidationValidator
  56. {
  57. public function validateTextareaName($attribute, $value, $parameters)
  58. {
  59. $array = explode(PHP_EOL, trim($value));
  60.  
  61. if (count($array) !== count(array_unique($array))) {
  62. return false;
  63. }
  64.  
  65.  
  66. foreach ($array as $item) {
  67. $data['name'] = $item;
  68.  
  69.  
  70.  
  71. $rules['name']
  72. = '
  73. required |
  74. unique_with:categories, parent = par_cat ';
  75.  
  76. $validator = Validator::make($data, $rules);
  77.  
  78. if (!$validator->passes()) {
  79. return false;
  80. }
  81. }
  82.  
  83. return true;
  84. }
  85. }
  86.  
  87. 'FelixkissUniqueWithValidatorUniqueWithValidatorServiceProvider',
  88. 'ProvidersTextareaNameValidatorServiceProvider',
  89.  
  90. 'ProvidersTextareaNameValidatorServiceProvider',
  91. 'FelixkissUniqueWithValidatorUniqueWithValidatorServiceProvider',
  92.  
  93. // 'ProvidersTextareaNameValidatorServiceProvider',
  94.  
  95. Validator::extend('TextareaName', function($attribute, $value, $parameters)
  96. {
  97. $array = explode(PHP_EOL, trim($value));
  98.  
  99. if (count($array) !== count(array_unique($array))) {
  100. return false;
  101. }
  102.  
  103.  
  104. foreach ($array as $item) {
  105. $data['name'] = $item;
  106.  
  107.  
  108.  
  109. $rules['name']
  110. = '
  111. required |
  112. unique_with:categories, parent = par_cat ';
  113.  
  114. $validator = Validator::make($data, $rules);
  115.  
  116. if (!$validator->passes()) {
  117. return false;
  118. }
  119. }
  120.  
  121. return true;
  122. });
  123.  
  124. // 'ProvidersTextareaNameValidatorServiceProvider',
  125.  
  126. Validator::extend('textarea_name', 'UtilsTextAreaNameValidator@validateTextareaName');
  127.  
  128. <?php
  129.  
  130. namespace Utils;
  131.  
  132. use Validator;
  133.  
  134. class TextareaNameValidator
  135. {
  136. public function validateTextareaName($attribute, $value, $parameters)
  137. {
  138. $array = explode(PHP_EOL, trim($value));
  139.  
  140. if (count($array) !== count(array_unique($array))) {
  141. return false;
  142. }
  143.  
  144.  
  145. foreach ($array as $item) {
  146. $data['name'] = $item;
  147.  
  148.  
  149.  
  150. $rules['name']
  151. = '
  152. required
  153. |
  154. unique_with:categories, parent = par_cat
  155. ';
  156.  
  157. $validator = Validator::make($data, $rules);
  158.  
  159. if (!$validator->passes()) {
  160. return false;
  161. }
  162. }
  163.  
  164. return true;
  165. }
  166.  
  167. }
  168.  
  169. validateTextareaname
  170.  
  171. validateTextareaName
  172.  
  173. $rules['name'] = 'textareaname'; // my custom validator
  174.  
  175. $rules['name'] = 'textarea_name'; // my custom validator
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement