織夢自定義表單驗證字段不能爲空,不是經過前臺頁面js驗證,絕對靈活實用。php
模板文件的修改:html
一、修改diy_field_add.htm,23行左右添加代碼sql
var necessary = theform.necessary[0].checked?theform.necessary[0].value:theform.necessary[1].value;ide
替換字符串,大概在38行ui
替換revalue = "<field:"+fieldname+" itemname=\""+itemname+"\" autofield=\"1\" type=\""+dtype+"\" isnull=\""+sisnull+"\" default=\""+vdefault+"\" ";spa
爲revalue = "<field:"+fieldname+" itemname=\""+itemname+"\" autofield=\"1\" type=\""+dtype+"\" isnull=\""+sisnull+"\" necessary=\""+necessary+"\" default=\""+vdefault+"\" ";3d
二、diy_field_add.htm,大概124行</tr>後面,添加代碼orm
<tr> htm
<td bgcolor="#FFFFFF">blog
<strong>是否爲必填項:</strong><br/>
<span class="STYLE2">防止惡意提交表單</span>
</td>
<td bgcolor="#FFFFFF">
<input name="necessary" type="radio" value="true" class='np' checked='1' />
是
<input name="necessary" type="radio" value="false" class='np' />
否
</td>
</tr>
三、修改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+"\" ";
四、diy_field_edit.htm,大概139行</tr>後面添加代碼
<tr>
<td bgcolor="#FFFFFF"><strong>是否爲必填項:</strong><br/>
<span class="STYLE2">防止惡意提交表單</span></td>
<td bgcolor="#FFFFFF">
<input name="necessary" type="radio" value="true"<?php if($ctag->GetAtt('necessary')=='true') echo " checked='1' "; ?> class='np' />
是
<input name="necessary" type="radio" value="false"<?php if($ctag->GetAtt('necessary')=='false'||$ctag->GetAtt('necessary')=='') echo " checked='1' "; ?> class='np' />
否 </td>
</tr>
五、表單提交處理:
修改plus/diy.php文件
找到$addvar = $addvalue = '';在上面添加代碼
//驗證必填字段
$row = $dsql->GetOne("SELECT * FROM `#@__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;
}
}
}
六、這樣基本就搞定了,設置參數請經過後臺字段編輯設置
查看效果:
轉載:http://jingyan.baidu.com/article/380abd0a44f4151d90192c2a.html