我有一个在jQuery对话框中弹出的编辑表单,它有一个Ajax提交按钮,可以在不关闭对话框或将其重定向到其他页面的情况下执行验证。我的问题是,在我的表单中,我显示当前值以及新值的空输入框,如果新值不是数字,则当前值仍会更改为该值。
例如,如果编辑窗口显示“当月”当前值为5且用户输入5sdf,则验证将启动并说明修复错误,但当前值列现在将显示5sdf。我需要帮助保持当前值保持其最新的有效值。
以下是控制器中编辑操作的代码:
public function edit($id = null)
{
$this->LocalClock->id = $id;
$this->set('isEditValid', false);
$this->set('editValidationErrors', false);
if ($this->request->is('get'))
{
$this->request->data = $this->LocalClock->read();
$this->set('localClocks', $this->LocalClock->read());
}
else
{
if ($this->LocalClock->save($this->request->data))
{
$this->set('isEditValid', true);
$this->set('editValidationErrors', false);
$this->set('localClocks', $this->request->data);
}
else
{
$this->set('isEditValid', false);
$this->set('editValidationErrors', true);
$this->set('localClocks', $this->request->data);
}
}
}
这是我的edit.ctp:
<div id="edit_table">
<div id="edit_errors">
</div>
<?php echo $this->Form->create('LocalClock', array('action' => 'edit', 'inputDefaults' => array('label' => false))); ?>
<?php echo $this->Form->input('id', array('type' => 'hidden')); ?>
<table>
<thead><tr>
<th>Field</th> <th>Current Value</th> <th>New Value</th>
</tr></thead>
<tbody>
<tr><td>Name</td><td> <?php echo $localClocks['LocalClock']['name']; ?></td><td> <?php echo $this->Form->input('LocalClock.name', array('id' => 'focusMeEdit'));?></td></tr>
<tr><td>Auto Offset</td><td><?php echo $localClocks['LocalClock']['auto_offset'];?> </td><td><?php echo $this->Form->input('LocalClock.auto_offset');?> </td></tr>
<tr><td>UTC Offset Sec</td><td><?php echo $localClocks['LocalClock']['utc_offset_sec'];?> </td><td><?php echo $this->Form->input('LocalClock.utc_offset_sec');?></td></tr>
<tr><td>In Month</td><td><?php echo $localClocks['LocalClock']['in_month'];?></td> <td><?php echo $this->Form->input('LocalClock.in_month');?> </td> </tr>
</tbody>
</table>
<?php
// The Ajax Helper submit is used to submit the changes made to the edit form
// because it allows the modal window to be loaded with the updated edit view
// without leaving the modal window.
echo $this->Ajax->submit('Save Changes', array(
'url' => array('controller' => 'localClocks', 'action' => 'edit'),
'update' => 'edit_table'));
// Call the close Dialog() function if data was validated sucessfully; valid == true.
// Else it stays open displaying validation error message.
if (false != $isEditValid)
{
echo "<script>$('#[id*=dialog]').dialog('close');</script>";
echo "<script>$('#success_messages').dialog('open');</script>";
echo "<script>$('#success_messages').text('Local Clock Successfully Edited')</script>";
}
if(false != $editValidationErrors)
{
echo "<script>$('#edit_errors').html('<h3><em><font color=\"red\">Please Correct Errors Before Proceeding</font></em></h3>')</script>";
}
echo $this->Form->end(); ?>
</div>
我不确定问题是否在我的行动或视图中导致它发生。
提前致谢
答案 0 :(得分:1)
显示错误值的标准行为。如果您输入的答案很长,由于某种原因无效,您就不希望删除答案,否则您可能需要进行大量的重新安排。
保存失败后,为什么不读取原始记录,请检查$ this-&gt; LocalClock-&gt; invalidFields()以查找错误,并将$ this-&gt; request-&gt;数据替换为受影响的原始值字段?