Problem with ActiveRecord and CForm

I have created User model with additional fields for captcha and password repeat. I have added rules for them and I have added them to ‘register’ scenario. I have created CForm configuration file for registration form, but it do not show fields for register scenario. What should I do to tell CForm that it is register scenario?

Hi,

When you create the new user, you can give the scenario as a parameter:


$userModel = new User('register');

Then in the form configuration you can use:

‘model’=>$userModel,

‘type’=>‘form’

Could you please tell me, how you inserted the CCaptcha widget into the form? I’m using the form builder, but had no luck :( Here’s my first try - missing input field for captcha string, I know ;) but this thing throws an exception “Object of class CCaptcha could not be converted to string”




<?php


return array(

    'elements' => array(

        'name' => array(

            'type' => 'text',

            'class' => 'span-6',

        ),

        'email' => array(

            'type' => 'text',

            'class' => 'span-6',

        ),

        'body' => array(

            'type' => 'textarea',

            'class' => 'span-6',

            'cols' => 50,

            'rows' => 10,

        ),

        Yii::app()->controller->widget('CCaptcha'),

        'capatcha' => array(

            'label' => Yii::t('form', 'Insert verification code'),

            'type' => 'text',

        ),

    ),

    

    'buttons' => array(

        'submit' => array(

            'type' => 'submit',

            'label' => Yii::t('form', 'submit'),

        ),

    ),

);



Hi!

I had the same problem with the captcha image, but there is a solution for it. May not be the nicest solution, but it still works.

I have a function in the controller, just like the sitecontroller in the main code:




public function actions()

{

  return array('captcha'=>array('class'=>'CCaptchaAction'));

}



And then in the form builder I just create the captcha image:




ob_start();

$this->widget('CCaptcha', array('captchaAction'=>#url to the captcha action);

$image = ob_get_clean();



Then I can use the image in the form builder:




return array(

  'title'=>'Verification',

  $image,

  array(

    'name'=>'verifyCode',

    'type'=>'text',

  )

);



Hope this helps!

I encountered the same clue this afternoon and posted that:

follow this wiki -

http://www.yiiframework.com/wiki/733/how-to-show-a-captcha-in-cform/