Ajax validation in widget does not work...

Good morning,

first of all the important scripts. Error happens when calling index.php?r=site/index

app/controllers/site/IndexAction.php

[PHP]class IndexAction extends CAction

{

    public function run()


    {


            $this->controller->render('index');


    }

}[/PHP]

app/views/site/index.php

[PHP]<h1 class="alt">Output b4 widget</h1>

<hr />

<?php $this->widget(‘ext.smstrade.ESmsTrade’); ?>[/PHP]

app/extensions/smstrade/ESmsTrade.php (ESmsTrade extends CWidget)

[PHP]public function run()

{

    &#036;form = &#036;this-&gt;createForm();


    &#036;this-&gt;handleForm(&#036;form);

}

// This is where i enable ajax validation

private function createForm()

{

    // ...some unimportant code...


    &#036;form-&gt;activeForm = array('class' =&gt; 'CActiveForm',


                              'id' =&gt; self::ID_FORM,


                              'enableAjaxValidation' =&gt; true);


    return &#036;form;

}

private function handleForm($form)

{

    &#036;this-&gt;performAjaxValidation(&#036;form);


            


    //...[code code code]...


    &#036;this-&gt;render('form', array('form' =&gt; &#036;form));

}

private function performAjaxValidation($form)

{

    if(isset(&#036;_POST['ajax']) &amp;&amp; &#036;_POST['ajax'] == self::ID_FORM)


    {


            echo CActiveForm::validate(&#036;form-&gt;model);


            Yii::app()-&gt;end();


    }

}[/PHP]

As you can see my view already has some content before it calls the widget. Actually this isn’t a big problem unless it comes to an ajax validation call which is sending a POST request to index/site.

So the response contain the content of the view till the widget call, appended by the JSON code.

My question is: How can i avoid such a behavior? The only way i can think of is calling the widget directly before i render any HTML code - but thats not the way how it is supposed to work.

Ajax validation needs a bit of care more.

You have to find some compromize, for example:

app/controllers/site/IndexAction.php




class IndexAction extends CAction

{

        public function run()

        {

                if (isset($_POST['ajax']) && $_POST['ajax'] == self::ID_FORM)

                      [.. perform ajax validator ..]

                else

                      $this->controller->render('index');

        }

}



For perform ajax validator you can create a static method ext.smstrade.ESmsTrade::handleForm() that create the form and perform the ajax validation.

In one word, you cannot make the controller indipendent fromt the widget, that’s it…

Too bad! I hoped it would be possible in some kind of way (or by using an other technique).

But thanks for pointing that out for me!

I’ve now found a very simple solution. Since static methods were not possible in my case (I was not able to access the controller from static methods which I needed to access the render() method), I’ve simply called the widget at the beginning of the view, but captured its output.

app/views/site/index.php

[PHP]<?php $widget = $this->widget(‘ext.smstrade.ESmsTrade’, array(), true); ?>

<h1 class="alt">SMS versenden</h1>

<hr />

<?php echo $widget; ?>

<hr class="prepend-top" />

<p>

Ein bisschen blabla für untendrunter

</p>[/PHP]

I hope this is helpful for someone who is encountering the same problem. (AJAX Validation works fine now)

Oh, you can!


Yii::app()->controller

This works everywhere.