From 83036103b42615b2575219fc2ae268bd31a5a1cd Mon Sep 17 00:00:00 2001 From: Maxim Kerstens Date: Sun, 28 Jun 2015 13:37:53 +0200 Subject: [PATCH 01/16] removed use of a view:shared dependency use collective/html instead of illuminate's since it's no longer under development added usage instructions --- README.md | 25 ++++++ composer.json | 4 +- src/Idma/LaravelParsley/FormBuilder.php | 80 +++++++------------ .../LaravelParsleyServiceProvider.php | 4 +- src/Idma/LaravelParsley/ParsleyConverter.php | 10 ++- 5 files changed, 64 insertions(+), 59 deletions(-) diff --git a/README.md b/README.md index 137e27a..e3ffd76 100644 --- a/README.md +++ b/README.md @@ -2,3 +2,28 @@ laravel-parsley =============== Converts FormRequest rules to [Parsley](http://parsleyjs.org/) rules. + + +## Install + +If you have previously set up LaravelCollective/Html you can remove its service provider from `app/config` + +in `app/config` add the following under service providers: + +`Idma\LaravelParsley\LaravelParsleyServiceProvider` + +If you haven't already, add these facades: + + 'Form' => 'Collective\Html\FormFacade', + 'Html' => 'Collective\Html\HtmlFacade', + +## Useage + +All that's needed is for you to supply the name of the `FormRequest` in the `request` key when opening a form. + + Form::open(['request' => 'YourFormRequestClass']) + Form::model(['request' => 'YourFormRequestClass']) + +Lastly you should include parsley's scripts on the page and activate parsley for your form. + +easy enough don't you think? \ No newline at end of file diff --git a/composer.json b/composer.json index 358d1e1..6f2a3fa 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "idma/laravel-parsley", "description": "Converts FormRequest rules to Parsley rules", - "keywords": ["laravel", "laravel 5", "parsley", "parsleyjs", "html", "form"], + "keywords": ["laravel", "laravel 5", "parsley", "parsleyjs", "html", "form", "collective"], "authors": [ { "name": "idma", @@ -11,7 +11,7 @@ "require": { "php": ">=5.4.0", "illuminate/support": "~5.0", - "illuminate/html": "~5.0", + "laravelcollective/html": "~5.0", "illuminate/translation" : "~5.0" }, "autoload": { diff --git a/src/Idma/LaravelParsley/FormBuilder.php b/src/Idma/LaravelParsley/FormBuilder.php index 7a131f3..7b7ca32 100644 --- a/src/Idma/LaravelParsley/FormBuilder.php +++ b/src/Idma/LaravelParsley/FormBuilder.php @@ -2,103 +2,76 @@ namespace Idma\LaravelParsley; -use Illuminate\Html\FormBuilder as BaseFormBuilder; +use Collective\Html\FormBuilder as BaseFormBuilder; -class FormBuilder extends BaseFormBuilder { +class FormBuilder extends BaseFormBuilder +{ protected $modelName = null; /** * @type ParsleyConverter */ - protected $parsley = null; + protected $parsley = null; /** * {@inheritdoc} */ - public function open(array $options = []) { - $this->parsley = new ParsleyConverter(); - - if ($this->model && !isset($options['method'])) { - $options['method'] = $this->model->getAttribute('id') ? 'put' : 'post'; - } - - $method = strtoupper(array_get($options, 'method', 'post')); - - // We need to extract the proper method from the attributes. If the method is - // something other than GET or POST we'll use POST since we will spoof the - // actual method since forms don't support the reserved methods in HTML. - $attributes['method'] = $this->getMethod($method); - - $attributes['action'] = $this->getAction($options); - - $attributes['accept-charset'] = 'UTF-8'; - - // If the method is PUT, PATCH or DELETE we will need to add a spoofer hidden - // field that will instruct the Symfony request to pretend the method is a - // different method than it actually is, for convenience from the forms. - $append = $this->getAppendage($method); - - if (isset($options['files']) && $options['files']) { - $options['enctype'] = 'multipart/form-data'; - } - - // Finally we're ready to create the final form HTML field. We will attribute - // format the array of attributes. We will also add on the appendage which - // is used to spoof requests for this PUT, PATCH, etc. methods on forms. - $attributes = array_merge($attributes, array_except($options, $this->reserved)); - - // Finally, we will concatenate all of the attributes into a single string so - // we can build out the final form open statement. We'll also append on an - // extra value for the hidden _method field if it's needed for the form. - $attributes = $this->html->attributes($attributes); + public function open(array $options = []) + { + $this->reserved[] = 'request'; + $this->parsley = new ParsleyConverter(array_get($options, 'request', null)); - return ''.$append; + return parent::open($options); } /** * {@inheritdoc} */ - public function model($model, array $options = []) { + public function model($model, array $options = []) + { $this->setModel($model); return $this->open($options); } - public function openModel($model, array $options = []) { + public function openModel($model, array $options = []) + { return $this->model($model, $options); } /** * {@inheritdoc} */ - public function label($name, $value = null, $options = []) { + public function label($name, $value = null, $options = []) + { $this->labels[] = $name; $options = $this->html->attributes($options); $value = e($this->formatLabel($name, $value)); - $for = ($this->modelName && !starts_with($name, '_')) ? $this->modelName.'-'.$name : $name; + $for = ($this->modelName && !starts_with($name, '_')) ? $this->modelName . '-' . $name : $name; - return ''; + return ''; } /** * Create a Bootstrap-like help block. * * @param string $value - * @param array $options + * @param array $options * * @return string */ - public function helpBlock($value, array $options = []) { + public function helpBlock($value, array $options = []) + { if (isset($options['class'])) { - $options['class'] = 'help-block '.$options['class']; + $options['class'] = 'help-block ' . $options['class']; } else { $options['class'] = 'help-block'; } - return 'html->attributes($options).'>'.$value.''; + return 'html->attributes($options) . '>' . $value . ''; } /** @@ -147,7 +120,8 @@ public function select($name, $list = [], $selected = null, $options = []) // return null; // } - public function setModel($model) { + public function setModel($model) + { $this->model = $model; $this->modelName = strtolower((new \ReflectionClass($this->model))->getShortName()); } @@ -157,11 +131,13 @@ public function setModel($model) { * * @return string */ - public function getModelName() { + public function getModelName() + { return $this->modelName; } - public function name() { + public function name() + { return $this->getModelName(); } } diff --git a/src/Idma/LaravelParsley/LaravelParsleyServiceProvider.php b/src/Idma/LaravelParsley/LaravelParsleyServiceProvider.php index 65121b2..437bd5b 100644 --- a/src/Idma/LaravelParsley/LaravelParsleyServiceProvider.php +++ b/src/Idma/LaravelParsley/LaravelParsleyServiceProvider.php @@ -2,7 +2,7 @@ namespace Idma\LaravelParsley; -use Illuminate\Html\HtmlServiceProvider; +use Collective\Html\HtmlServiceProvider; class LaravelParsleyServiceProvider extends HtmlServiceProvider { @@ -11,8 +11,6 @@ class LaravelParsleyServiceProvider extends HtmlServiceProvider */ public function register() { - $this->package('idma/parsley-laravel'); - parent::register(); } diff --git a/src/Idma/LaravelParsley/ParsleyConverter.php b/src/Idma/LaravelParsley/ParsleyConverter.php index 12af147..ac2a1d1 100644 --- a/src/Idma/LaravelParsley/ParsleyConverter.php +++ b/src/Idma/LaravelParsley/ParsleyConverter.php @@ -5,6 +5,8 @@ use Illuminate\Translation\Translator; class ParsleyConverter { + use \Illuminate\Console\AppNamespaceDetectorTrait; + protected $rules = []; protected $customAttributes = []; @@ -13,9 +15,13 @@ class ParsleyConverter { */ protected $translator = null; - public function __construct() + public function __construct($formRequest=null) { - $formRequest = \View::shared('_ilp_request'); + if($formRequest != null && !is_object($formRequest)) + { + $class = $this->getAppNamespace() . 'Http\Requests\\'.$formRequest; + $formRequest = new $class; + } if ($formRequest && method_exists($formRequest, 'rules')) { $this->rules = $formRequest->rules(); From 597ab4185dd5e9e956e30102febc511c1f103924 Mon Sep 17 00:00:00 2001 From: Maxim Kerstens Date: Mon, 29 Jun 2015 09:18:03 +0200 Subject: [PATCH 02/16] Added URL validation, removed unnecessary code, updated read me --- README.md | 27 +++++++++++++++++++- src/Idma/LaravelParsley/FormBuilder.php | 25 ------------------ src/Idma/LaravelParsley/ParsleyConverter.php | 6 ++++- 3 files changed, 31 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index e3ffd76..02cd315 100644 --- a/README.md +++ b/README.md @@ -26,4 +26,29 @@ All that's needed is for you to supply the name of the `FormRequest` in the `req Lastly you should include parsley's scripts on the page and activate parsley for your form. -easy enough don't you think? \ No newline at end of file +easy enough don't you think? + +### Validation rules that are implemented + + - [x] required + - [x] remail + - [x] min + - [x] max + - [x] between + - [x] integer + - [x] url + - [x] alpha_num + - [x] alpha_dash + - [x] alpha + - [x] regex + - [x] confirmed + + - [ ] accepted + - [ ] after:(date) + - [ ] before:(date) + - [ ] boolean + - [ ] date_format + - [ ] different + - [ ] in + - [ ] not_in + - [ ] ip_address \ No newline at end of file diff --git a/src/Idma/LaravelParsley/FormBuilder.php b/src/Idma/LaravelParsley/FormBuilder.php index 7b7ca32..db09fb1 100644 --- a/src/Idma/LaravelParsley/FormBuilder.php +++ b/src/Idma/LaravelParsley/FormBuilder.php @@ -101,31 +101,6 @@ public function select($name, $list = [], $selected = null, $options = []) return parent::select($name, $list, $selected, $options); } - /** - * {@inheritdoc} - */ -// public function getIdAttribute($name, $attributes) { -// $id = null; -// -// if (array_key_exists('id', $attributes)) { -// $id = $attributes['id']; -// } else { -// $id = $name; -// } -// -// if ($this->modelName && $id && !starts_with($id, '_')) { -// return $this->modelName.'-'.$id; -// } -// -// return null; -// } - - public function setModel($model) - { - $this->model = $model; - $this->modelName = strtolower((new \ReflectionClass($this->model))->getShortName()); - } - /** * Gets the short model name. * diff --git a/src/Idma/LaravelParsley/ParsleyConverter.php b/src/Idma/LaravelParsley/ParsleyConverter.php index ac2a1d1..67c940e 100644 --- a/src/Idma/LaravelParsley/ParsleyConverter.php +++ b/src/Idma/LaravelParsley/ParsleyConverter.php @@ -37,7 +37,6 @@ public function __construct($formRequest=null) public function getFieldRules($field) { $rules = []; - if (isset($this->rules[$field])) { $rawRules = explode('|', $this->rules[$field]); @@ -93,6 +92,11 @@ public function convertRules($attribute, $rules) $parsleyRule = 'integer'; break; + case 'url': + $parsleyRule = 'type'; + $params = 'url'; + break; + case 'alpha_num': $parsleyRule = 'alphanum'; $params = '/^\d[a-zа-яё\-\_]+$/i'; From b1b2b79186948863aaf5badb2a62d4a4dbc424b5 Mon Sep 17 00:00:00 2001 From: Maxim Kerstens Date: Mon, 29 Jun 2015 09:23:57 +0200 Subject: [PATCH 03/16] reformatted readme --- README.md | 40 ++++++++++++++++------------------------ 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 02cd315..bbeddc1 100644 --- a/README.md +++ b/README.md @@ -28,27 +28,19 @@ Lastly you should include parsley's scripts on the page and activate parsley for easy enough don't you think? -### Validation rules that are implemented - - - [x] required - - [x] remail - - [x] min - - [x] max - - [x] between - - [x] integer - - [x] url - - [x] alpha_num - - [x] alpha_dash - - [x] alpha - - [x] regex - - [x] confirmed - - - [ ] accepted - - [ ] after:(date) - - [ ] before:(date) - - [ ] boolean - - [ ] date_format - - [ ] different - - [ ] in - - [ ] not_in - - [ ] ip_address \ No newline at end of file +### Validation rules + +Implemented | Not implemented +---------------|----------------- + [x] required | [ ] accepted + [x] email | [ ] after:(date) + [x] min | [ ] before:(date) + [x] max | [ ] before:(date) + [x] between | [ ] date_format + [x] integer | [ ] different + [x] url | [ ] in + [x] alpha_num | [ ] not_in + [x] alpha_dash| [ ] ip_address + [x] alpha | + [x] regex | + [x] confirmed | \ No newline at end of file From e5a567ba289ae1f83a53d82fe1149bc2d8d7f536 Mon Sep 17 00:00:00 2001 From: Maxim Kerstens Date: Mon, 29 Jun 2015 09:27:32 +0200 Subject: [PATCH 04/16] removed check boxes --- README.md | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index bbeddc1..f850ba0 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,12 @@ laravel-parsley =============== -Converts FormRequest rules to [Parsley](http://parsleyjs.org/) rules. +Converts [FormRequest](http://laravel.com/docs/5.0/validation#form-request-validation) rules to [Parsley](http://parsleyjs.org/) rules. ## Install -If you have previously set up LaravelCollective/Html you can remove its service provider from `app/config` +If you have previously set up `LaravelCollective/Html` or `Illuminate/Html` you can remove its service provider from `app/config` in `app/config` add the following under service providers: @@ -30,17 +30,17 @@ easy enough don't you think? ### Validation rules -Implemented | Not implemented ----------------|----------------- - [x] required | [ ] accepted - [x] email | [ ] after:(date) - [x] min | [ ] before:(date) - [x] max | [ ] before:(date) - [x] between | [ ] date_format - [x] integer | [ ] different - [x] url | [ ] in - [x] alpha_num | [ ] not_in - [x] alpha_dash| [ ] ip_address - [x] alpha | - [x] regex | - [x] confirmed | \ No newline at end of file +Implemented| Not implemented +-----------|----------------- + required | accepted + email | after:(date) + min | before:(date) + max | before:(date) + between | date_format + integer | different + url | in + alpha_num | not_in + alpha_dash| ip_address + alpha | + regex | + confirmed | \ No newline at end of file From 7485fe4c6f6c1437a5d4a4d3c2e5643f361e6478 Mon Sep 17 00:00:00 2001 From: Maxim Kerstens Date: Mon, 29 Jun 2015 09:29:21 +0200 Subject: [PATCH 05/16] Trait is obsolete --- .../UseParsleyValidationTrait.php | 27 ------------------- 1 file changed, 27 deletions(-) delete mode 100644 src/Idma/LaravelParsley/UseParsleyValidationTrait.php diff --git a/src/Idma/LaravelParsley/UseParsleyValidationTrait.php b/src/Idma/LaravelParsley/UseParsleyValidationTrait.php deleted file mode 100644 index 66b0132..0000000 --- a/src/Idma/LaravelParsley/UseParsleyValidationTrait.php +++ /dev/null @@ -1,27 +0,0 @@ -all()) { - call_user_func('parent::validate'); - } else { - if (!call_user_func([$this, 'passesAuthorization'])) { - call_user_func([$this, 'failedAuthorization']); - } - } - } -} From 3b3996c9b2cfdd62528d2069a73183f2d3a6197c Mon Sep 17 00:00:00 2001 From: Kerstens Maxim Date: Tue, 30 Jun 2015 12:05:46 +0200 Subject: [PATCH 06/16] Update README.md table formatting --- README.md | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index f850ba0..161523f 100644 --- a/README.md +++ b/README.md @@ -30,17 +30,17 @@ easy enough don't you think? ### Validation rules -Implemented| Not implemented ------------|----------------- - required | accepted - email | after:(date) - min | before:(date) - max | before:(date) - between | date_format - integer | different - url | in - alpha_num | not_in - alpha_dash| ip_address - alpha | - regex | - confirmed | \ No newline at end of file +Implemented| Not implemented | +-----------|-----------------| + required | accepted | + email | after:(date) | + min | before:(date) | + max | before:(date) | + between | date_format | + integer | different | + url | in | + alpha_num | not_in | + alpha_dash| ip_address | + alpha | | + regex | | + confirmed | | From d28f776fe68af99cd727c519194ffe7441a16c30 Mon Sep 17 00:00:00 2001 From: Maxim Kerstens Date: Wed, 1 Jul 2015 17:37:36 +0200 Subject: [PATCH 07/16] Corrected email validator --- src/Idma/LaravelParsley/ParsleyConverter.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Idma/LaravelParsley/ParsleyConverter.php b/src/Idma/LaravelParsley/ParsleyConverter.php index 67c940e..ffb04ce 100644 --- a/src/Idma/LaravelParsley/ParsleyConverter.php +++ b/src/Idma/LaravelParsley/ParsleyConverter.php @@ -63,7 +63,11 @@ public function convertRules($attribute, $rules) switch ($rule) { case 'required': + break; + case 'email': + $parsleyRule = 'type'; + $params = 'email'; break; case 'min': @@ -123,6 +127,7 @@ public function convertRules($attribute, $rules) default: $message = null; + break; } if ($message) { From 8d12fc57e10ffbc6f20bee1cdf945c6129c92106 Mon Sep 17 00:00:00 2001 From: Kerstens Maxim Date: Thu, 2 Jul 2015 09:17:24 +0200 Subject: [PATCH 08/16] Update composer.json --- composer.json | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 6f2a3fa..9df2108 100644 --- a/composer.json +++ b/composer.json @@ -1,11 +1,15 @@ { - "name": "idma/laravel-parsley", + "name": "happydemon/laravel-parsley", "description": "Converts FormRequest rules to Parsley rules", "keywords": ["laravel", "laravel 5", "parsley", "parsleyjs", "html", "form", "collective"], "authors": [ { "name": "idma", "email": "info@idma.ru" + }, + { + "name": "Maxim 'happyDemon' Kerstens", + "email": "maxim.kerstens@gmail.com" } ], "require": { @@ -16,7 +20,7 @@ }, "autoload": { "psr-0": { - "Idma\\LaravelParsley": "src/" + "happyDemon\\LaravelParsley": "src/" } }, "config": { From d54af599544595c1cc0ace930a4fe8b25d8df185 Mon Sep 17 00:00:00 2001 From: Maxim Kerstens Date: Thu, 2 Jul 2015 09:22:28 +0200 Subject: [PATCH 09/16] Switched namespace --- README.md | 2 +- composer.json | 8 ++++++-- src/Idma/LaravelParsley/FormBuilder.php | 2 +- src/Idma/LaravelParsley/LaravelParsleyServiceProvider.php | 2 +- src/Idma/LaravelParsley/ParsleyConverter.php | 2 +- 5 files changed, 10 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 161523f..d7fcd8b 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ If you have previously set up `LaravelCollective/Html` or `Illuminate/Html` you in `app/config` add the following under service providers: -`Idma\LaravelParsley\LaravelParsleyServiceProvider` +`HappyDemon\LaravelParsley\LaravelParsleyServiceProvider` If you haven't already, add these facades: diff --git a/composer.json b/composer.json index 6f2a3fa..3613d26 100644 --- a/composer.json +++ b/composer.json @@ -1,11 +1,15 @@ { - "name": "idma/laravel-parsley", + "name": "happydemon/laravel-parsley", "description": "Converts FormRequest rules to Parsley rules", "keywords": ["laravel", "laravel 5", "parsley", "parsleyjs", "html", "form", "collective"], "authors": [ { "name": "idma", "email": "info@idma.ru" + }, + { + "name": "Maxim 'happyDemon' Kerstens", + "email": "maxim.kerstens@gmail.com" } ], "require": { @@ -16,7 +20,7 @@ }, "autoload": { "psr-0": { - "Idma\\LaravelParsley": "src/" + "HappyDemon\\LaravelParsley": "src/" } }, "config": { diff --git a/src/Idma/LaravelParsley/FormBuilder.php b/src/Idma/LaravelParsley/FormBuilder.php index db09fb1..b7f0883 100644 --- a/src/Idma/LaravelParsley/FormBuilder.php +++ b/src/Idma/LaravelParsley/FormBuilder.php @@ -1,6 +1,6 @@ Date: Thu, 2 Jul 2015 09:27:02 +0200 Subject: [PATCH 10/16] Update composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 9df2108..d64805a 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,7 @@ }, "autoload": { "psr-0": { - "happyDemon\\LaravelParsley": "src/" + "HappyDemon\\LaravelParsley": "src/" } }, "config": { From 6c87ec68fe106c26a5427ebb955fe5f58fee86ad Mon Sep 17 00:00:00 2001 From: Maxim Kerstens Date: Thu, 2 Jul 2015 09:49:20 +0200 Subject: [PATCH 11/16] switched directory --- src/{Idma => HappyDemon}/LaravelParsley/FormBuilder.php | 0 .../LaravelParsley/LaravelParsleyServiceProvider.php | 0 src/{Idma => HappyDemon}/LaravelParsley/ParsleyConverter.php | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename src/{Idma => HappyDemon}/LaravelParsley/FormBuilder.php (100%) rename src/{Idma => HappyDemon}/LaravelParsley/LaravelParsleyServiceProvider.php (100%) rename src/{Idma => HappyDemon}/LaravelParsley/ParsleyConverter.php (100%) diff --git a/src/Idma/LaravelParsley/FormBuilder.php b/src/HappyDemon/LaravelParsley/FormBuilder.php similarity index 100% rename from src/Idma/LaravelParsley/FormBuilder.php rename to src/HappyDemon/LaravelParsley/FormBuilder.php diff --git a/src/Idma/LaravelParsley/LaravelParsleyServiceProvider.php b/src/HappyDemon/LaravelParsley/LaravelParsleyServiceProvider.php similarity index 100% rename from src/Idma/LaravelParsley/LaravelParsleyServiceProvider.php rename to src/HappyDemon/LaravelParsley/LaravelParsleyServiceProvider.php diff --git a/src/Idma/LaravelParsley/ParsleyConverter.php b/src/HappyDemon/LaravelParsley/ParsleyConverter.php similarity index 100% rename from src/Idma/LaravelParsley/ParsleyConverter.php rename to src/HappyDemon/LaravelParsley/ParsleyConverter.php From d4d6fb271e3779ee97ba8f13e9d64b746a103a78 Mon Sep 17 00:00:00 2001 From: Kerstens Maxim Date: Thu, 2 Jul 2015 10:41:38 +0200 Subject: [PATCH 12/16] Added replace key to composer --- composer.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/composer.json b/composer.json index d64805a..c85c7b9 100644 --- a/composer.json +++ b/composer.json @@ -12,6 +12,9 @@ "email": "maxim.kerstens@gmail.com" } ], + "replace": { + "idma/laravel-parsley":"0.*" + } "require": { "php": ">=5.4.0", "illuminate/support": "~5.0", From 6b8be8f522fc0f36b38feefb7c34cf41424d8abc Mon Sep 17 00:00:00 2001 From: Kerstens Maxim Date: Thu, 2 Jul 2015 10:44:15 +0200 Subject: [PATCH 13/16] Update composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index c85c7b9..207165e 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ ], "replace": { "idma/laravel-parsley":"0.*" - } + }, "require": { "php": ">=5.4.0", "illuminate/support": "~5.0", From a0e8f4c846647866759699d20948d10826c9d17f Mon Sep 17 00:00:00 2001 From: Maxim Kerstens Date: Thu, 2 Jul 2015 11:52:17 +0200 Subject: [PATCH 14/16] Don't force parsley property to be initialized --- src/HappyDemon/LaravelParsley/FormBuilder.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/HappyDemon/LaravelParsley/FormBuilder.php b/src/HappyDemon/LaravelParsley/FormBuilder.php index b7f0883..0bcea65 100644 --- a/src/HappyDemon/LaravelParsley/FormBuilder.php +++ b/src/HappyDemon/LaravelParsley/FormBuilder.php @@ -79,7 +79,8 @@ public function helpBlock($value, array $options = []) */ public function input($type, $name, $value = null, $options = []) { - $options = array_merge($options, $this->parsley->getFieldRules($name)); + if($this->parsley != null) + $options = array_merge($options, $this->parsley->getFieldRules($name)); return parent::input($type, $name, $value, $options); } @@ -89,14 +90,16 @@ public function input($type, $name, $value = null, $options = []) */ public function textarea($name, $value = null, $options = []) { - $options = array_merge($options, $this->parsley->getFieldRules($name)); + if($this->parsley != null) + $options = array_merge($options, $this->parsley->getFieldRules($name)); return parent::textarea($name, $value, $options); } public function select($name, $list = [], $selected = null, $options = []) { - $options = array_merge($options, $this->parsley->getFieldRules($name)); + if($this->parsley != null) + $options = array_merge($options, $this->parsley->getFieldRules($name)); return parent::select($name, $list, $selected, $options); } From 6056b338ca3377bdce1de4495016990c6b339759 Mon Sep 17 00:00:00 2001 From: Maxim Kerstens Date: Sun, 19 Jul 2015 23:18:42 +0200 Subject: [PATCH 15/16] Converted functionality to a trait Kept the FormBuilder intact in case you need it, but now it's possible to with other extensions of the FormBuilder --- src/HappyDemon/LaravelParsley/FormBuilder.php | 113 +----------------- src/HappyDemon/LaravelParsley/FormTrait.php | 59 +++++++++ 2 files changed, 60 insertions(+), 112 deletions(-) create mode 100644 src/HappyDemon/LaravelParsley/FormTrait.php diff --git a/src/HappyDemon/LaravelParsley/FormBuilder.php b/src/HappyDemon/LaravelParsley/FormBuilder.php index 0bcea65..3cadf83 100644 --- a/src/HappyDemon/LaravelParsley/FormBuilder.php +++ b/src/HappyDemon/LaravelParsley/FormBuilder.php @@ -6,116 +6,5 @@ class FormBuilder extends BaseFormBuilder { - protected $modelName = null; - - /** - * @type ParsleyConverter - */ - protected $parsley = null; - - /** - * {@inheritdoc} - */ - public function open(array $options = []) - { - $this->reserved[] = 'request'; - $this->parsley = new ParsleyConverter(array_get($options, 'request', null)); - - return parent::open($options); - } - - /** - * {@inheritdoc} - */ - public function model($model, array $options = []) - { - $this->setModel($model); - - return $this->open($options); - } - - public function openModel($model, array $options = []) - { - return $this->model($model, $options); - } - - /** - * {@inheritdoc} - */ - public function label($name, $value = null, $options = []) - { - $this->labels[] = $name; - - $options = $this->html->attributes($options); - - $value = e($this->formatLabel($name, $value)); - - $for = ($this->modelName && !starts_with($name, '_')) ? $this->modelName . '-' . $name : $name; - - return ''; - } - - /** - * Create a Bootstrap-like help block. - * - * @param string $value - * @param array $options - * - * @return string - */ - public function helpBlock($value, array $options = []) - { - if (isset($options['class'])) { - $options['class'] = 'help-block ' . $options['class']; - } else { - $options['class'] = 'help-block'; - } - - return 'html->attributes($options) . '>' . $value . ''; - } - - /** - * {@inheritdoc} - */ - public function input($type, $name, $value = null, $options = []) - { - if($this->parsley != null) - $options = array_merge($options, $this->parsley->getFieldRules($name)); - - return parent::input($type, $name, $value, $options); - } - - /** - * {@inheritdoc} - */ - public function textarea($name, $value = null, $options = []) - { - if($this->parsley != null) - $options = array_merge($options, $this->parsley->getFieldRules($name)); - - return parent::textarea($name, $value, $options); - } - - public function select($name, $list = [], $selected = null, $options = []) - { - if($this->parsley != null) - $options = array_merge($options, $this->parsley->getFieldRules($name)); - - return parent::select($name, $list, $selected, $options); - } - - /** - * Gets the short model name. - * - * @return string - */ - public function getModelName() - { - return $this->modelName; - } - - public function name() - { - return $this->getModelName(); - } + use FormTrait; } diff --git a/src/HappyDemon/LaravelParsley/FormTrait.php b/src/HappyDemon/LaravelParsley/FormTrait.php new file mode 100644 index 0000000..301e417 --- /dev/null +++ b/src/HappyDemon/LaravelParsley/FormTrait.php @@ -0,0 +1,59 @@ +reserved[] = 'request'; + $this->parsley = new ParsleyConverter(array_get($options, 'request', null)); + + return parent::open($options); + } + + /** + * {@inheritdoc} + */ + public function input($type, $name, $value = null, $options = []) + { + if ($this->parsley != null) + { + $options = array_merge($options, $this->parsley->getFieldRules($name)); + } + + return parent::input($type, $name, $value, $options); + } + + /** + * {@inheritdoc} + */ + public function textarea($name, $value = null, $options = []) + { + if ($this->parsley != null) + { + $options = array_merge($options, $this->parsley->getFieldRules($name)); + } + + return parent::textarea($name, $value, $options); + } + + public function select($name, $list = [], $selected = null, $options = []) + { + if ($this->parsley != null) + { + $options = array_merge($options, $this->parsley->getFieldRules($name)); + } + + return parent::select($name, $list, $selected, $options); + } +} \ No newline at end of file From 5e4cb9cb0d8fbbfc80303d0ff125a3cabc9a1277 Mon Sep 17 00:00:00 2001 From: Maxim Kerstens Date: Mon, 20 Jul 2015 11:41:48 +0200 Subject: [PATCH 16/16] Added the following validators: after, before, date_format, in, not_in, different They required custom validators, which is included in resources/assets/parsley-laravel.js and should be included after the parsley js file. --- resources/assets/parsley-laravel.js | 72 +++++++++++++++++++ .../LaravelParsley/ParsleyConverter.php | 45 +++++++++++- 2 files changed, 115 insertions(+), 2 deletions(-) create mode 100644 resources/assets/parsley-laravel.js diff --git a/resources/assets/parsley-laravel.js b/resources/assets/parsley-laravel.js new file mode 100644 index 0000000..360a125 --- /dev/null +++ b/resources/assets/parsley-laravel.js @@ -0,0 +1,72 @@ +window.ParsleyValidator + .addValidator('after', function (value, requirement) { + var params = requirement.split('|-|'); + if(params.length == 1) + { + // is valid date? + var timestamp = Date.parse(value), + minTs = Date.parse(params[0]); + + return isNaN(timestamp) ? false : timestamp > minTs; + } + else + { + // A format was given, use momentJS + var timestamp = moment(value, params[1]); + var minTs = moment(params[0], params[1]); + + // If it's a valid date + // and the difference in milliseconds is greater than 0 + return (timestamp.isValid()) ? timestamp.isAfter(minTs) : false; + } + }, 32) + .addMessage('en', 'after', 'This date should be after %s'); +window.ParsleyValidator + .addValidator('before', function (value, requirement) { + var params = requirement.split('|-|'); + if(params.length == 1) + { + // is valid date? + var timestamp = Date.parse(value), + maxTs = Date.parse(params[0]); + + return isNaN(timestamp) ? false : timestamp < maxTs; + } + else + { + // A format was given, use momentJS + var timestamp = moment(value, params[1]); + var maxTs = moment(params[0], params[1]); + + // If it's a valid date + // and the difference in milliseconds is smaller than 0 + return (timestamp.isValid()) ? timestamp.isBefore(maxTs) : false; + } + }, 32) + .addMessage('en', 'before', 'This date should be before %s'); +window.ParsleyValidator + .addValidator('formatDate', function (value, requirement) { + var d = moment(value,requirement); + + return (d == null || !d.isValid()); + }, 32) + .addMessage('en', 'formatDate', 'This date should be in this format: "%s"'); +window.ParsleyValidator + .addValidator('inList', function (value, requirement) { + var list = requirement.split(','); + + return list.indexOf(value) > -1; + }, 32) + .addMessage('en', 'inList', 'The value should be one of the following: "%s"'); +window.ParsleyValidator + .addValidator('notInList', function (value, requirement) { + var list = requirement.split(','); + + return list.indexOf(value) != -1; + }, 32) + .addMessage('en', 'notInList', 'The value should not be one of the following: "%s"'); +window.ParsleyValidator + .addValidator('different', function (value, requirement) { + return $(requirement).val() != value; + }, 32) + .addMessage('en', 'different', 'The value should not be the same as "%s"'); \ No newline at end of file diff --git a/src/HappyDemon/LaravelParsley/ParsleyConverter.php b/src/HappyDemon/LaravelParsley/ParsleyConverter.php index 2d28d61..03de3f8 100644 --- a/src/HappyDemon/LaravelParsley/ParsleyConverter.php +++ b/src/HappyDemon/LaravelParsley/ParsleyConverter.php @@ -51,10 +51,12 @@ public function convertRules($attribute, $rules) $attrs = []; $message = null; + $date_format = null; + foreach ($rules as $rule) { list($rule, $params) = explode(':', $rule . ':'); - $params = explode(',', str_replace(' ', '', $params)); + $params = explode(',', $params); $parsleyRule = $rule; @@ -62,6 +64,7 @@ public function convertRules($attribute, $rules) $message = $this->getMessage($attribute, $rule, $rules); switch ($rule) { + case 'accepted': case 'required': break; @@ -125,6 +128,45 @@ public function convertRules($attribute, $rules) $params = "#{$attribute}_confirmation"; break; + case 'different': + $parsleyRule = 'different'; + $message = str_replace(':other', $params[0], $message); + $params = '#'.$params[0]; + break; + + case 'date_format': + $parsleyRule = 'dateformat'; + $replace = [ + // Day + 'd' => 'DD', 'D' => 'ddd', 'j' => 'D', 'l' => 'DDDD', + 'N' => 'E', 'S' => '', 'w' => 'W', 'z' => 'DDD', + // Week + 'W' => 'w', + // Month + 'F' => 'MMMM', 'm' => 'MM', 'M' => 'MMM', 'n' => 'M', 't' => '', + // Year + 'L' => '', 'o' => 'YYYY', 'Y' => 'YYYY', 'y' => 'YY', + // Time + 'a' => 'a', 'A' => 'A', 'B' => '', 'g' => 'h', 'G' => 'H', + 'h' => 'hh', 'H' => 'HH', 'i' => 'i', 's' => 's', 'u' => '' + ]; + $params = str_replace(array_keys($replace), array_values($replace), $params[0]); + $date_format = $params; + $message = str_replace(':format', $params, $message); + break; + case 'before': + case 'after': + $params = $params[0]; + if($date_format !== null) + { + $params .= '|-|'.$date_format; + } + break; + case 'in': + case 'not_in': + $parsleyRule = camel_case($rule).'List'; + $params = implode(',', $params); + break; default: $message = null; break; @@ -141,7 +183,6 @@ public function convertRules($attribute, $rules) $message = null; } - return $attrs; }