Question

After reading through YiiFramework and Ullman, there are a several things about forms that remain very unclear to me. I have tried many things, but generally think I must be missing something fundamental.

As an application, say I am creating a wall that users can post to. I can show all of the posts for a particular user with the following method in my controller:

public function actionShow($id)
{
    $userModel = new User;
    $user      = $userModel->findByPk($id);

    $postModel = new UserPost;
    $criteria = new CDbCriteria();
    $criteria->condition = 'user_id='.$id;

    $myPosts = $postModel->findAll($criteria);

    $this->render('show',array(
                  'user'    => $user,
                  'myPosts' => $myPosts,
                  'post'    => $postModel,
                  ));
}

The following (abbreviated) code in the view (show.php) renders the output:

echo '<h2>' . $user->first_name . '</h2>';

foreach ($myPosts as $myPost){
    echo $myPost->post;
    echo '<br/>';
}

I tried creating a form using the form builder:

return array(
    'title'=>'Write Something',

    'elements'=>array(
        'post'=>array(
            'type'=>'text',
            'maxlength'=>255,
        )
    ),

    'buttons'=>array(
        'post'=>array(
            'type'=>'submit',
            'label'=>'Post',
        ),
    ),
);

I then created a reference (in the above actionShow) using:

...
$myPosts = $postModel->findAll($criteria);
$postForm = new CForm('application.views.site.postForm', $postModel);
$this->render('show',array(
...

pass it into my view (show.php), and rendered at the top with:

$form->render();

or

echo $form;

At this point, it just renders the contents of the postForm.php file.

The questions...

  1. Is it correct to use CForm (or should I be using CActiveForm)?

  2. Why isn't it rendering the form?

  3. Should I be directly using CHtml in this case?

Thanks!

Was it helpful?

Solution

The example is really quite close to working. After looking through several more examples, I pieced together the details and actually starting making progress with Yii.

1. Is it correct to use CForm (or should I be using CActiveForm)?
In this case, CForm is correct. The question to ask is whether or not the model behind the 'post' is an ActiveRecord or not (i.e. does it extend CActiveModel or just CModel)? The use of CForm/CActiveForm should follow the model used. The code could be written to use either style of model, it just depends on how the developer is handling the information.

If you are populating a form that fills in a table entry for the database, then it makes sense to have an active model. If the form is just providing a piece of information to act upon (e.g. search), there may not even be a model.

I found this to be a really helpful example.

2. Why isn't it rendering the form?
The above code was based on the example on the Yii Framework page about using the form builder. To make this work, the form builder file needed the following code in the first line:

<?php

Without that, the page just renders the content as an array. This was not on the original page, but was found in some other examples.

3. Should I be directly using CHtml in this case?
Before discovering the solution to question #2, I implemented the form using CHtml and it worked great. The following code produces the same form (roughly) that was expressed in the form builder code above.

echo CHtml::beginForm();
echo CHtml::textField('Text', 'some value');
echo CHtml::submitButton('Post');
echo CHtml::endForm();

This is not reusable, but in this case, it's a really simple form. Regardless, I will likely recode this using the form builder.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top