Skip to content

Commit

Permalink
Merge pull request #48 from nthachus/master
Browse files Browse the repository at this point in the history
Improve to use HTML5 attributes for laravel 5
  • Loading branch information
bgultekin committed Jan 9, 2018
2 parents 290c68e + dcc9c6d commit c8e66ec
Show file tree
Hide file tree
Showing 26 changed files with 1,773 additions and 393 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
composer.phar
composer.lock
.DS_Store
Thumbs.db
.idea/
77 changes: 37 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,40 +30,40 @@ This package makes validation rules defined in laravel work client-side by conve
### Installation

Require `bllim/laravalid` in composer.json and run `composer update`.

```json
{
"require": {
"laravel/framework": "5.1.*", //or "5.0.*"
"laravel/framework": "5.2.*", //or "5.0.*"
...
"bllim/laravalid": "*"
}
...
}

```
> **Note:** For **Laravel 4** use `laravel4` branch like as `"bllim/laravalid": "dev-laravel4"`
Composer will download the package. After the package is downloaded, open `config/app.php` and add the service provider and alias as below:
```php
'providers' => array(
...
'Bllim\Laravalid\LaravalidServiceProvider',
Bllim\Laravalid\LaravalidServiceProvider::class,
),
```
```php
'aliases' => array(
...
'HTML' => 'Collective\Html\HtmlFacade::class', // if not exists add for html too
'Form' => 'Bllim\Laravalid\Facade',
'HTML' => Collective\Html\HtmlFacade::class, // if not exists add for html too
'Form' => Bllim\Laravalid\Facade::class,
),
```

Also you need to publish configuration file and assets by running the following Artisan commands.
```php
```bash
$ php artisan vendor:publish
```

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

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

### Usage

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:
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:
```php
$rules = ['name' => 'required|max:100', 'email' => 'required|email', 'birthdate' => 'date'];
Form::open(array('url' => 'foo/bar', 'method' => 'put'), $rules);
Expand All @@ -82,7 +82,7 @@ The package uses laravel Form Builder to make validation rules work for both sid
Form::text('birthdate');
Form::close(); // don't forget to close form, it reset validation rules
```
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
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`
```php
// in controller or route
$rules = ['name' => 'required|max:100', 'email' => 'required|email', 'birthdate' => 'date'];
Expand All @@ -93,19 +93,19 @@ Also if you don't want to struggle with $rules at view files, you can set it in
// some form inputs
Form::close();
```
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.
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`.
```php
$rules = ['age' => 'numeric|max'];
```
The converter assume input is string by default. File type is not supported yet.
The converter assume input is `string` by default. File type is also supported.

**Validation Messages**

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.
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.

#### Plugins
**Jquery Validation**
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:
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:
```html
<script type="text/javascript">
$('form').validate({onkeyup: false}); //while using remote validation, remember to set onkeyup false
Expand All @@ -118,23 +118,20 @@ Controller/Route side
```php
class UserController extends Controller {

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

public function getCreate()
{
Form::setValidation($this->createValidation);
Form::setValidation(static::$createValidations);
return View::make('user.create');
}

public function postCreate()
{
$inputs = Input::only($this->createColumns);
$rules = $this->createValidation;
$inputs = Input::only(array_keys(static::$createValidations));
$validator = Validator::make($inputs, static::$createValidations);

$validator = Validator::make($inputs, $rules);

if($validator->fails())
if ($validator->fails())
{
// actually withErrors is not really neccessary because we already show errors at client side for normal users
return Redirect::back()->withErrors($validator);
Expand Down Expand Up @@ -163,9 +160,9 @@ View side
{{ Form::number('age') }}
{{ Form::close() }}

<script src="{{ asset('js/jquery-1.10.2.min.js') }}"></script>
<script src="{{ asset('js/jquery.validate.min.js') }}"></script>
<script src="{{ asset('js/jquery.validate.laravalid.js') }}"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.17.0/jquery.validate.min.js"></script>
<script src="{{ asset('vendor/laravalid/jquery.validate.laravalid.js') }}"></script>
<script type="text/javascript">
$('form').validate({onkeyup: false});
</script>
Expand All @@ -188,52 +185,52 @@ Form::converter()->route()->extend('someotherrule', function($name, $parameters)
// some code
return ['valid' => false, 'messages' => 'Seriously dude, what kind of input is this?'];
});

```

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.

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


| Rules | Jquery Validation |
| ---------------|:----------------:|
| Accepted | - |
| Active URL | - |
| After (Date) | - |
| Active URL | `+` |
| After (Date) | `+` |
| Alpha | `+` |
| Alpha Dash | - |
| Alpha Numeric | - |
| Alpha Numeric | `+` |
| Array | - |
| Before (Date) | - |
| Before (Date) | `+` |
| Between | `+` |
| Boolean | - |
| Confirmed | - |
| Date | `+` |
| Date Format | - |
| Different | - |
| Different | `+` |
| Digits | - |
| Digits Between | - |
| E-Mail | `+` |
| Exists (Database) | `+` |
| Image (File) | - |
| Image (File) | `+` |
| In | - |
| Integer | - |
| Integer | `+` |
| IP Address | `+` |
| Max | `+` |
| MIME Types | - |
| MIME Types | `+` |
| Min | `+` |
| Not In | - |
| Numeric | `+` |
| Regular Expression | `+` |
| Required | `+` |
| Required If | - |
| Required With | - |
| Required With | `+` |
| Required With All | - |
| Required Without | - |
| Required Without | `+` |
| Required Without All | - |
| Same | `+` |
| Size | - |
Expand All @@ -248,14 +245,13 @@ To use Jquery Validation, change plugin to `JqueryValidation` in config file and
You can fork and contribute to development of the package. All pull requests is welcome.

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

### Known issues
- Some rules are not supported for now

### TODO
- Test script
- Support unsupported rules
- Improve doc
- Comment code
Expand All @@ -265,6 +261,7 @@ You can look at existed methods and plugins to understand how it works. Explanat
- @phpspider
- @jannispl
- @rene-springmann
- @nthachus

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

Expand Down
13 changes: 9 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@
],
"require": {
"php": ">=5.3.0",
"laravelcollective/html": "^5.4.8",
"illuminate/support": ">=4.2",
"illuminate/validation": ">=4.0",
"illuminate/routing": ">=4.0"
"laravelcollective/html": "^5.2",
"illuminate/support": "~5.2",
"illuminate/validation": "~5.0",
"illuminate/routing": "~5.0",
"illuminate/translation": "~5.0"
},
"require-dev": {
"mockery/mockery": "~0.9",
"phpunit/phpunit": "~4.0"
},
"autoload": {
"psr-0": {
Expand Down
Empty file removed config/.gitkeep
Empty file.
Empty file removed public/.gitkeep
Empty file.
58 changes: 54 additions & 4 deletions public/jquery.validate.laravalid.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,54 @@
$.validator.addMethod("regex", function(value, element, regexp) {
var regex = new RegExp(regexp);
return this.optional(element) || regex.test(value);
}, 'Format is invalid');
$.extend($.validator.methods, {
maxlength: function (value, element, param) {
if (this.optional(element))
return true;

var length = (element.files && element.files.length) ? element.files[0].size / 1024
: ($.isArray(value) ? value.length : this.getLength(value, element));
return length <= param;
}
});

// Return true if the field value matches the given format RegExp
$.validator.addMethod("pattern", function (value, element, param) {
if (this.optional(element))
return true;

if (typeof param === "string")
param = new RegExp(param.charAt(0) == "^" ? param : "^(?:" + param + ")$");
return param.test(value);
}, "Invalid format.");

$.validator.addMethod("notEqualTo", function (value, element, param) {
return this.optional(element) || !$.validator.methods.equalTo.call(this, value, element, param);
}, "Please enter a different value, values must not be the same.");

$.validator.addMethod("integer", function (value, element) {
return this.optional(element) || /^-?\d+$/.test(value);
}, "A positive or negative non-decimal number please");

$.validator.addMethod("ipv4", function (value, element) {
return this.optional(element)
|| /^(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);
}, "Please enter a valid IP v4 address.");

// Accept a value from a file input based on a required mime-type
$.validator.addMethod("accept", function (value, element, param) {
// Browser does not support element.files and the FileList feature
if (this.optional(element) || $(element).attr("type") !== "file" || !element.files || !element.files.length)
return true;

// Split mime on commas in case we have multiple types we can accept
var typeParam = typeof param === "string" ? param.replace(/\s+/g, "") : "image/*",
// Escape string to be used in the regex
regex = new RegExp(".?(" + typeParam.replace(/[\-\[\]\/\{}\(\)\+\?\.\\\^\$\|]/g, "\\$&").replace(/,/g, "|").replace(/\/\*/g, "/.*") + ")$", "i");

// Grab the mime-type from the loaded file, verify it matches
for (var i = 0; i < element.files.length; i++) {
if (!regex.test(element.files[i].type))
return false;
}

// We've validated each file
return true;
}, "Please enter a value with a valid mime-type.");
Empty file.
Empty file.
2 changes: 1 addition & 1 deletion src/Bllim/Laravalid/Converter/Base/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ abstract class Container
{
protected $customMethods = [];

public function convert($name, $parameters)
public function convert($name, $parameters = [])
{
$methodName = strtolower($name);

Expand Down
Loading

0 comments on commit c8e66ec

Please sign in to comment.