电话
13363039260
在文本字段后方加一个字数控制。
开发思路:
重写Text字段类别
实现步骤:
1、新建文件
dayrui/My/Field/Text.php
2、代码修改
<?php namespace My\Field; // 这里要改成My前缀
class Text extends \Phpcmf\Field\Text {
/**
* 字段相关属性参数
*
* @param array $value 值
* @return string
*/
public function option($option) {
// 这里增加字数限制设置选项
$style = '
<div class="form-group">
<label class="col-md-2 control-label">'.dr_lang('输入字数限制').'</label>
<div class="col-md-10">
<label><input type="text" class="form-control" size="10" name="data[setting][option][zishu]" value="'.$option['zishu'].'"></label>
<span class="help-block">'.dr_lang('要求最大输入的字数,超过会禁止提交').'</span>
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label">'.dr_lang('控件宽度').'</label>
<div class="col-md-10">
<label><input type="text" class="form-control" size="10" name="data[setting][option][width]" value="'.$option['width'].'"></label>
<span class="help-block">'.dr_lang('[整数]表示固定宽度;[整数%]表示百分比').'</span>
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label">'.dr_lang('密码框模式').'</label>
<div class="col-md-10">
<input type="checkbox" name="data[setting][option][ispwd]" '.($option['ispwd'] ? 'checked' : '').' value="1" data-on-text="'.dr_lang('开启').'" data-off-text="'.dr_lang('关闭').'" data-on-color="success" data-off-color="danger" class="make-switch" data-size="small">
<span class="help-block">'.dr_lang('开启之后它将作为密码框来显示').'</span>
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label">'.dr_lang('验证重复').'</label>
<div class="col-md-10">
<input type="checkbox" name="data[setting][option][unique]" '.($option['unique'] ? 'checked' : '').' value="1" data-on-text="'.dr_lang('开启').'" data-off-text="'.dr_lang('关闭').'" data-on-color="success" data-off-color="danger" class="make-switch" data-size="small">
<span class="help-block">'.dr_lang('开启将会判断此字段的唯一性(本字段只对内容模块主表有效)').'</span>
</div>
</div>
';
$option = $this->field_type($option['fieldtype'], $option['fieldlength']).'
<div class="form-group">
<label class="col-md-2 control-label">'.dr_lang('默认填充值').'</label>
<div class="col-md-10">
<label><input id="field_default_value" type="text" class="form-control" size="20" value="'.$option['value'].'" name="data[setting][option][value]"></label>
<label>'.$this->member_field_select().'</label>
<span class="help-block">'.dr_lang('也可以设置会员表字段,表示用当前登录会员信息来填充这个值').'</span>
</div>
</div>
';
return [$option, $style];
}
/**
* 字段入库值
*
* @param array $field 字段信息
* @return void
*/
public function insert_value($field) {
if ($field['setting']['option']['zishu']) {
// 验证字数
$value = \Phpcmf\Service::L('Field')->post[$field['fieldname']];
if (mb_strlen($value) > $field['setting']['option']['zishu']) {
\Phpcmf\Service::C()->_json(0, '字数超限了', ['field' => $field['fieldname']]);
}
}
parent::insert_value($field);
}
/**
* 字段表单输入
*
* @param string $field 字段数组
* @param array $value 值
* @return string
*/
public function input($field, $value = null) {
// 字段禁止修改时就返回显示字符串
if ($this->_not_edit($field, $value)) {
return $this->show($field, $value);
}
// 字段存储名称
$name = $field['fieldname'];
// 字段显示名称
$text = ($field['setting']['validate']['required'] ? '<span class="required" aria-required="true"> * </span>' : '').$field['name'];
// 表单宽度设置
$width = \Phpcmf\Service::_is_mobile() ? '100%' : ($field['setting']['option']['width'] ? $field['setting']['option']['width'] : 200);
// 风格
$style = 'style="width:'.$width.(is_numeric($width) ? 'px' : '').';"';
// 表单附加参数
$attr = $field['setting']['validate']['formattr'];
// 字段提示信息
$tips = ($name == 'title' && APP_DIR) || $field['setting']['validate']['tips'] ? '<span class="help-block" id="dr_'.$field['fieldname'].'_tips">'.$field['setting']['validate']['tips'].'</span>' : '';
// 当字段必填时,加入html5验证标签
$required = $field['setting']['validate']['required'] ? ' required="required"' : '';
// 是否密码框
$type = $field['setting']['option']['ispwd'] ? 'password' : 'text';
// 字段默认值
$value = strlen($value) ? $value : $this->get_default_value($field['setting']['option']['value']);
$str = '<input class="form-control '.$field['setting']['option']['css'].'" type="'.$type.'" name="data['.$field['fieldname'].']" id="dr_'.$field['fieldname'].'" value="'.$value.'" '.$style.' '.$required.' '.$attr.' />';
if ($field['setting']['option']['zishu']) {
// 验证字数
$str = '<label>'.$str.'</label><label style="margin-left:10px"><span id=\'dr_'.$field['fieldname'].'_msg\'>'.mb_strlen($value).'</span>/'.$field['setting']['option']['zishu'].'</label>';
$str.= '<script>
function getLength(fData)
{
var intLength=0
for (var i=0;i<fData.length;i++)
{
intLength=intLength+1
}
return intLength
}
$(\'#dr_'.$field['fieldname'].'\').keyup(function () {
var hidTitleWordNum = parseInt('.$field['setting']['option']['zishu'].');
var titleAreaVal = $(\'#dr_'.$field['fieldname'].'\').val();
var sum = 0;
for (var i = 0; i < titleAreaVal.length; i++) {
sum = sum + 1;
if (sum > hidTitleWordNum) {
dr_tips(0, "输入数据超长!不能再输入数据。");
var str = titleAreaVal.substring(0, i);
$(\'#dr_'.$field['fieldname'].'\').val(str);
$(\'#dr_'.$field['fieldname'].'_msg\').html(hidTitleWordNum);
break;
} else {
$(\'#dr_'.$field['fieldname'].'_msg\').html(sum);
}
}
});
</script>';
}
return $this->input_format($field['fieldname'], $text, $str.$tips);
}
}
3、自定义字段,设置长度