定制 raoul2000/yii2-twbsmaxlength-widget 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

raoul2000/yii2-twbsmaxlength-widget

Composer 安装命令:

composer require raoul2000/yii2-twbsmaxlength-widget

包简介

The TwbsMaxlength widget is a wrapper for the Bootstrap Maxlength plugin, a visual feedback indicator for the maxlength attribute.

README 文档

README

The TwbsMaxlength widget is a wrapper for the great Bootstrap Maxlength plugin, a visual feedback indicator for the maxlength attribute. Have a look to the demo page for more !

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist raoul2000/yii2-twbsmaxlength-widget "*"

or add

"raoul2000/yii2-twbsmaxlength-widget": "*"

to the require section of your composer.json file.

Basic Usage

Using TwbsMaxlength widget is easy. In the example below, we are attaching the "Bootstrap Maxlength" plugin to an input text control, with a maximum length of 20 characters set by the maxlength HTML5 attribute :

<input type="text" class="form-control" id="txtinput1" name="xyz" maxlength="20" />

<?php
	raoul2000\widget\twbsmaxlength\TwbsMaxlength::widget(['selector' => '#txtinput1']);
?>

The user will not be able to enter more than 20 characters in the text input control. After the 10th character (hard coded default) is entered by the user, a small alert will appear at the bottom of the control.

Remember to use the selector option only when you need to attach the "Bootstrap Maxlength" plugin to an existing HTML input tag (text or textarea).

ActiveForm

Most of the time, a text or textarea input control is produced by an ActiveForm widget. The TwbsMaxlength is of course also able to handle such use case.

<?php 
	use raoul2000\widget\twbsmaxlength\TwbsMaxlength;
	
	$form = ActiveForm::begin(); 

	echo $form->field($model, 'name')
		->textInput(['maxlength' => true])		
		->widget(TwbsMaxlength::className());

	ActiveForm::end();
?>

The code above only works since Yii2 v2.0.3 which includes a feature to automatically set the maxlength attribute of an ActiveField textInput based on the related string validation rule (Read more...)

To use it with a textarea, simply add the typeconfiguration parameter with value TwbsMaxlength::INPUT_TEXTAREA :

<?php 
	use raoul2000\widget\twbsmaxlength\TwbsMaxlength;
	
	$form = ActiveForm::begin(); 

	echo $form->field($model, 'name')
		->textInput(['maxlength' => true])		
		->widget(TwbsMaxlength::className(),['type' => TwbsMaxlength::INPUT_TEXTAREA]);

	ActiveForm::end();
?>

Legacy support

If you are using Yii2 version prior to 2.0.3 no problem : the TwbsMaxlength widget implements the same kind of feature than Yii2 2.0.3. If you don't set the maxlength HTML5 attribute yourself, the widget will search for this value among the validation rules defined for the model's attribute. If the rule string is found and if a maximum length is set, it will be used to inject maxlength into the HTML input element. Otherwise the "Bootstrap Maxlength* plugin will not be enabled.

Let's see that on an example with the famous ContactForm model. We use the StringValidator to define the maximum length of the 'subject' attribute (here it will be set to 10) :

class ContactForm extends Model
{
    public $subject;
    
    /**
     * @return array the validation rules.
     */
    public function rules()
    {
        return [
        	['subject','string', 'length'=> [4,10]],
        ];
    }
    // ....

Now, no need to set the maxlength HTML5 attribute because the widget will do it for us. We can simply write :

<?php 
	use raoul2000\widget\twbsmaxlength\TwbsMaxlength;
	
	$form = ActiveForm::begin(); 

	echo $form->field($model, 'subject')->widget(TwbsMaxlength::className());

	ActiveForm::end();
?>

And that's it ! Take a look to the input tag created and you'll see that the maxlength HTML5 attribute has been set to 10, just like defined in the validation rule of the subject attribute.

Plugin Options

The Bootstrap Maxlength accepts several options to custoomize its behavior. These options can be initialized in the clientOptions array, when configuring the widget.

<?php
  use raoul2000\widget\twbsmaxlength\TwbsMaxlength;
  
  $form = ActiveForm::begin(); 
  
  echo $form->field($model, 'body')
  	->textinput(['maxlength' => true])
  	->widget(
      TwbsMaxlength::className(), 
      [
          'clientOptions' => [
              'threshold'         => 10,
              'preText'           => 'You have ',
              'separator'         => ' of ',
              'postText'          => ' chars remaining.',
              'warningClass'      => "label label-success",
              'limitReachedClass' => "label label-danger"
      ]
  ]);
  
  ActiveForm::end();
?>

For more information on the plugin options, please refer to bootstrap-maxlength github page.

Widget Options

textarea

To produce a textarea element instead of the classical text input (produced by default), use the type option when configuring your widget.

<?php 
    use raoul2000\widget\twbsmaxlength\TwbsMaxlength;
    
	$form = ActiveForm::begin(); 

	echo $form->field($model, 'body')
		->textinput(['maxlength' => true])
		->widget(
			TwbsMaxlength::className(),
			[
	    		'type' => TwbsMaxlength::INPUT_TEXTAREA
	    	]
	    );

	ActiveForm::end();
?>

Setting the Threshold

The threshold is a numeric value representing the number of characters left before reaching the maxlength limitation. When the threshold is reached, an alert is displayed to the user to inform her/him that the max length is about to be reached. You can set the threshold value option like any other clientOption for the plugin.

<?php
  use raoul2000\widget\twbsmaxlength\TwbsMaxlength;
  
  $form = ActiveForm::begin(); 
  
  echo $form->field($model, 'body')
  	->textinput(['maxlength' => true])
  	->widget(
  		TwbsMaxlength::className(), 
      	[
        	'clientOptions' => [ 'threshold' => 10]
      	]
	);
  
  ActiveForm::end();
?>

TwbsMaxlength provide a way to dynamically calculate the threshold value depending on the max length value. This option name is thresholdPolicy. The TwbsMaxlength widget includes 3 built-in thresholds policies :

  • TwbsMaxlength::THRESHOLD_HALF
  • TwbsMaxlength::THRESHOLD_TWO_THIRD (default)
  • TwbsMaxlength::THRESHOLD_THREE_QUARTERS

In the example below, the alert will be displayed after 3/4 of the total number of character allowed have been entered by the user. So for instance if the "subject' validation rule set the maximum length to be 40, the alert will show up when 30 characters are entered.

<?php 
    use raoul2000\widget\twbsmaxlength\TwbsMaxlength;
    
	$form = ActiveForm::begin(); 

	echo $form->field($model, 'subject')
		->textinput(['maxlength' => true])
		->widget(
			TwbsMaxlength::className(),
			[
    			'thresholdPolicy' => TwbsMaxlength::THRESHOLD_THREE_QUARTERS,
    		]
    	);

	ActiveForm::end();
?>

Note that the thresholdPolicy is only available when working with ActiveForms.

License

yii2-twbsmaxlength-widget is released under the BSD 3-Clause License. See the bundled LICENSE.md for details.

raoul2000/yii2-twbsmaxlength-widget 适用场景与选型建议

raoul2000/yii2-twbsmaxlength-widget 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.31k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2015 年 02 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「bootstrap」 「validation」 「extension」 「widget」 「yii2」 「length」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 raoul2000/yii2-twbsmaxlength-widget 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 raoul2000/yii2-twbsmaxlength-widget 我们能提供哪些服务?
定制开发 / 二次开发

基于 raoul2000/yii2-twbsmaxlength-widget 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 8.31k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 28
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 2
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2015-02-12