cuiliang
7 years ago
committed by
GitHub
5 changed files with 578 additions and 36 deletions
@ -0,0 +1,202 @@
|
||||
在客户端扩展 ActiveForm |
||||
===================== |
||||
|
||||
[[yii\widgets\ActiveForm]] 小部件附带一组用于客户端验证的 JavaScript 方法。 |
||||
它的实现非常灵活,可以让你以不同的方式扩展它。 |
||||
下面我们来看描述。 |
||||
|
||||
## ActiveForm 事件 |
||||
|
||||
ActiveForm 触发一系列专用事件。使用类似以下的代码,您可以订阅这些代码 |
||||
事件并处理它们: |
||||
|
||||
```javascript |
||||
$('#contact-form').on('beforeSubmit', function (e) { |
||||
if (!confirm("Everything is correct. Submit?")) { |
||||
return false; |
||||
} |
||||
return true; |
||||
}); |
||||
``` |
||||
|
||||
在下文中,我们将查看可用的事件。 |
||||
|
||||
### `beforeValidate` |
||||
|
||||
`beforeValidate` 是在验证整个表单之前触发的。 |
||||
|
||||
事件处理程序的签名应该是: |
||||
|
||||
```javascript |
||||
function (event, messages, deferreds) |
||||
``` |
||||
|
||||
其中 |
||||
|
||||
- `event`: 一个 Event 对象。 |
||||
- `messages`: 一个关联数组,其中键是属性ID, |
||||
值是相应属性的错误消息数组。 |
||||
- `deferreds`: 一个 Deferred 对象数组。你可以使用 `deferreds.add(callback)` |
||||
来添加一个新的延迟验证。 |
||||
|
||||
如果处理程序返回一个布尔型 `false`,它将在此事件后停止进一步的表单验证。 |
||||
结果 `afterValidate` 事件将不会被触发。 |
||||
|
||||
### `afterValidate` |
||||
|
||||
`afterValidate` 是在验证整个表单后触发的。 |
||||
|
||||
事件处理程序的签名应该是: |
||||
|
||||
```javascript |
||||
function (event, messages, errorAttributes) |
||||
``` |
||||
|
||||
其中 |
||||
|
||||
- `event`: 一个 Event 对象。 |
||||
- `messages`: 一个关联数组,其中键是属性ID, |
||||
值是相应属性的错误消息数组。 |
||||
- `errorAttributes`: 一个具有验证错误的属性数组。有关此参数的结构, |
||||
请参阅 `attributeDefaults`。 |
||||
|
||||
### `beforeValidateAttribute` |
||||
|
||||
`beforeValidateAttribute` 事件是在验证属性之前触发的。 |
||||
事件处理程序的签名应该是: |
||||
|
||||
```javascript |
||||
function (event, attribute, messages, deferreds) |
||||
``` |
||||
|
||||
其中 |
||||
|
||||
- `event`: 一个 Event 对象。 |
||||
- `attribute`: 要验证的属性。 请参阅这个参数的 `attributeDefaults` |
||||
结构。 |
||||
- `messages`: 可以为其添加指定属性的验证错误消息的数组。 |
||||
- `deferreds`: 一个 Deferred 对象数组。你可以使用 `deferreds.add(callback)` |
||||
来添加一个新的延迟验证。 |
||||
|
||||
如果处理程序返回布尔型 `false`,它将停止进一步验证指定的属性。 |
||||
结果,`afterValidateAttribute` 事件将不会被触发。 |
||||
|
||||
### `afterValidateAttribute` |
||||
|
||||
`afterValidateAttribute` 事件在验证整个表单和每个属性后触发的。 |
||||
|
||||
事件处理程序的签名应该是: |
||||
|
||||
```javascript |
||||
function (event, attribute, messages) |
||||
``` |
||||
|
||||
其中 |
||||
|
||||
- `event`: 一个 Event 对象。 |
||||
- `attribute`: 该属性会被验证。有关此参数的结构, |
||||
请参阅 `attributeDefaults`。 |
||||
- `messages`: 可以为其添加指定属性的其他验证错误消息的 |
||||
数组。 |
||||
|
||||
### `beforeSubmit` |
||||
|
||||
`beforeSubmit` 是在所有验证通过后且提交表单之前触发的。 |
||||
|
||||
事件处理程序的签名应该是: |
||||
|
||||
```javascript |
||||
function (event) |
||||
``` |
||||
|
||||
其中 `event` 是一个 Event 对象。 |
||||
|
||||
如果处理返回布尔型“false”,它将停止表单提交。 |
||||
|
||||
### `ajaxBeforeSend` |
||||
|
||||
`ajaxBeforeSend` 事件是在发送用于基于AJAX的验证的AJAX请求之前触发的。 |
||||
|
||||
事件处理程序的签名应该是: |
||||
|
||||
```javascript |
||||
function (event, jqXHR, settings) |
||||
``` |
||||
|
||||
其中 |
||||
|
||||
- `event`: 一个 Event 对象。 |
||||
- `jqXHR`: 一个 jqXHR 对象 |
||||
- `settings`: AJAX 请求的设置 |
||||
|
||||
### `ajaxComplete` |
||||
|
||||
`ajaxComplete` 事件是在完成基于 AJAX 验证的 AJAX 请求后触发的。 |
||||
|
||||
事件处理程序的签名应该是: |
||||
|
||||
```javascript |
||||
function (event, jqXHR, textStatus) |
||||
``` |
||||
|
||||
where |
||||
|
||||
- `event`: 一个 Event 对象。 |
||||
- `jqXHR`: 一个 jqXHR 对象 |
||||
- `textStatus`: 请求的状态 ("success", "notmodified", "error", "timeout", |
||||
"abort", or "parsererror")。 |
||||
|
||||
## 通过 AJAX 提交表单 |
||||
|
||||
虽然可以在客户端或通过 AJAX 请求进行验证,但作为默认的正常请求 |
||||
表单提交本身已完成。如果你想通过 AJAX 提交表单,你可以 |
||||
通过以下方式处理表单的 `beforeSubmit` 事件做到这一点: |
||||
|
||||
```javascript |
||||
var $form = $('#formId'); |
||||
$form.on('beforeSubmit', function() { |
||||
var data = $form.serialize(); |
||||
$.ajax({ |
||||
url: $form.attr('action'), |
||||
type: 'POST', |
||||
data: data, |
||||
success: function (data) { |
||||
// 执行成功 |
||||
}, |
||||
error: function(jqXHR, errMsg) { |
||||
alert(errMsg); |
||||
} |
||||
}); |
||||
return false; // 防止默认提交 |
||||
}); |
||||
``` |
||||
|
||||
要了解更多关于 jQuery `ajax()` 函数的信息,请参阅 [jQuery 文档](https://api.jquery.com/jQuery.ajax/)。 |
||||
|
||||
|
||||
## 动态添加字段 |
||||
|
||||
在现代 Web 应用程序中,您经常需要在向用户显示表单后更改表单。 |
||||
这可以例如在点击“加号”图标之后添加新的字段。 |
||||
要为这些字段启用客户端验证,他们必须注册 ActiveForm JavaScript 插件。 |
||||
|
||||
您必须自行添加一个字段,然后将其添加到验证列表中: |
||||
|
||||
```javascript |
||||
$('#contact-form').yiiActiveForm('add', { |
||||
id: 'address', |
||||
name: 'address', |
||||
container: '.field-address', |
||||
input: '#address', |
||||
error: '.help-block', |
||||
validate: function (attribute, value, messages, deferred, $form) { |
||||
yii.validation.required(value, messages, {message: "Validation Message Here"}); |
||||
} |
||||
}); |
||||
``` |
||||
|
||||
要从验证列表中删除一个字段,使它不被验证,您可以执行以下操作: |
||||
|
||||
```javascript |
||||
$('#contact-form').yiiActiveForm('remove', 'address'); |
||||
``` |
@ -0,0 +1,22 @@
|
||||
# 你需要了解什么 |
||||
|
||||
Yii 的学习曲线并不像其他 PHP 框架那样陡峭,但仍然需要一些基础知识。 |
||||
|
||||
## PHP |
||||
|
||||
Yii 是一个 PHP 框架,因此请确保您 [阅读并理解语言参考](http://php.net/manual/zh/langref.php)。 |
||||
|
||||
## 面向对象编程 |
||||
|
||||
对面向对象编程的基本理解是必需的。如果您不熟悉它,请选中其中一个 |
||||
可用的教程,如 [tuts+](https://code.tutsplus.com/tutorials/object-oriented-php-for-beginners--net-12762)。 |
||||
|
||||
请注意,您的应用程序越复杂,您需要学习的更高级的 OOP 概念才能成功 |
||||
掌握这种复杂度。 |
||||
|
||||
## 命令行和 Composer |
||||
|
||||
Yii 广泛使用了标准的 PHP 包管理器 [Composer](https://getcomposer.org/),因此请确保您阅读 |
||||
并理解其指南。如果您不熟悉命令行,现在该开始尝试了。 |
||||
一旦你学会了基础知识,你就永远不想在没有它的情况下工作。 |
||||
|
@ -0,0 +1,103 @@
|
||||
Yii 和 Docker |
||||
============= |
||||
|
||||
对于开发和部署 Yii 应用程序可以使用 Docker 容器运行。容器就像一个轻量级的独立虚拟机,它将其服务映射到主机的端口,即在端口 80 上的容器中的Web服务器在您的(本地)主机上的端口 8888 上可用。 |
||||
|
||||
容器可以解决许多问题,例如在开发人员计算机和服务器上具有相同的软件版本,在开发时快速部署或模拟多服务器体系结构。 |
||||
|
||||
您可以在 [docker.com](https://www.docker.com/what-docker) 上阅读有关Docker容器的更多信息。 |
||||
|
||||
## 要求 |
||||
|
||||
- `docker` |
||||
- `docker-compose` |
||||
|
||||
访问[下载页面](https://www.docker.com/community-edition)获取 Docker 工具。 |
||||
|
||||
## 安装 |
||||
|
||||
安装后,你应该可以运行 `docker ps` 并看到类似的输出: |
||||
|
||||
``` |
||||
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS |
||||
``` |
||||
|
||||
这意味着您的Docker守护进程已启动并正在运行。 |
||||
|
||||
另外运行 `docker-compose version`,你的输出应该是这样的 |
||||
|
||||
``` |
||||
docker-compose version 1.20.0, build unknown |
||||
docker-py version: 3.1.3 |
||||
CPython version: 3.6.4 |
||||
OpenSSL version: OpenSSL 1.1.0g 2 Nov 2017 |
||||
``` |
||||
|
||||
使用 Compose,您可以配置管理您的应用程序所需的所有服务,例如数据库和缓存。 |
||||
|
||||
## 资源 |
||||
|
||||
- 基于 PHP 的 Yii 镜像可以在这里找到 [yii2-docker](https://github.com/yiisoft/yii2-docker) |
||||
- Docker 支持的 [yii2-app-basic](https://github.com/yiisoft/yii2-app-basic#install-with-docker) |
||||
- Docker 支持的 [yii2-app-advanced](https://github.com/yiisoft/yii2-app-advanced/pull/347) 正在开发中 |
||||
|
||||
## 用法 |
||||
|
||||
Docker的基本命令是: |
||||
|
||||
docker-compose up -d |
||||
|
||||
在后台启动堆栈中的所有服务 |
||||
|
||||
docker-compose ps |
||||
|
||||
列出正在运行的服务 |
||||
|
||||
docker-compose logs -f |
||||
|
||||
持续查看所有服务的日志 |
||||
|
||||
docker-compose stop |
||||
|
||||
优雅地停止堆栈中的所有服务 |
||||
|
||||
docker-compose kill |
||||
|
||||
立即停止堆栈中的所有服务 |
||||
|
||||
docker-compose down -v |
||||
|
||||
停止并删除所有服务,**在不使用 host-volumes 时注意数据丢失** |
||||
|
||||
在容器中运行命令 |
||||
|
||||
docker-compose run --rm php composer install |
||||
|
||||
在新的容器中运行 composer 安装 |
||||
|
||||
docker-compose exec php bash |
||||
|
||||
在 *运行中的* `php` 服务中执行 bash |
||||
|
||||
|
||||
## 高级主题 |
||||
|
||||
### Yii 框架测试 |
||||
|
||||
你可以按照[这里](https://github.com/yiisoft/yii2/blob/master/tests/README.md#dockerized-testing)描述的方式为 Yii 本身运行 dockerized 框架测试。 |
||||
|
||||
### 数据库管理工具 |
||||
|
||||
以 MySQL(`mysql`)的形式运行MySQL时,可以将 phpMyAdmin 容器添加到您的堆栈中,如下所示: |
||||
|
||||
``` |
||||
phpmyadmin: |
||||
image: phpmyadmin/phpmyadmin |
||||
ports: |
||||
- '8888:80' |
||||
environment: |
||||
- PMA_ARBITRARY=1 |
||||
- PMA_HOST=mysql |
||||
depends_on: |
||||
- mysql |
||||
``` |
Loading…
Reference in new issue