织梦的自定义表单功能可以让你轻松的创建自己的表单功能,收集用户的留言、订单等信息。
但是dedecms的自定义表单有一个缺点,没有验证功能,允许提交空表单,这样致使后台会显示很多空数据,不能收集到完整的用户信息。今天我们就要给dedecms的自定义表单添加一个必填内容验证功能:
1、修改diy_field_add.htm,24行左右添加代码
var necessary = theform.necessary[0].checked?theform.necessary[0].value:theform.necessary[1].value;
替换字符串,大概在38行
替换
revalue = "为
revalue = "2、diy_field_add.htm,大概111行后面,添加代码
3、修改diy_field_edit.htm,18行左右添加代码
var necessary = theform.necessary[0].checked?theform.necessary[0].value:theform.necessary[1].value;
替换字符串,大概在38行
替换
revalue += " isnull=\""+sisnull+"\" default=\""+vdefault+"\" ";
为
revalue += " isnull=\""+sisnull+"\" necessary=\""+necessary+"\" default=\""+vdefault+"\" ";
4、diy_field_edit.htm,大概139行后面添加代码
5、表单提交处理:
修改plus/diy.php文件
找到$addvar = $addvalue = '';在上面添加代码
//验证必填字段
$row = $dsql->GetOne("SELECT * FROM `dede_diyforms` WHERE `table`='{$diy->table}'");
$fieldset = $row['info'];
require_once(DEDEINC."/dedetag.class.php");
$dtp = new DedeTagParse();
$dtp->SetNameSpace("field","");
$dtp->LoadSource($fieldset);
if(is_array($dtp->CTags))
{
foreach($dtp->CTags as $tagid=>$ctag)
{
if($ctag->GetAttribute("necessary")=='true' && empty(${$ctag->GetName()}))
{
showmsg($ctag->GetAttribute("itemname")."不能为空!", -1);
exit;
}
}
}
6、这样基本就搞定了,设置参数请通过后台字段编辑设置
以上就是修改织梦自定义表单字段为必填的方法,希望对大家的dedecms建站有所帮助。