Silverstripe - 带有自定义模板的表单显示错误的验证错误

时间:2011-05-27 20:37:32

标签: validation forms silverstripe

我有以下自定义表单

$list = DataObject::get('AreaOfExpertise');
foreach ($list as $l) {
$source[$l->ID] = $l->Name;
}
$fields = new FieldSet(
new TextField('Customer'),
new TextField('ProjectName'),
new DateField('WinDate'),
new TextareaField('Details'),
new OptionsetField('AreaOfExpertiseID', 'Area Of Expertise', $source),
new TextField('ProjectValueLength'),
new TextField('DeliverTimeframe'),
new TextField('ProjectTeam'),
new TextField('Contact')
);
$actions = new FieldSet(
new FormAction('doSubmit', 'Submit')
);
$requiredFields = array(
'Customer',
'ProjectName',
'WinDate',
'Details',
'AreaOfExpertiseID',
'ProjectValueLength',
'DeliverTimeframe',
'ProjectTeam',
'Contact',
);
$validator = new RequiredFields($requiredFields);
$form = new Form($this, 'SubmissionForm', $fields, $actions, $validator);
$form->setTemplate('Forms/NewWinForm');
return $form;

我的模板是

<form $FormAttributes>
<% if Message %>
<p id="{$FormName}_error" class="message $MessageType">$Message</p>
<% else %>
<p id="{$FormName}_error" class="message $MessageType" style="display: none"></p>
<% end_if %>

<fieldset id="formContainer" class="clearfix NewWinForm">

<div id="Customer" class="row field text">
<label class="left" for="{$FormName}_Customer">Customer</label>
<div class="middleColumn">$dataFieldByName(Customer)</div>
</div>
<div id="ProjectName" class="row field text">
<label class="left" for="{$FormName}_ProjectName">Project name</label>
<div class="middleColumn">$dataFieldByName(ProjectName)</div>
</div>
<div id="WinDate" class="row field text">
<label class="left" for="{$FormName}_WinDate">Win date</label>
<div class="middleColumn">$dataFieldByName(WinDate)</div>
</div>
<div id="AreaOfExpertiseID" class="row field text">
<label class="left" for="{$FormName}_AreaOfExpertiseID">Area of expertise</label>
<div class="middleColumn">$dataFieldByName(AreaOfExpertiseID)</div>
</div>

<div id="Details" class="row field textarea">
<label class="left" for="{$FormName}_Details">Project details</label>
<div class="middleColumn">$dataFieldByName(Details)</div>
</div>
<div id="ProjectValueLength" class="row field text">
<label class="left" for="{$FormName}_ProjectValueLength">Project value/length</label>
<div class="middleColumn">$dataFieldByName(ProjectValueLength)</div>
</div>
<div id="DeliverTimeframe" class="row field text">
<label class="left" for="{$FormName}_DeliverTimeframe">Delivery timeframe</label>
<div class="middleColumn">$dataFieldByName(DeliverTimeframe)</div>
</div>
<div id="ProjectTeam" class="row field text">
<label class="left" for="{$FormName}_ProjectTeam">Project team</label>
<div class="middleColumn">$dataFieldByName(ProjectTeam)</div>
</div>
<div id="Contact" class="row field text">
<label class="left" for="{$FormName}_Contact">Contact</label>
<div class="middleColumn">$dataFieldByName(Contact)</div>
</div>
</fieldset>
<div class="Actions">
<% if Actions %>
<% control Actions %>$Field<% end_control %>
<% end_if %>
</div>
</form>

当我提交表单时,不是“请填写”客户“,而是。”消息我得到“请填写”“,这是必需的。”

详细信息字段外,所有字段都会发生这种情况!

有人可以告诉我我做错了什么吗?它杀了我......

2 个答案:

答案 0 :(得分:1)

好的,经过一些挖掘sapphire/javascript/Validator.js后,我发现包装div的第一个类必须是field所以我更换了这个 <div id="Customer" class="row field text"> <div id="Customer" class="field text row"> {{1}},一切正常。

答案 1 :(得分:0)

这应该通过创建表单类并实现“forTemplate()”函数来完成。请参阅SilverStripe Forms

此外,不是按照您的方式创建“AreaOfExpertise”源数组,而是有一种方便的方法toDropDownMap(),可以为您完成此任务。

$source = $list->toDropDownMap('ID', 'Name');

希望这有帮助。