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 7045969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:35:41+00:00 2026-05-28T02:35:41+00:00

To complete a Yii form field, users often need to search for a referenced

  • 0

To complete a Yii form field, users often need to search for a referenced model record (like searching for a friend’s profile in a social app). I’m sure other Yii apps are doing this elegantly. But in my dirty approach, in the search results page, I use a CHtml::submitButton to POST two models back to the form containing:

  1. the “found” record (a user id associated with one of the profiles from the search results)
  2. the previously entered form field contents (relationship characterization fields)

Alternatively, the autocomplete widget works well, but doesn’t do the detailed search that I need (e.g. search based on a partial name and city or state or other user profile content).

Alternatively, you’d think that within the search results view I might be able to modify the form member to contain the found record (new friend’s user id) and just POST/submit the modified model from the search results page . But for that to work each of the search results in the list needs a unique user id populated in that form field, and I can’t figure out how to duplicate the form model before modifying that one member server-side for each of the search results’ “submit” or “select” buttons, and it just doesn’t seem right to create all those form models.
So what seems to work is to submit two separate models using subforms (within the search results view) , with the submitButton POSTing a model and the extra parameter (user id) separately.

Is there a better way? …to link to and from a search results page and a form field, retaining already-entered data and populating the searched for field with a selected record from the search results.

Here’s controllers/SiteController:

public function actionBefriend() {
    $model=new BefriendForm;
    if(isset($_POST['BefriendForm'])) {
        $model->attributes=$_POST['BefriendForm'];
        if ($model->validate()) {
            $model->createFriendship();
            $this->redirect('Index'); } 
        else
            $er=$model->getErrors(); }
    if(isset($_POST['idfriend'])) {
        $model->idfriend=$_POST['idfriend']; }
    if(isset($model->idfriend)) {
        $model->friend_name=Bio::model()->findByPk($model->idfriend)->name; }
    $this->render('newFrienship', array('model' =>$model)); // newFriendship is the form view }

Here’s controllers/Bio.php (Profile)

public function actionIndex() {
    $criteria = new CDbCriteria();
    $model=new BefriendForm;
    if(isset($_GET['q']))
        $q = $_GET['q'];
    elseif (isset($_POST['BefriendForm']))  {
        $model->attributes=$_POST['BefriendForm'];
        $q = $model['friend_name']; }
    if(isset($q))   {
        $criteria->compare('name', $q, true, 'OR');
        $criteria->compare('city', $q, true, 'OR');
        $criteria->compare('state', $q, true, 'OR');
        $criteria->compare('bio_text', $q, true, 'OR'); }
    else
        $q = '';
    $dataProvider=new CActiveDataProvider('Bio', array('criteria'=>$criteria));
    $this->render('index',array('dataProvider'=>$dataProvider, 'q'=>$q, 'model'=>$model )); }

Here’s the start of views/site/newFriendship (form view)

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'newFriendship-BefriendForm',
    'enableAjaxValidation'=>true,)); ?>

Here’s the core of views/bio/index.php (search results index page):

<?php $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view',
'viewData'=>array('model'=>$model) )); ?>

Here’s the search result row in views/bio/_view.php that links back to BefriendForm (SiteController) that sends the id of the user to populate the friend field in the form (which gets a name from the id):

<form method="POST">
    <input type="hidden" name="idfriend" value="<?php echo $data->idfriend ?>" />
    // Here's that submit button that I can't get to send both the model 
    //and the idfriend back to the form to repopulate it
    //   without manually writing HTML to submit all the fields individually
    //   or creating 2 subforms to submit together with a signle submitButton.
    <?php echo CHtml::submitButton('Befriend', array('submit' => array('site/Befriend'),'model'=$model);
</form>
  • 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-28T02:35:41+00:00Added an answer on May 28, 2026 at 2:35 am

    Turns out you can just replace the form field containing the primary key with the appropriate value before POSTing back to the NewFriendship form when the user clicks the “Select” or “Befriend” button. So only one model is posted back to the original form from the search results page.

    Replace the section from views/bio/_view.php in the question with…

    <?php
    foreach($model->attributeNames() as $name)
      if($name != 'friend_id')
        echo CHtml::activeHiddenField($model,$name);
      else
        echo CHtml::activeHiddenField($model,$name,array('value'=>$data->getPrimaryKey()));
    echo CHtml::submitButton('Befriend', array('submit' => array('site/Befriend')));
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

To complete some testing I need to load the 64 bit version of an
Complete Flash / AS Noob here. Friend wanted a change in Email address and
The complete error I am getting is this: Model compatibility cannot be checked because
I'm a complete newb when it comes to regex and I need a way
I have a complete XML document in a string and would like a Document
Complete beginner to Rails here: In Rails: I have a Model Post, which hasmany
Complete Oracle n00b here. I have to maintain a legacy form that was developed
I need to complete this job in Batch. I use NSLOOKUP command to find
Complete C# newbie here, so cut me some slack. I have this application that
Complete C++ i18n gettext() hello world example has C++ code that works for a

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.