Yii Framework Forum: CActiveForm or CForm? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

CActiveForm or CForm? what's differences? Rate Topic: -----

#1 User is offline   zitter 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 140
  • Joined: 14-July 09

Posted 03 May 2010 - 02:06 AM

Sorry for stupid question: what's differences between CActiveForm and CForm? When I have to use one or the other?

Q#2: I think Form.builder is very cool. Personally I prefer it to the classic system, but I've noticed that in Gii is using the classic one to generate forms. So, for the future, is it better to use form.builder or remain to the classic?

TIA
Danilo
0

#2 User is offline   Onman 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 173
  • Joined: 26-December 09
  • Location:The Netherlands

Posted 03 May 2010 - 02:49 AM

Q#1: The main difference is that CActiveForm can perform ajax validation (build in) while CForm can't (you'll have to develop that yourself).
Next to that, CActiveForm is derived from CWidget and can therefore benefit from the pro's of a widget, i.e. it has it's own set of controller actions. CForm is derived from CFormElement.

Q#2: Good question. I'd like to know this, too.
0

#3 User is offline   KJedi 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 381
  • Joined: 19-October 08
  • Location:Nikolaev, Ukraine (Europe)

Posted 11 May 2010 - 11:05 AM

Answering both questions:
1) You can do this easily bu specifying config property for the CForm: http://www.yiiframew...tiveForm-detail
Here's the code that does the same as auto-generated login form does:
- SiteController
public function actionLogin() {
		$model=new LoginForm;

		// if it is ajax validation request
		if(isset($_POST['ajax']) && $_POST['ajax']==='login-form') {
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}

		// collect user input data
		if(isset($_POST['LoginForm'])) {
			$model->attributes=$_POST['LoginForm'];
			// validate user input and redirect to the previous page if valid
			if($model->validate() && $model->login())
				$this->redirect(Yii::app()->user->returnUrl);
		}
		// display the login form
		$form = new CForm('application.views.site.loginForm', $model);
		$this->render('login',array('form'=>$form));
	}

- loginForm.php
<?php
return array(
    'title'=>'Please provide your login credential',
	'activeForm' => array(
			'class' => 'CActiveForm',
			'enableAjaxValidation' => true,
			'id' => 'login-form',
		),
    'elements'=>array(
        'username'=>array(
            'type'=>'text',
            'maxlength'=>32,
        ),
        'password'=>array(
            'type'=>'password',
            'maxlength'=>32,
        ),
        'rememberMe'=>array(
            'type'=>'checkbox',
        )
    ),

    'buttons'=>array(
        'login'=>array(
            'type'=>'submit',
            'label'=>'Login',
        ),
    ),
);
?>


That's it. Key points:
- You validate using model, not CForm. You can use the latter, but I don't see any benefit in this. Maybe I'm mistaken
- You specify the config for the CActiveFrom widget in the form config.

2) This depends on the app. I'm pretty sure Yii will generate form configs in the future. I am going to use CForms in my projects.
Posted Image
My Blog: http://programmersnotes.info/ - tips, design patterns, client and server-side coding, yii articles and simply my experience.

Follow me on twitter: http://twitter.com/KonstantinMirin
Add me to your friends on facebook: http://www.facebook....hp?id=784832902
1

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users