Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5975431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:05:03+00:00 2026-05-22T21:05:03+00:00

When the user clicks on create and the validation of the model fails, my

  • 0

When the user clicks on create and the validation of the model fails, my site redirects to the same page displaying the exact same form. I have been doing some research on how to load the previously typed in values so as not to make the user type it all in again. With firebug i could observe the post contents and these dont seem to contain such information.

Looking up on the internet i saw someone suggesting to use Ajax to submit the form and have it return JSON data specifying whether validation was successful or not. However, im pretty new to Yii and PHP altogether, so i would need some guidance to be able to get started with this.

Is there another (simpler) way? In case not, how could i have a JSON return specifying if the validation was successful or not? Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T21:05:03+00:00Added an answer on May 22, 2026 at 9:05 pm

    It seems to me like what you need to do is look at the $_POST variables to see which dependent dropdown was selected, and pre-render it when the form validation fails so Yii’s default validation errors work. Using this link as an example:

    First, refactor out part of the actionDynamiccities() method into a separate public (non-action) method in your controller:

    public function getCitiesList($country_id) {
        $data=Location::model()->findAll('parent_id=:parent_id', 
                  array(':parent_id'=>(int) $country_id));
        return CHtml::listData($data,'id','name');
    }
    

    Then fix your refactored AJAX action like so:

    public function actionDynamiccities()
    {
      $data = $this->getCitiesList($_POST['country_id']);
      foreach($data as $value=>$name)
      {
        echo CHtml::tag('option',
                   array('value'=>$value),CHtml::encode($name),true);
      }
    }
    

    Then in your _form.php view file, in the #city_id div where the second dropdown would be AJAXed in by the first, add a little PHP snippet like this:

    <div id="city_id">
    <?php if(isset($_POST['city_id'])): ?>
      <?php foreach($this->getCitiesList($_POST['country_id']) as $value=>$name): ?>
        <?php echo CHtml::tag('option', array('value'=>$value),CHtml::encode($name),true); ?>
      <?php endforeach; ?>
    <?php endif; ?>
    </div>
    

    This way, if you have already submitted the form and the $_POST[‘country_id’] is set, the dependent dropdown is already rendered and it should display the appropriate validation errors.

    If the user selects a different country the city dropdown should be replaced like normal still.

    I didn’t test this code but it should work in principle. Good luck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I create a new page using HTMl and I whant when user clicks on
I've got a form that is using jQuery validation. When the user clicks the
I have a form that is generated when a user clicks a button, and
I have a simple form that uses jQuery validation to notify the user of
I have have a site where the user can alter the page content using
I have a text box on a page, and when the user clicks Submit
When a user clicks a submit button I want the form to be submitted.
So I have been using data annotations for my validation in an MVC project
I'm trying to create a simply login page. I want validation on that page
I have a model with a integer field wich will increment on user click,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.