update/create form

I have an Update and create form that does not want to work

It looks like this

<?php $form=$this->beginWidget(‘CActiveForm’, array(

'id'=&gt;'anagrafe-form',


'enableAjaxValidation'=&gt;true,


'clientOptions'=&gt;array('validateonsubmit'=&gt;true, 'validateonchange'=&gt;true),

));

?>

<div class="form">

&lt;p class=&quot;note&quot;&gt;I campi con  &lt;span class=&quot;required&quot;&gt;*&lt;/span&gt; sono obbligatori.&lt;/p&gt;





&lt;?php echo &#036;form-&gt;errorSummary(&#036;model); ?&gt;





&lt;div class=&quot;row&quot; align=&quot;left&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'CODICE_FISCALE'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'CODICE_FISCALE',array('size'=&gt;16,'maxlength'=&gt;16)); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'CODICE_FISCALE'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot; align=&quot;left&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'NOME'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'NOME',array('size'=&gt;50,'maxlength'=&gt;50)); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'NOME'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot; align=&quot;left&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'COGNOME'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'COGNOME',array('size'=&gt;50,'maxlength'=&gt;50)); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'COGNOME'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot; align=&quot;left&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'DATA_NASCITA'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'DATA_NASCITA',array(&quot;id&quot;=&gt;&quot;DATA_NASCITA&quot;)); ?&gt;


	&lt;?php echo CHtml::image(&quot;images/calendario.gif&quot;,&quot;calendar&quot;,array(&quot;id&quot;=&gt;&quot;c_button_nascita&quot;,&quot;class&quot;=&gt;&quot;pointer&quot;,'width'=&gt;&quot;25px&quot;,&quot;height&quot; =&gt; '25px')); ?&gt;


		&amp;nbsp;(calendar appears when image is clicked)


 	&lt;?php &#036;this-&gt;widget('application.extensions.calendar.SCalendar',


    array(


    'inputField'=&gt;'DATA_NASCITA',


    'button'=&gt;'c_button_nascita',


    'skin'=&gt;&#036;skin,


    'stylesheet'=&gt;&#036;style,


    'ifFormat'=&gt;'%Y-%m-%d',


));


?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'DATA_NASCITA'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot; align=&quot;left&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'COMUNE_COMUNE_ID_NASCE'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'COMUNE_COMUNE_ID_NASCE'); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'COMUNE_COMUNE_ID_NASCE'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot; align=&quot;left&quot; &gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'CITTADINANZA'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'CITTADINANZA',array('size'=&gt;60,'maxlength'=&gt;240)); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'CITTADINANZA'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot; align=&quot;left&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'STATO_CIVILE');?&gt;


	&lt;?php echo &#036;form-&gt;dropDownList(&#036;model,'STATO_CIVILE',array(1=&gt;'celibe',2=&gt;'Nubile',3=&gt;'Sposato',4=&gt;'Separato'));?&gt;


	&lt;?php // echo CHtml::activeTextField(&#036;model,'STATO_CIVILE',array('size'=&gt;30,'maxlength'=&gt;30)); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'STATO_CIVILE'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot; align=&quot;left&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'SESSO'); ?&gt;


	&lt;?php //echo CHtml::activeRadioButtonList(&#036;model,'SESSO',array(1=&gt;'M',2=&gt;'F'),array('size'=&gt;1,'maxlength'=&gt;1,'separator'=&gt;'  '));?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'SESSO',array('size'=&gt;1,'maxlength'=&gt;1)); ?&gt;


		&amp;nbsp;(digitare m/f )


	&lt;?php echo &#036;form-&gt;error(&#036;model,'SESSO'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot; align=&quot;left&quot; &gt;


	&lt;?php echo CHtml::activeLabelEx(&#036;model,'NOTE'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'NOTE',array('size'=&gt;60,'maxlength'=&gt;240)); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'NOTE'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot; align=&quot;left&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'INDIRIZZO'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'INDIRIZZO',array('size'=&gt;60,'maxlength'=&gt;110)); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'INDIRIZZO'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot; align=&quot;left&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'CAP'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'CAP',array('size'=&gt;5,'maxlength'=&gt;5)); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'CAP'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot; align=&quot;left&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'COMUNE_COMUNE_ID');?&gt;


	&lt;?php echo &#036;form-&gt;dropDownList(&#036;model,'COMUNE_COMUNE_ID',CHtml::listData(Comune::model()-&gt;findAll(array('order'=&gt;'NOME')),'COMUNE_ID','NOME'));?&gt;


	&lt;?php //echo CHtml::activeTextField(&#036;model,'COMUNE_COMUNE_ID'); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'COMUNE_COMUNE_ID'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot; align=&quot;left&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'DOMICILIO'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'DOMICILIO',array('size'=&gt;60,'maxlength'=&gt;100)); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'DOMICILIO'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot; align=&quot;left&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'CAP_DOM'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'CAP_DOM',array('size'=&gt;5,'maxlength'=&gt;5)); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'CAP_DOM'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot; align=&quot;left&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'COMUNE_COMUNE_ID_DOMICILIA'); ?&gt;


	&lt;?php echo &#036;form-&gt;dropDownList(&#036;model,'COMUNE_COMUNE_ID',CHtml::listData(Comune::model()-&gt;findAll(array('order'=&gt;'NOME')),'COMUNE_ID','NOME'));?&gt;


	&lt;?php //echo CHtml::activeTextField(&#036;model,'COMUNE_COMUNE_ID_DOMICILIA'); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'COMUNE_COMUNE_ID_DOMICILIA'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot; align=&quot;left&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'TELEFONO_1'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'TELEFONO_1',array('size'=&gt;30,'maxlength'=&gt;30)); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'TELEFONO_1'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot; align=&quot;left&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'TELEFONO_2'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'TELEFONO_2',array('size'=&gt;30,'maxlength'=&gt;30)); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'TELEFONO_2'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot; align=&quot;left&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'CELLULARE'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'CELLULARE',array('size'=&gt;30,'maxlength'=&gt;30)); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'CELLULARE'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot; align=&quot;left&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'E_MAIL'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'E_MAIL',array('size'=&gt;60,'maxlength'=&gt;100)); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'E_MAIL'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot; align=&quot;left&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'FAX'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'FAX',array('size'=&gt;30,'maxlength'=&gt;30)); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'FAX'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot; &gt;


	&lt;?php echo &#036;form-&gt;hiddenField(&#036;model,'TELEFONO_3'); ?&gt;


	&lt;?php echo &#036;form-&gt;hiddenField(&#036;model,'TELEFONO_3',array('size'=&gt;30,'maxlength'=&gt;30)); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'TELEFONO_3'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;hiddenField(&#036;model,'ACCESSO_PORTAL'); ?&gt;


	&lt;?php echo &#036;form-&gt;hiddenField(&#036;model,'ACCESSO_PORTAL',array('size'=&gt;50,'maxlength'=&gt;50)); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'ACCESSO_PORTAL'); ?&gt;


&lt;/div&gt;








  &lt;div  id=&quot;clickme&quot; class=&quot;row buttons&quot; &gt;


	&lt;?php echo CHtml::submitButton(&#036;model-&gt;isNewRecord ? 'Create' : 'Save'); ?&gt;


 &lt;/div&gt;





&lt;div id=&quot;clickme1&quot; class=&quot;row buttons&quot; &gt;


	&lt;?php echo CHtml::submitButton('Save1', array('submit'=&gt;'/tirociniStage/index.php?r=studente/create'),array(&quot;id&quot;=&gt;&quot;studente&quot;)); ?&gt; 





&lt;/div&gt;	 


&lt;div  id=&quot;clickme2&quot; class=&quot;row buttons&quot; &gt;


	&lt;?php echo CHtml::submitButton('Save',array('submit' =&gt; '/tirociniStage/index.php?r=docente/create'),array(&quot;id&quot;=&gt;&quot;docente&quot;)); ?&gt;


&lt;/div&gt;	

<?php $this->endWidget(); ?>

</div><!-- form -->

When I call the form create and click on button ‘create’ … does not insert data in my db and does not go to the view form as write in controller but the form create is reload

Can you supply your controller/action code?

The class controller is:

<?php

class AnagrafeController extends Controller

{

/**


 * @var string the default layout for the views. Defaults to 'column2', meaning


 * using two-column layout. See 'protected/views/layouts/column2.php'.


 */


public &#036;layout='column2';





/**


 * @var CActiveRecord the currently loaded data model instance.


 */


private &#036;_model;





/**


 * @return array action filters


 */


public function filters()


{


	return array(


		'accessControl', // perform access control for CRUD operations


	);


}





/**


 * Specifies the access control rules.


 * This method is used by the 'accessControl' filter.


 * @return array access control rules


 */


public function accessRules()


{


	return array(


		array('allow',  // allow all users to perform 'index' and 'view' actions


			'actions'=&gt;array('index','view'),


			'users'=&gt;array('*'),


		),


		array('allow', // allow authenticated user to perform 'create' and 'update' actions


			'actions'=&gt;array('create','update'),


			'users'=&gt;array('@'),


		),


		array('allow', // allow admin user to perform 'admin' and 'delete' actions


			'actions'=&gt;array('admin','delete'),


			'users'=&gt;array('admin'),


		),


		array('deny',  // deny all users


			'users'=&gt;array('*'),


		),


	);


}





/**


 * Displays a particular model.


 */


public function actionView()


{


	&#036;this-&gt;render('view',array(


		'model'=&gt;&#036;this-&gt;loadModel(),


	));


}





/**


 * Creates a new model.


 * If creation is successful, the browser will be redirected to the 'view' page.


 */


public function actionCreate()


{


	&#036;model=new anagrafe;





	// Uncomment the following line if AJAX validation is needed


	 &#036;this-&gt;performAjaxValidation(&#036;model);





	if(isset(&#036;_POST['anagrafe']))


	{


		&#036;model-&gt;attributes=&#036;_POST['anagrafe'];


		if(&#036;model-&gt;save())


			&#036;this-&gt;redirect(array('view','id'=&gt;&#036;model-&gt;ANAGRA_ID));


	}





	&#036;this-&gt;render('create',array(


		'model'=&gt;&#036;model,


	));


}





/**


 * Updates a particular model.


 * If update is successful, the browser will be redirected to the 'view' page.


 */


public function actionUpdate()


{


	&#036;model=&#036;this-&gt;loadModel();





	// Uncomment the following line if AJAX validation is needed


	 &#036;this-&gt;performAjaxValidation(&#036;model);





	if(isset(&#036;_POST['anagrafe']))


	{


		&#036;model-&gt;attributes=&#036;_POST['anagrafe'];


		if(&#036;model-&gt;save())


			&#036;this-&gt;redirect(array('view','id'=&gt;&#036;model-&gt;ANAGRA_ID));


	}





	&#036;this-&gt;render('update',array(


		'model'=&gt;&#036;model,


	));


}





/**


 * Deletes a particular model.


 * If deletion is successful, the browser will be redirected to the 'index' page.


 */


public function actionDelete()


{


	if(Yii::app()-&gt;request-&gt;isPostRequest)


	{


		// we only allow deletion via POST request


		&#036;this-&gt;loadModel()-&gt;delete();





		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser


		if(&#33;isset(&#036;_GET['ajax']))


			&#036;this-&gt;redirect(array('index'));


	}


	else


		throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');


}





/**


 * Lists all models.


 */


public function actionIndex()


{


	&#036;dataProvider=new CActiveDataProvider('anagrafe');


	&#036;this-&gt;render('index',array(


		'dataProvider'=&gt;&#036;dataProvider,


	));


}





/**


 * Manages all models.


 */


public function actionAdmin()


{


	&#036;model=new anagrafe('search');


	if(isset(&#036;_GET['anagrafe']))


		&#036;model-&gt;attributes=&#036;_GET['anagrafe'];





	&#036;this-&gt;render('admin',array(


		'model'=&gt;&#036;model,


	));


}





/**


 * Returns the data model based on the primary key given in the GET variable.


 * If the data model is not found, an HTTP exception will be raised.


 */


public function loadModel()


{


	if(&#036;this-&gt;_model===null)


	{


		if(isset(&#036;_GET['id']))


			&#036;this-&gt;_model=anagrafe::model()-&gt;findbyPk(&#036;_GET['id']);


		if(&#036;this-&gt;_model===null)


			throw new CHttpException(404,'The requested page does not exist.');


	}


	return &#036;this-&gt;_model;


}





/**


 * Performs the AJAX validation.


 * @param CModel the model to be validated


 */


protected function performAjaxValidation(&#036;model)


{


	if(isset(&#036;_POST['ajax']) &amp;&amp; &#036;_POST['ajax']==='anagrafe-form')


	{


		echo CActiveForm::validate(&#036;model);


		Yii::app()-&gt;end();


	}


}

}

I don’t see a mistake in your controller code.

Does the controller actionCreate get to the $model->save() statement? This may return a false which causes the model not to be saved and the create form is displayed again.

Please check if that if statement is the problem. If that particular statement is the problem supply the model’s rules here.

It maybe you’re right … and what should I change? What rules?

I’m not quite sure what you need to change. Try looking at the source of the CActiveRecord::save() function to find out what statement causes it to return false, i.e. without saving.

It usually has to do with the rules of the model (as defined in the anagrafe::rules() function). Or you might have added an onBeforeValidate event handler which causes the problem.

So if you have a onBeforeValidate event handler added, list it here.

List the anagrafe::rules() function here.

My anagrafe::rules() is this…

public function rules()

{


	// NOTE: you should only define rules for those attributes that


	// will receive user inputs.


	return array(


		array('NOME, SESSO, COGNOME', 'required'),


		array('COMUNE_COMUNE_ID, COMUNE_COMUNE_ID_NASCE, COMUNE_COMUNE_ID_DOMICILIA', 'numerical', 'integerOnly'=&gt;true),


		array('NOME, COGNOME, ACCESSO_PORTAL', 'length', 'max'=&gt;50),


		array('E_MAIL, DOMICILIO', 'length', 'max'=&gt;100),


		array('INDIRIZZO', 'length', 'max'=&gt;110),


		array('NOTE, CITTADINANZA', 'length', 'max'=&gt;240),


		array('TELEFONO_3, TELEFONO_1, TELEFONO_2, STATO_CIVILE, FAX, CELLULARE', 'length', 'max'=&gt;30),


		array('CODICE_FISCALE', 'length', 'max'=&gt;16),


		array('SESSO', 'length', 'max'=&gt;1),


		array('CAP, CAP_DOM', 'length', 'max'=&gt;5),


		array('DATA_NASCITA', 'safe'),


		// The following rule is used by search().


		// Please remove those attributes that should not be searched.


		//array('NOME, ANAGRA_ID, E_MAIL, INDIRIZZO, NOTE, TELEFONO_3, CODICE_FISCALE, TELEFONO_1, SESSO, COGNOME, DATA_NASCITA, TELEFONO_2, COMUNE_COMUNE_ID, COMUNE_COMUNE_ID_NASCE, CAP, DOMICILIO, CAP_DOM, COMUNE_COMUNE_ID_DOMICILIA, ACCESSO_PORTAL, STATO_CIVILE, FAX, CELLULARE, CITTADINANZA', 'safe', 'on'=&gt;'search'),


		array('NOME, ANAGRA_ID,COGNOME','safe', 'on'=&gt;'search'),


	


		);


}

I don’t have added any onBeforeValidate event…