freesoftwarefactory/smartform
Composer 安装命令:
composer create-project freesoftwarefactory/smartform
包简介
Build forms automatically. Advanced controls. Yii2
README 文档
README
-
Install this class to make it visible
cd /your/app/components; ln -s ../vendor/freesoftwarefactory/smartform/SmartformWidget.php . -
Setup a config file by installing it in your 'params' section,
// copy the template from ./template/smartform.php to: 'field-groups'=>require(__DIR__.'/my-smart-form-settings.php'),
Usage
[php]
use yii\widgets\ActiveForm;
$form = ActiveForm::begin();
echo \app\components\SmartformWidget::widget([
'config_entry'=>'field-groups',
'form_id'=>'form-1',
'active_form'=>$form,
'model'=>$model,
]);
ActiveForm::end();
Callback Examples
Sometimes a callback is required to get values from it.
Callback Example: Read select options from database.
[php]
<?php
function _somecallback($_call,$_model,$_field_name){
if('select-options' == $_call){
if('some_custom_field_name'==$_field_name){
$options = [''=>'--Choose--'];
foreach(\app\models\Options::all() as $item)
$options[$item->id] = $item->text;
return $options;
}
}
}
?>
...html...
<?=\app\components\SmartformWidget::widget([
...
'callback'=>'_somecallback',
...
]);?>
How to Upload a File
- in your config file define a field named "product_image" in this way:
"product_image"=>"upload_one_picture",
- in the widget definition, add a inline callback. this is used to tell the widget where to put the $_POST and $_FILES.
echo \app\components\SmartformWidget::widget([
'config_entry'=>'field-groups',
'form_id'=>'some-form-id',
'active_form'=>$form,
'model'=>$model,
'callback' => function($_call,$_model,$_fieldname){
if('get_file_upload_url'==$_call){
return \yii\helpers\Url::toRoute(['ajax-upload-product-image']);
}
if('instance_files' == $_call){
$list = [];
if('product_image'==$_fieldname) $list[] = [
'id'=>1, 'file_name'=>'', 'file_path'=>'',
'preview_url'=>'/media/landingpage/product/thumb/'.$_model->id,
'delete_url'=>'',
];
return $list;
}
}
]);
- the controller which will receive the $_POST and $_FILES should declare a action and some exceptions to CSRF:
// methods required in the controller:
public function beforeAction($action) {
if($action->id == 'ajax-upload-product-image')
$this->enableCsrfValidation = false;
return parent::beforeAction($action);
}
public function actionAjaxUploadProductImage(){
if(!Yii::$app->request->isAjax) die('invalid ajax request');
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$model_id=filter_input(INPUT_POST,"model_id",FILTER_SANITIZE_STRING);
// this will help you to get more information:
Yii::info("UPLOAD_INFO\n", print_r(["POST"=>$_POST,"FILES"=>$_FILES],true));
$model = $this->findModel($model_id);
$tmp_file = $_FILES['Landingpage']['tmp_name']['product_image'];
$binary_data = file_get_contents($tmp_file);
return [];
}
- only for your information, the LOG injected into the action will tell you something like the following, so use this information in order to get your file:
$_POST = [
'file_id' => '0'
'model_id' => '1'
'field_name' => 'product_image'
]
$_FILES = [
'Landingpage' => [
'name' => [
'product_image' => 'foto_1_verde.jpg'
]
'type' => [
'product_image' => 'image/jpeg'
]
'tmp_name' => [
'product_image' => '/tmp/phppQhABh'
]
'error' => [
'product_image' => 0
]
'size' => [
'product_image' => 2597
]
]
]
freesoftwarefactory/smartform 适用场景与选型建议
freesoftwarefactory/smartform 是一款 基于 JavaScript 开发的 Composer 扩展包,目前已累计 49 次下载、GitHub Stars 达 0, 最近一次更新时间为 2016 年 09 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 freesoftwarefactory/smartform 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 freesoftwarefactory/smartform 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 49
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-09-24