Register here!

Forgot password?

ASK NEW QUESTION

Home Page » Views » Ajax Cform validation within renderPartial view
1 Vote
Vote Con!

Ajax Cform validation within renderPartial view

AJAXAJAXLINKajaxSubmitButtoncformrenderPartialvalidation

hi there,

I have created an list of categories, each category link uses ajaxLink() function to call a action in the controller to load a new view file via AJAX. View file are loaded in the div using renderPartial(). this part works fine.

One of the Ajax view contains a cform object and it fields which also loads fine in the view. Now i am trying to add Ajax validation on this form but it does not work, basically every time the form is submitted the view is redirected to another page that has no styles just plain text with form and validation error.

the model also contains error checking.

my form is based on the Cform from the doc or api, as follow:

  1.  $form = $this->beginWidget('CActiveForm', array(
  2.     'id'=>'user-form',
  3.     'enableAjaxValidation'=>true,
  4.     'enableClientValidation'=>true,
  5.     'focus'=>array($model,'firstName'),
  6. )); // and so on with fields
 $form = $this->beginWidget('CActiveForm', array(
    'id'=>'user-form',
    'enableAjaxValidation'=>true,
    'enableClientValidation'=>true,
    'focus'=>array($model,'firstName'),
)); // and so on with fields
  1. // controller
  2.  if(isset($_POST['ajax']) && $_POST['ajax']==='user-form')
  3.     {
  4.         echo CActiveForm::validate($model);
  5.         Yii::app()->end();
  6.     }
// controller
 if(isset($_POST['ajax']) && $_POST['ajax']==='user-form')
    {
        echo CActiveForm::validate($model);
        Yii::app()->end();
    }

how to slove this problem. is it even possible to load element using Ajax within many different views?

By TheDeveloper in Views · Asked 470 days 22 hours 57 mins ago

Questions: 1 Accepted: 0 ( 0% ) | Reputation: 0

 

Answers (4)

  1. 0 votes

    Did you process your output on your renderPartial function? Set it to true…

    By tonydspaniard · 470 days 20 hours 18 mins ago

    Questions: 2 Accepted: 0 ( 0% ) | Reputation: 0

    • I managed to fix using Ajaxlink() instead and setting post type of the link to POST. i also set the last part renderPartial to true. But i notice another problem or unless there is a bug in Yii framework. when a new view is load using AjaxLink using renderPartial function, jQuery files are also loaded again, i already load jQuery in my main layout so don't need it to load ot twice. how do i stop this?

      By TheDeveloper · 469 days 23 hours 57 mins ago

  2. 0 votes

    Yes, you do that by setting the following before you render the partial view:

    1. Yii::app()->clientScript->corePackages = array();
    Yii::app()->clientScript->corePackages = array();

    By tonydspaniard · 465 days 6 hours 9 mins ago

    Questions: 2 Accepted: 0 ( 0% ) | Reputation: 0

  3. 0 votes

    Tony that fixes the problem partialy. If I set corepackages to empty array and then in the view I have for example a bootstrap widget, it again registers the coreScripts (jqeury and bootstrap) which causes problems.
    I’m currently struggling with this problem. If you have any ideas, you are welcome. I dont need any script files to be registered, just the scripts. And I dont want to modify the fw source.

    By phreak · 314 days 4 hours 51 mins ago

    Questions: 1 Accepted: 0 ( 0% ) | Reputation: 0

© YiiAnswers.com 2011. All rights reserved.

Switch to our mobile site