phpyiiyii-cformmodel

Prepopulating Yii form, from GET/POST


My problem is I have an HTML page that includes a short form on it. What I'd like is when this page posts/gets into my Yii model form, to be able to grab and pre-populate the empty form for the model with the values from the incoming form...

I dont think I can use the pagination widget because my initial page is HTML. Is there some way I can just pull this POST value in if it's set rather than the model.

Im still new to Yii so if this is a simple answer, I apologize

Edit: To clarify, my initial form has a few values from my main page form. The HTML page has 3 fields, say first name, last name, email address. That form then posts in to my full page form, which asks for additional information to complete the model. I'm hoping though that I can pre-populate the first and last name in the new php/yii form.

Basically I'm hoping when I first render _form.php I can grab a GET/POST value and assign it to the current model, or add it as a default value on the form.


Solution

  • "I'm hoping ... I can grab a GET/POST value and assign it to the current model"

    You sure can. Do this in your controller and you'll be set:

    $model->first_name = $_GET['first_name'];
    

    Of course, you may also want to validate those values and set a model scenario to make sure you don't end up with bad data being passed in by an attacker. Otherwise you could run into XSS attacks. But the ability to directly assign model attributes is nice and powerful ...