Skip to content

Commit c8e66ec

Browse files
authored
Merge pull request #48 from nthachus/master
Improve to use HTML5 attributes for laravel 5
2 parents 290c68e + dcc9c6d commit c8e66ec

26 files changed

+1773
-393
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
composer.phar
33
composer.lock
44
.DS_Store
5+
Thumbs.db
6+
.idea/

README.md

Lines changed: 37 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -30,40 +30,40 @@ This package makes validation rules defined in laravel work client-side by conve
3030
### Installation
3131

3232
Require `bllim/laravalid` in composer.json and run `composer update`.
33-
33+
```json
3434
{
3535
"require": {
36-
"laravel/framework": "5.1.*", //or "5.0.*"
36+
"laravel/framework": "5.2.*", //or "5.0.*"
3737
...
3838
"bllim/laravalid": "*"
3939
}
4040
...
4141
}
42-
42+
```
4343
> **Note:** For **Laravel 4** use `laravel4` branch like as `"bllim/laravalid": "dev-laravel4"`
4444
4545
Composer will download the package. After the package is downloaded, open `config/app.php` and add the service provider and alias as below:
4646
```php
4747
'providers' => array(
4848
...
49-
'Bllim\Laravalid\LaravalidServiceProvider',
49+
Bllim\Laravalid\LaravalidServiceProvider::class,
5050
),
5151
```
5252
```php
5353
'aliases' => array(
5454
...
55-
'HTML' => 'Collective\Html\HtmlFacade::class', // if not exists add for html too
56-
'Form' => 'Bllim\Laravalid\Facade',
55+
'HTML' => Collective\Html\HtmlFacade::class, // if not exists add for html too
56+
'Form' => Bllim\Laravalid\Facade::class,
5757
),
5858
```
5959

6060
Also you need to publish configuration file and assets by running the following Artisan commands.
61-
```php
61+
```bash
6262
$ php artisan vendor:publish
6363
```
6464

6565
### Configuration
66-
After publishing configuration file, you can find it in config/laravalid folder. Configuration parameters are as below:
66+
After publishing configuration file, you can find it in `config` folder as `laravalid.php` file. Configuration parameters are as below:
6767

6868
| Parameter | Description | Values |
6969
|-----------|-------------|--------|
@@ -73,7 +73,7 @@ After publishing configuration file, you can find it in config/laravalid folder.
7373

7474
### Usage
7575

76-
The package uses laravel Form Builder to make validation rules work for both sides. Therefore you should use Form Builder. While opening form by using Form::open you can give $rules as second parameter:
76+
The package uses laravel Form Builder to make validation rules work for both sides. Therefore you should use Form Builder. While opening form by using `Form::open` you can give `$rules` as second parameter:
7777
```php
7878
$rules = ['name' => 'required|max:100', 'email' => 'required|email', 'birthdate' => 'date'];
7979
Form::open(array('url' => 'foo/bar', 'method' => 'put'), $rules);
@@ -82,7 +82,7 @@ The package uses laravel Form Builder to make validation rules work for both sid
8282
Form::text('birthdate');
8383
Form::close(); // don't forget to close form, it reset validation rules
8484
```
85-
Also if you don't want to struggle with $rules at view files, you can set it in Controller or route with or without form name by using Form::setValidation($rules, $formName). If you don't give form name, this sets rules for first Form::open
85+
Also if you don't want to struggle with `$rules` at view files, you can set it in `Controller` or route with or without form name by using `Form::setValidation($rules, $formName)`. If you don't give form name, this sets rules for first `Form::open`
8686
```php
8787
// in controller or route
8888
$rules = ['name' => 'required|max:100', 'email' => 'required|email', 'birthdate' => 'date'];
@@ -93,19 +93,19 @@ Also if you don't want to struggle with $rules at view files, you can set it in
9393
// some form inputs
9494
Form::close();
9595
```
96-
For rules which is related to input type in laravel (such as max, min), the package looks for other given rules to understand which type is input. If you give integer or numeric as rule with max, min rules, the package assume input is numeric and convert to data-rule-max instead of data-rule-maxlength.
96+
For rules which is related to input type in laravel (such as `max`, `min`), the package looks for other given rules to understand which type is input. If you give integer or numeric as rule with `max`, `min` rules, the package assume input is numeric and convert to `data-rule-max` instead of `data-rule-maxlength`.
9797
```php
9898
$rules = ['age' => 'numeric|max'];
9999
```
100-
The converter assume input is string by default. File type is not supported yet.
100+
The converter assume input is `string` by default. File type is also supported.
101101

102102
**Validation Messages**
103103

104-
Converter uses validation messages of laravel (app/lang/en/validation.php) by default for client-side too. If you want to use jquery validation messages, you can set useLaravelMessages, false in config file of package which you copied to your config dir.
104+
Converter uses validation messages of laravel (`resources/lang/en/validation.php`) by default for client-side too. If you want to use jquery validation messages, you can set `useLaravelMessages`, false in config file of package which you copied to your config dir.
105105

106106
#### Plugins
107107
**Jquery Validation**
108-
While using Jquery Validation as html/js validation plugin, you should include jquery.validate.laravalid.js in your views, too. After assets published, it will be copied to your public folder. The last thing you should do at client side is initializing jquery validation plugin as below:
108+
While using Jquery Validation as html/js validation plugin, you should include `jquery.validate.laravalid.js` in your views, too. After assets published, it will be copied to your public folder. The last thing you should do at client side is initializing jquery validation plugin as below:
109109
```html
110110
<script type="text/javascript">
111111
$('form').validate({onkeyup: false}); //while using remote validation, remember to set onkeyup false
@@ -118,23 +118,20 @@ Controller/Route side
118118
```php
119119
class UserController extends Controller {
120120

121-
public $createValidation = ['name' => 'required|max:255', 'username' => 'required|regex:/^[a-z\-]*$/|max:20', 'email' => 'required|email', 'age' => 'numeric'];
122-
public $createColumns = ['name', 'username', 'email', 'age'];
121+
static $createValidations = ['name' => 'required|max:255', 'username' => 'required|regex:/^[a-z\-]*$/|max:20', 'email' => 'required|email', 'age' => 'numeric'];
123122

124123
public function getCreate()
125124
{
126-
Form::setValidation($this->createValidation);
125+
Form::setValidation(static::$createValidations);
127126
return View::make('user.create');
128127
}
129128

130129
public function postCreate()
131130
{
132-
$inputs = Input::only($this->createColumns);
133-
$rules = $this->createValidation;
131+
$inputs = Input::only(array_keys(static::$createValidations));
132+
$validator = Validator::make($inputs, static::$createValidations);
134133

135-
$validator = Validator::make($inputs, $rules);
136-
137-
if($validator->fails())
134+
if ($validator->fails())
138135
{
139136
// actually withErrors is not really neccessary because we already show errors at client side for normal users
140137
return Redirect::back()->withErrors($validator);
@@ -163,9 +160,9 @@ View side
163160
{{ Form::number('age') }}
164161
{{ Form::close() }}
165162

166-
<script src="{{ asset('js/jquery-1.10.2.min.js') }}"></script>
167-
<script src="{{ asset('js/jquery.validate.min.js') }}"></script>
168-
<script src="{{ asset('js/jquery.validate.laravalid.js') }}"></script>
163+
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
164+
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.17.0/jquery.validate.min.js"></script>
165+
<script src="{{ asset('vendor/laravalid/jquery.validate.laravalid.js') }}"></script>
169166
<script type="text/javascript">
170167
$('form').validate({onkeyup: false});
171168
</script>
@@ -188,52 +185,52 @@ Form::converter()->route()->extend('someotherrule', function($name, $parameters)
188185
// some code
189186
return ['valid' => false, 'messages' => 'Seriously dude, what kind of input is this?'];
190187
});
191-
192188
```
189+
193190
Second, you can create your own converter (which extends baseconverter or any current plugin converter) in `Bllim\Laravalid\Converter\` namespace and change plugin configuration in config file with your own plugin name.
194191

195-
> **Note:** If you are creating a converter for some existed html/js plugin please create it in `converters` folder and send a pull-request.
192+
> **Note:** If you are creating a converter for some existed html/js plugin please create it in `Converter` folder and send a pull-request.
196193
197194
### Plugins and Supported Rules
198195
**Jquery Validation**
199-
To use Jquery Validation, change plugin to `JqueryValidation` in config file and import jquery, jquery-validation and **jquery.validation.laravel.js** in views.
196+
To use Jquery Validation, change plugin to `JqueryValidation` in config file and import `jquery`, `jquery-validation` and **jquery.validate.laravalid.js** in views.
200197

201198

202199
| Rules | Jquery Validation |
203200
| ---------------|:----------------:|
204201
| Accepted | - |
205-
| Active URL | - |
206-
| After (Date) | - |
202+
| Active URL | `+` |
203+
| After (Date) | `+` |
207204
| Alpha | `+` |
208205
| Alpha Dash | - |
209-
| Alpha Numeric | - |
206+
| Alpha Numeric | `+` |
210207
| Array | - |
211-
| Before (Date) | - |
208+
| Before (Date) | `+` |
212209
| Between | `+` |
213210
| Boolean | - |
214211
| Confirmed | - |
215212
| Date | `+` |
216213
| Date Format | - |
217-
| Different | - |
214+
| Different | `+` |
218215
| Digits | - |
219216
| Digits Between | - |
220217
| E-Mail | `+` |
221218
| Exists (Database) | `+` |
222-
| Image (File) | - |
219+
| Image (File) | `+` |
223220
| In | - |
224-
| Integer | - |
221+
| Integer | `+` |
225222
| IP Address | `+` |
226223
| Max | `+` |
227-
| MIME Types | - |
224+
| MIME Types | `+` |
228225
| Min | `+` |
229226
| Not In | - |
230227
| Numeric | `+` |
231228
| Regular Expression | `+` |
232229
| Required | `+` |
233230
| Required If | - |
234-
| Required With | - |
231+
| Required With | `+` |
235232
| Required With All | - |
236-
| Required Without | - |
233+
| Required Without | `+` |
237234
| Required Without All | - |
238235
| Same | `+` |
239236
| Size | - |
@@ -248,14 +245,13 @@ To use Jquery Validation, change plugin to `JqueryValidation` in config file and
248245
You can fork and contribute to development of the package. All pull requests is welcome.
249246

250247
**Convertion Logic**
251-
Package converts rules by using converters (in src/converters). It uses Converter class of chosen plugin which extends BaseConverter/Converter class.
248+
Package converts rules by using converters (in `src/Bllim/Laravalid/Converter`). It uses `Converter` class of chosen plugin which extends `Converter/Base/Converter` class.
252249
You can look at existed methods and plugins to understand how it works. Explanation will be ready, soon.
253250

254251
### Known issues
255252
- Some rules are not supported for now
256253

257254
### TODO
258-
- Test script
259255
- Support unsupported rules
260256
- Improve doc
261257
- Comment code
@@ -265,6 +261,7 @@ You can look at existed methods and plugins to understand how it works. Explanat
265261
- @phpspider
266262
- @jannispl
267263
- @rene-springmann
264+
- @nthachus
268265

269266
and [more](https://github.com/bllim/laravalid/graphs/contributors)
270267

composer.json

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,15 @@
1111
],
1212
"require": {
1313
"php": ">=5.3.0",
14-
"laravelcollective/html": "^5.4.8",
15-
"illuminate/support": ">=4.2",
16-
"illuminate/validation": ">=4.0",
17-
"illuminate/routing": ">=4.0"
14+
"laravelcollective/html": "^5.2",
15+
"illuminate/support": "~5.2",
16+
"illuminate/validation": "~5.0",
17+
"illuminate/routing": "~5.0",
18+
"illuminate/translation": "~5.0"
19+
},
20+
"require-dev": {
21+
"mockery/mockery": "~0.9",
22+
"phpunit/phpunit": "~4.0"
1823
},
1924
"autoload": {
2025
"psr-0": {

config/.gitkeep

Whitespace-only changes.

public/.gitkeep

Whitespace-only changes.
Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,54 @@
1-
$.validator.addMethod("regex", function(value, element, regexp) {
2-
var regex = new RegExp(regexp);
3-
return this.optional(element) || regex.test(value);
4-
}, 'Format is invalid');
1+
$.extend($.validator.methods, {
2+
maxlength: function (value, element, param) {
3+
if (this.optional(element))
4+
return true;
5+
6+
var length = (element.files && element.files.length) ? element.files[0].size / 1024
7+
: ($.isArray(value) ? value.length : this.getLength(value, element));
8+
return length <= param;
9+
}
10+
});
11+
12+
// Return true if the field value matches the given format RegExp
13+
$.validator.addMethod("pattern", function (value, element, param) {
14+
if (this.optional(element))
15+
return true;
16+
17+
if (typeof param === "string")
18+
param = new RegExp(param.charAt(0) == "^" ? param : "^(?:" + param + ")$");
19+
return param.test(value);
20+
}, "Invalid format.");
21+
22+
$.validator.addMethod("notEqualTo", function (value, element, param) {
23+
return this.optional(element) || !$.validator.methods.equalTo.call(this, value, element, param);
24+
}, "Please enter a different value, values must not be the same.");
25+
26+
$.validator.addMethod("integer", function (value, element) {
27+
return this.optional(element) || /^-?\d+$/.test(value);
28+
}, "A positive or negative non-decimal number please");
29+
30+
$.validator.addMethod("ipv4", function (value, element) {
31+
return this.optional(element)
32+
|| /^(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)$/i.test(value);
33+
}, "Please enter a valid IP v4 address.");
34+
35+
// Accept a value from a file input based on a required mime-type
36+
$.validator.addMethod("accept", function (value, element, param) {
37+
// Browser does not support element.files and the FileList feature
38+
if (this.optional(element) || $(element).attr("type") !== "file" || !element.files || !element.files.length)
39+
return true;
40+
41+
// Split mime on commas in case we have multiple types we can accept
42+
var typeParam = typeof param === "string" ? param.replace(/\s+/g, "") : "image/*",
43+
// Escape string to be used in the regex
44+
regex = new RegExp(".?(" + typeParam.replace(/[\-\[\]\/\{}\(\)\+\?\.\\\^\$\|]/g, "\\$&").replace(/,/g, "|").replace(/\/\*/g, "/.*") + ")$", "i");
45+
46+
// Grab the mime-type from the loaded file, verify it matches
47+
for (var i = 0; i < element.files.length; i++) {
48+
if (!regex.test(element.files[i].type))
49+
return false;
50+
}
51+
52+
// We've validated each file
53+
return true;
54+
}, "Please enter a value with a valid mime-type.");

src/Bllim/Laravalid/Converter/.gitkeep

Whitespace-only changes.

src/Bllim/Laravalid/Converter/Base/.gitkeep

Whitespace-only changes.

src/Bllim/Laravalid/Converter/Base/Container.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ abstract class Container
1616
{
1717
protected $customMethods = [];
1818

19-
public function convert($name, $parameters)
19+
public function convert($name, $parameters = [])
2020
{
2121
$methodName = strtolower($name);
2222

0 commit comments

Comments
 (0)