CakePHP:在模态中保持验证错误

时间:2016-07-08 10:06:44

标签: php jquery ajax cakephp modal-dialog

我有一个模态的用户添加表单。

UserController中:

   public function admin_add() {
  if(!$this->request->is('ajax')) {
    $this->render('admin_add');
  } else {
    $this->layout = false;
  }
  $this->set('saved', false);
  if($this->request->is('post') && !empty($this->request->data)) {
    $this->User->create();
    if($this->User->save($this->request->data)) {
      $this->set('saved', true);
      $this->Flash->success(__('The user has been saved'));
      return $this->redirect(['action' => 'index']);
    }
    $this->Flash->error(__('The user could not be saved. Please, try again.'));
  }
  $groups = $this->User->Group->find('list');
  $this->set(compact('groups'));
}

的usermodel:

    public $validate = [
  'username' => [
    'required' => [
      'rule' => 'notBlank',
      'message' => 'A username is required'
    ],
    'login' => array(
      'rule' => 'isUnique',
      'message' => 'This username has already been taken.'
    )
  ],
  'password' => [
    'required' => [
      'rule' => 'notBlank',
      'message' => 'A password is required'
    ]
  ],
];

UserAddView(表格):

echo $this->Form->create('User', [
'horizontal' => true,
'div' => false,
'vp' => 'xs',
'cols' => [
  'label' => 3,
  'input' => 9,
  'error' => 12,
  'submit' => false
]
]);
echo $this->Form->input('username', ['type' => 'text']);
echo $this->Form->input('password', ['type' => 'password']);
echo $this->Form->input('group_id', ['type' => 'select']);
?>
<div class="modal-footer">
<?php
echo $this->Form->button('Cancel', ['data-dismiss' => 'modal']);
echo $this->Js->submit('Save', [  //create 'ajax' save button
                                  'update' => '#body',  //id of DOM element to update with selector
                                  'div' => false,
                                  'type' => 'json',
                                  'async' => false
]);
?>
</div>
<?php

if(false != $saved) { //will only be true if saved OK in controller from ajax save above
echo "<script>
    $('#data-modal').modal('hide');  //close containing dialog
</script>";
}
echo $this->Form->end();
echo $this->Js->writeBuffer();

?>

JS for Modal:

    $('body').on('click', 'a[href][rel="modal"]', function (event) {
    event.preventDefault();
    var url = $(this).attr('href');
    var title = '&nbsp;';
    var width = '';
    var height = '';
    if ($(this).attr('data-modal-title')) {
        title = $(this).attr('data-modal-title');
    }
    if ($(this).attr('data-modal-width')) {
        width = $(this).attr('data-modal-width');
    }
    if ($(this).attr('data-modal-height')) {
        height = $(this).attr('data-modal-height');
    }
    //@foff
    var modal = $(
        '' +
        '<div id="data-modal" class="modal fade" tabindex="-1" role="dialog" aria-hidden="true">' +
            '<div class="modal-dialog" style="width:' + width + ';">' +
                 '<div class="modal-content">' +
                    '<div class="modal-header">' +
                         '<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>' +
                         '<h4 class="modal-title">' + title + '</h4>' +
                    '</div>' +
                    '<div class="modal-body" id="modal-content" style="height:' + height + ';">' +
                    '</div>' +
                '</div>' +
             '</div>' +
        '</div>' +
        ''
    );
    //@fon
    if ($(this).attr('data-modal-rel')) {
        modal.attr('data-rel', $(this).attr('data-modal-rel'));
    }
    //if ($(this).attr('data-modal-class')) {
    //    modal.removeClass('fullscreen').addClass($(this).attr('data-modal-class'));
    //}
    modal.modal('show');
    modal.find('#modal-content').load(url);
});

目前,当我获得Validationerror时,我将重定向到没有布局的添加视图。但我想在我的表单所在的模态中保留错误。 我不知道我应该怎么做...请帮忙:) CakePHP 2.6

0 个答案:

没有答案