cdmckay/pajama 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

cdmckay/pajama

Composer 安装命令:

composer require cdmckay/pajama

包简介

A PHP model validator compatible with the jQuery Validation plugin that allows for shared validation rules (in JSON) between JavaScript and PHP.

README 文档

README

Pajama provides drop-in server-side validation for your existing jQuery Validation plugin forms. It can also be used for standalone server-side validation in a pinch.

Since the goal of Pajama was to be used with the jQuery Validation plugin, the API has been designed to be similar and thus familiar to developers who already know how to use the jQuery Validation plugin.

For more information on using the Pajama API, please refer to the documentation.

For documentation on the validation methods (like required, min, max, etc.) refer to the jQuery Validation plugin documentation.

Pajama is now Composer-compatible and available on Packagist. Simply add this line to your composer.json to use it:

{
    "require": {
        ...
        "cdmckay/pajama": "dev-master"
    }
}

Getting Started (jQuery Validation integration)

First, get jQuery and the jQuery Validation plugin.

Next, create a rules.json file:

{
    "rating":{
        "required":true,
        "range":[1, 100]
    }
}

Now, include jQuery and the jQuery Validation plugin on a page:

<html>
    <head>
        <title>Getting Started</title>
        <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript" src="jquery.validate.js"></script>
    </head>
    <body>
        <form method="post" action="form-handler.php">
            <label>Rating: <input type="text" name="rating" /></label>
        </form>
        <script type="text/javascript">
        $.getJSON("rules.json", function(rules) {
            $("form").validate({ rules: rules });
        });
        </script>
    </body>
</html>

Finally, create a PHP script to handle the form submission:

require 'ValidatorContext.php';
require 'Validator.php';

\Cdmckay\Pajama\Validator::validate(array(
    'model' => $_POST,
    'rules' => json_decode('rules.json', true),
    'validHandler' => function() {
        // Store rating in database.
    },
));

For a more detailed example, see the jquery-validation-integration example in the examples folder.

Getting Started (standalone)

Although not its primary design goal, Pajama can be used standalone. Simply include the vendor/autoload.php file (generated by Composer) in your PHP script and pass it a model and some rules, like so:

$rules = array(
    'first_name' => 'required',
    'last_name' => 'required',
    'password_1' => array(
        'required' => true,
        'minlength' => 5,
        'equalTo' => '#password_2',
    ),
    'password_2' => array(
        'required' => true,
        'minlength' => 5,
        'equalTo' => '#password_1',
    ),
);

// Validate with callbacks...
$validator = \Cdmckay\Pajama\Validator::validate(array(
    'model' => $_POST,
    'rules' => $rules,
    'validHandler' => function() {
        // Model validated.
    },
    'invalidHandler' => function() {
        // Model failed validation.
    },
));

// ...or methods.
if($validator->model()) {
    // Model validated.
} else {
    // Model failed validation.
}

See the standalone example in the examples folder.

Custom Validators

Like the jQuery Validation plugin, Pajama can be extended via custom validators. To create a custom validator, use the addMethod static method like so:

\Cdmckay\Pajama\Validator::addMethod('regex', function($context, $value, $param)) {
    return $context->optional($value) || preg_match('/' . $param . '/', $value);
});

\Cdmckay\Pajama\Validator::validate(array(
    'model' => $_POST,
    'rules' => array(
        'md5_hash' => array(
            'required' => true,
            'regex' => '^[A-Fa-f0-9]+$',
        ),
    ),
    'validHandler' => function() {
        // ...
    },
));

Remember that if you're using Pajama with the jQuery Validation plugin, you must also write a JavaScript version of the validation method:

$.validator.addMethod("regex", function(value, element, param) {
    return this.optional(element) || new RegExp(param).test(value);
}, "This field does not conform to a pattern.");

Limitations

Since Pajama has no access to the submitting form's DOM context, it only has limited support for CSS selectors.

For example, consider this mark-up:

<form>
    <div class="checkbox-group">
        <label><input type="checkbox" name="foo[0]" value="bar" />Bar</label>
        <label><input type="checkbox" name="foo[1]" value="baz" />Baz</label>
    </div>
</form>

The jQuery Validation plugin required validation method can support arbitrary jQuery selectors like this:

{
    "email": {
        "required":".checkbox-group :checkbox:first:checked"
        "email":true
    }
}

However, the best Pajama can do is this:

{
    "email": {
        "required":"[name=foo[0]]:checked"
        "email":true
    }
}

In general, Pajama can only recognize selectors with the form #foo or [name=foo] and the following pseudo-classes:

  • :checked
  • :unchecked
  • :filled
  • :blank

In the case of #foo, Pajama assumes that the name and the id of the element were the same, as in:

<input type="text" name="foo" id="foo" />

统计信息

  • 总下载量: 287
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 15
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 15
  • Watchers: 5
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2013-03-08

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固