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?
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.
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));
}