本文介绍了Yii框架:AJAX形式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我需要一些看起来很简单的帮助,但我只是想不通.

Ok, I need help with something that seems pretty straightforward but I just can't figure out.

我在Yii中有一个页面,我试图在其中嵌入AJAX表单.让我们将页面称为A.该表单采用单个值,如果可以的话,需要对其进行验证并将其存储到DB中.

I have a page in Yii into which I'm trying to embed an AJAX form. Let's call the page A. The form takes in a single value, and needs to validate and store it into the DB if it's alright.

到目前为止,这是我所发现的:

So far, here's what I've figured out:

该表单位于_form.php视图中,该视图包含一个CActiveForm和一个如下所示的ajaxSubmitButton:

The form is in a view _form.php, which contains a CActiveForm and an ajaxSubmitButton which looks like this:

<?php echo CHtml::ajaxSubmitButton('submit', $this->createUrl('/site/something'), array('update'=>'#targetdiv'));?>

在另一个A的视图中调用该表单,如下所示:

The form is called within another A's view like so:

<?php echo $this->renderPartial('/site/_form', array('AModel'=>$model)); //Passing some info about A ?>

在控制器的actionSomething中,我正在执行以下操作:

In the controller's actionSomething, I'm doing the following:

if (Yii::app()->request->isAjaxRequest) {

  $model = new AJAXForm('submit');

  if (isset($_POST['AJAXForm'])) {

    $model->attributes = $_POST['AJAXForm'];

    if ($model->validate()) {
    //When data's valid, save to DB is working fine. This part is working perfectly.
    }
    else {
      //This is the part I'm confused about and that's not working

      /*Trying to render the form to get the error messages and summary displayed
      but nothing's showing */
      $this->renderPartial('/site/_form', array('AModel'=>$model));

      Yii::app()->end();

    }
  }
}

在Firebug中,我确实看到遇到错误时,响应再次包含整个部分呈现的表单.但是,targetdiv不会使用包含错误消息的更新表单进行更新.

In Firebug, I do see that when an error is encountered, the response contains the entire partial rendered form again. However targetdiv is not getting updated with the updated form with the error messages.

我有一种感觉,我在actionController中做错了什么,但我不知道该怎么办.如果我也能看到AJAX提交表单的完整示例,那将会很有帮助.

I have a feeling I'm doing something wrong in actionController, but I can't figure out what. It would be helpful if I could see a full example of an AJAX submitted form as well.

谢谢!

推荐答案

$model->getErrors()将为您提供所有属性的所有错误

$model->getErrors() would give you all the errors for all attributes

http://www.yiiframework.com/doc/api/1.1/CModel#getErrors-detail

if ($model->validate()) {
  //When data's valid, save to DB is working fine. This part is working perfectly.
  }
else {
  $errors = $model->getErrors();
  echo $errors;

  Yii::app()->end();
}

然后根据Yii论坛上的帖子将其传递到ajaxSubmitButton() ajax选项中: http://www.yiichina.net/forum/index.php/topic/23236-extension-how-to- display-validation-errors-comming-from-ajax-validation/

And then pass this into ajaxSubmitButton() ajax option, according to this post on Yii forum: http://www.yiichina.net/forum/index.php/topic/23236-extension-how-to-display-validation-errors-comming-from-ajax-validation/

'success'=>"function(html) {
   if (html.indexOf('{')==0) {
        var e = jQuery.parseJSON(html);
        jQuery.each(e, function(key, value) {
        jQuery('#'+key+'_em_').show().html(value.toString());
        jQuery('#'+key).addClass('clsError');
        jQuery('label[for='+key+']').addClass('clsError');
   });
}

这篇关于Yii框架:AJAX形式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 17:41