diff --git a/.gitignore b/.gitignore index 5826402..9c10d38 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ composer.phar composer.lock .DS_Store +.idea diff --git a/composer.json b/composer.json index e4af043..d5ae414 100644 --- a/composer.json +++ b/composer.json @@ -4,6 +4,11 @@ "keywords" : ["laravel","laravel5","validation","laravel validation","client-side","client side","laravel validation converter","laravel validation for client-side", "jquery validation","ardent"], "license": "MIT", "authors": [ + { + "name": "Patrick de Git", + "email": "degitpatrick@gmail.com", + "role": "Making the package work with Laravel 5.2" + }, { "name": "Igor Santos", "email": "igorsantos07+ardent@gmail.com", @@ -16,10 +21,10 @@ } ], "require": { - "laravelcollective/html": "5.1.*", - "illuminate/support": "5.1.*", - "illuminate/validation": "5.1.*", - "illuminate/routing": "5.1.*", + "laravelcollective/html": "5.2.*", + "illuminate/support": "5.2.*", + "illuminate/validation": "5.2.*", + "illuminate/routing": "5.2.*", "laravelbook/ardent": "3.*" }, "autoload": { diff --git a/config/config.php b/config/config.php index 22fd887..d75a4a5 100644 --- a/config/config.php +++ b/config/config.php @@ -1,7 +1,8 @@ true, - 'plugin' => 'JqueryValidation', - 'route' => 'laravalid' + 'useLaravelMessages' => true, + 'plugin' => 'JqueryValidation', + 'route' => 'laravalid', + 'action' => 'SiteController@getValidation', ]; \ No newline at end of file diff --git a/public/jquery.validate.laravalid.js b/public/jquery.validate.laravalid.js index deb9605..27f88e3 100644 --- a/public/jquery.validate.laravalid.js +++ b/public/jquery.validate.laravalid.js @@ -1,4 +1,7 @@ $.validator.addMethod("regex", function(value, element, regexp) { - var regex = new RegExp(regexp); + var flags = regexp.replace(/.*\/([gimy]*)$/, '$1'), + pattern = regexp.replace(new RegExp('^/(.*?)/' + flags + '$'), '$1'), + regex = new RegExp(pattern, flags); + return this.optional(element) || regex.test(value); }, 'Format is invalid'); \ No newline at end of file diff --git a/src/Laravalid/Converter/JqueryValidation/Message.php b/src/Laravalid/Converter/JqueryValidation/Message.php index 929e499..7c41358 100644 --- a/src/Laravalid/Converter/JqueryValidation/Message.php +++ b/src/Laravalid/Converter/JqueryValidation/Message.php @@ -41,6 +41,12 @@ public function numeric($parsedRule, $attribute, $type) return ['data-msg-number' => $message]; } + public function confirmed($parsedRule, $attribute, $type) + { + $message = Helper::getValidationMessage($attribute, $parsedRule['name']); + return ['data-msg-equalto' => $message]; + } + public function max($parsedRule, $attribute, $type) { $message = Helper::getValidationMessage($attribute, $parsedRule['name'], ['max' => $parsedRule['parameters'][0]], $type); diff --git a/src/Laravalid/Converter/JqueryValidation/Rule.php b/src/Laravalid/Converter/JqueryValidation/Rule.php index 2e6b360..7d2c6e1 100644 --- a/src/Laravalid/Converter/JqueryValidation/Rule.php +++ b/src/Laravalid/Converter/JqueryValidation/Rule.php @@ -124,19 +124,32 @@ public function between($parsedRule, $attribute, $type) } } + public function confirmed($parsedRule, $attribute, $type) + { + return ['data-rule-equalto' => '#' . $attribute . '_confirmation']; + } + public function unique($parsedRule, $attribute, $type) { + $route = \Config::get('laravalid.route', 'laravalid'); + if (empty($route)) { + return []; + } + $param = implode(',', $parsedRule['parameters']); $encrpytedParam = Helper::encrypt($param); - $route = \Config::get('laravalid.route', 'laravalid'); return ['data-rule-remote' => '/' . $route . '/unique?params=' . $encrpytedParam]; } public function exists($parsedRule, $attribute, $type) { + $route = \Config::get('laravalid.route', 'laravalid'); + if (empty($route)) { + return []; + } + $param = implode(',', $parsedRule['parameters']); $encrpytedParam = Helper::encrypt($param); - $route = \Config::get('laravalid.route', 'laravalid'); return ['data-rule-remote' => '/' . $route . '/unique?params=' . $encrpytedParam]; } diff --git a/src/Laravalid/FormBuilder.php b/src/Laravalid/FormBuilder.php index 6e04eba..32322ae 100644 --- a/src/Laravalid/FormBuilder.php +++ b/src/Laravalid/FormBuilder.php @@ -20,6 +20,7 @@ * @license MIT * @see Collective\Html\FormBuilder */ +use Illuminate\Contracts\View\Factory; use LaravelArdent\Laravalid\Converter\Base\Converter; use Collective\Html\HtmlBuilder; use Illuminate\Routing\UrlGenerator; @@ -29,9 +30,9 @@ class FormBuilder extends \Collective\Html\FormBuilder protected $converter; - public function __construct(HtmlBuilder $html, UrlGenerator $url, $csrfToken, Converter $converter) + public function __construct(HtmlBuilder $html, UrlGenerator $url, Factory $view, $csrfToken, Converter $converter) { - parent::__construct($html, $url, $csrfToken); + parent::__construct($html, $url, $view, $csrfToken); $plugin = \Config::get('laravalid.plugin'); $this->converter = $converter; } @@ -97,12 +98,19 @@ public function open(array $options = [], $rules = null) * Create a new model based form builder. * @param \LaravelArdent\Ardent\Ardent $model An Ardent model instance. Validation rules will be taken from it * @param array $options + * @param array $rules Used instead of the model rules * @return string * @see Collective\Html\FormBuilder */ - public function model($model, array $options = []) + public function model($model, array $options = [], array $rules = null) { - $this->setValidation($model::$rules); + if (is_null($rules)) { + if (isset($model::$rules)) { + $this->setValidation($model::$rules); + } + } else { + $this->setValidation($rules); + } return parent::model($model, $options); } diff --git a/src/Laravalid/LaravalidServiceProvider.php b/src/Laravalid/LaravalidServiceProvider.php index 9f95848..f7c5f04 100644 --- a/src/Laravalid/LaravalidServiceProvider.php +++ b/src/Laravalid/LaravalidServiceProvider.php @@ -27,12 +27,14 @@ public function boot() ], 'public'); // remote validations - $routeName = \Config::get('laravalid.route'); - $routeAction = \Config::get('laravalid.action', function($rule) { - $this->app['laravalid']->remoteValidation($rule); - }); + $routeName = \Config::get('laravalid.route'); + if (!empty($routeName)) { + $routeAction = \Config::get('laravalid.action', function ($rule) { + $this->app['laravalid']->remoteValidation($rule); + }); - \Route::any($routeName.'/{rule}', $routeAction); + \Route::any($routeName . '/{rule}', $routeAction); + } } /** @@ -46,18 +48,18 @@ public function register() if(!isset($this->app['html'])) { - $this->app->bindShared('html', function($app) + $this->app->singleton('html', function($app) { return new \Collective\Html\HtmlBuilder($app['url']); }); } - $this->app->bindShared('laravalid', function ($app) { + $this->app->singleton('laravalid', function ($app) { $plugin = \Config::get('laravalid.plugin'); $converterClassName = 'LaravelArdent\Laravalid\Converter\\'.$plugin.'\Converter'; $converter = new $converterClassName(); - $form = new FormBuilder($app->make('html'), $app->make('url'), $app->make('session.store')->getToken(), $converter); + $form = new FormBuilder($app->make('html'), $app->make('url'), $app->make('view'), $app->make('session.store')->getToken(), $converter); return $form->setSessionStore($app->make('session.store')); } );