将动态字段数据发布回Cakephp中的视图

时间:2014-03-18 00:00:53

标签: jquery ajax validation cakephp dynamic

我正在为学校的非盈利组织开发一个系统,如果存在验证错误,我需要将动态字段中的数据发布回视图。下面是创建动态字段的代码。

- 这是jquery函数

var lastRow=0;

function addPerson() {
    lastRow++;
    $("#victimsTable tbody>tr:#victim0").clone(true).attr('id','victim'+lastRow).removeAttr('style').insertBefore("#victimsTable tbody>tr:#trAdd");
    $("#victim"+lastRow+" button").attr('onclick','removePerson('+lastRow+')');
    $("#victim"+lastRow+" input:eq(0)").attr('name','data[Victim]['+lastRow+'][victimId]').attr('id','victimVictimId'+lastRow);
    $("#victim"+lastRow+" input:eq(1)").attr('name','data[Victim]['+lastRow+'][firstName]').attr('id','victimFirstName'+lastRow);
    $("#victim"+lastRow+" input:eq(2)").attr('name','data[Victim]['+lastRow+'][lastName]').attr('id','victimLastName'+lastRow);
    $("#victim"+lastRow+" input:eq(3)").attr('name','data[Victim]['+lastRow+'][dateOfBirth]').attr('id','victimDateOfBirth'+lastRow);
    $("#victim"+lastRow+" select.g").attr('name','data[Victim]['+lastRow+'][gender]').attr('id','victimGender'+lastRow);
    $("#victim"+lastRow+" select.r").attr('name','data[Victim]['+lastRow+'][race]').attr('id','victimRace'+lastRow);
    $("#victim"+lastRow+" input:eq(4)").attr('name','data[Victim]['+lastRow+'][streetAddress]').attr('id','victimStreetAddress'+lastRow);
    $("#victim"+lastRow+" input:eq(5)").attr('name','data[Victim]['+lastRow+'][zipCode]').attr('id','victimZipCode'+lastRow);
    $("#victim"+lastRow+" input:eq(6)").attr('name','data[Victim]['+lastRow+'][city]').attr('id','victimCity'+lastRow);
    $("#victim"+lastRow+" input:eq(7)").attr('name','data[Victim]['+lastRow+'][state]').attr('id','victimState'+lastRow);
    $("#victim"+lastRow+" input:eq(8)").attr('name','data[Victim]['+lastRow+'][email]').attr('id','victimEmail'+lastRow);
    $("#victim"+lastRow+" input:eq(9)").attr('name','data[Victim]['+lastRow+'][phoneOne]').attr('id','victimPhoneOne'+lastRow);
    $("#victim"+lastRow+" select.p1t").attr('name','data[Victim]['+lastRow+'][phoneOneType]').attr('id','victimPhoneOneType'+lastRow);
    $("#victim"+lastRow+" input:eq(10)").attr('name','data[Victim]['+lastRow+'][phoneTwo]').attr('id','victimPhoneTwo'+lastRow);
    $("#victim"+lastRow+" select.p2t").attr('name','data[Victim]['+lastRow+'][phoneTwoType]').attr('id','victimPhoneTwoType'+lastRow);
    AddVictimCalScript(lastRow);
    AddVictimChosenScript(lastRow);
    AddVictimChosenRaceScript(lastRow);
}
function removePerson(x) {
    $("#victim"+x).remove();
}

- 这些是字段

的输入
<h3>Add Victims</h3>
<div class="table-responsive">
<table class="table" id="victimsTable">
<tr>
<tr id="victim0" style="display:none;">
    <td><?php echo $this->Form->button('&nbsp;Remove this Victim&nbsp;',array('type'=>'button','title'=>'Click Here to remove this victim','onclick'=>'removePerson()')); ?>

    <?php echo $this->Form->input('unused.victimId',array('type'=>'text','id'=>'resultField2')); ?>

    <?php echo $this->Form->input('unused.firstName',array('label'=>'First Name')); ?>
    <?php echo $this->Form->input('unused.lastName',array('label'=>'Last Name')); ?>
    <?php echo $this->Form->input('unused.dateOfBirth',array('type'=>'text')); ?>
    <?php echo $this->Form->input('unused.gender',array('class'=>'g','label'=>'Gender','type'=>'select','options'=>array('Unassigned'=>'----','Male' =>'Male','Female'=>'Female'))); ?>
    <?php echo $this->Form->input('unused.race',array('class'=>'r','label'=>'Race','type'=>'select','options'=>$race)); ?>
    <?php echo $this->Form->input('unused.streetAddress',array('label'=>'Street Address')); ?>
    <?php echo $this->Form->input('unused.zipCode',array('label'=>'Zip Code')); ?>
    <?php echo $this->Form->input('unused.city',array('label'=>'City')); ?>
    <?php echo $this->Form->input('unused.state',array('label'=>'State')); ?>
    <?php echo $this->Form->input('unused.email',array('label'=>'Email')); ?>
    <?php echo $this->Form->input('unused.phoneOneType',array('class'=>'p1t','label'=>'Phone One Type','type'=>'select','options'=>$phonetype)); ?>
    <?php echo $this->Form->input('unused.phoneOne',array('label'=>'Phone One')); ?>
    <?php echo $this->Form->input('unused.phoneTwoType',array('class'=>'p2t','label'=>'Phone Two Type','type'=>'select','options'=>$phonetype)); ?>
    <?php echo $this->Form->input('unused.phoneTwo',array('label'=>'Phone Two')); ?>
    </td>
</tr>
<tr id="trAdd"> </tr>
<tfoot>
    <tr>
    <td id="trAdd"> <?php echo $this->Form->button('Add Victim(s)',array('type'=>'button','label'=>'Click Here to add another victim','onclick'=>'addPerson()')); ?> </td>
    <tr>
</tfoot>
</table>
</div>

我的问题是,如果存在验证错误,如何将数据发布回来查看?代码保存完美,但我对cakephp,ajax和jquery编程非常新。任何帮助都将非常感谢

1 个答案:

答案 0 :(得分:0)

你可以试试这个:

$this->Model->save( $data, false);

但是如果你必须省略验证,则意味着错过了你的概念。您应该更改验证标准或更改数据库中的方法。

另一种可能性是您的应用中有安全组件。它可以防止使用客户端生成的动态表单而不使用 jQuery wrapper 。她是如何做到的。

http://book.cakephp.org/2.0/en/core-libraries/helpers/js.html