Input Primary Key's Field

i just wanna input data into a primary key field,

suppose i have table employee with 2 fields code and name,

employee :

field | type | width | primary key

code | varchar | 5 | *

name | varchar | 50 |

when i used crud, there wae only 1 field name, then i added code field,

but when i filled the code field, allways clear automatically when saved,

how to fix this problem?

thank’s before

(sorry for my bad english)

Did you put a validation rule for the field you added to the form or declare it as safe?

thanks jayrulez for the response,

here is my validation rules model :




public function rules()

	{

		return array(

			array('code','length','max'=>5),

			array('name','length','max'=>50),

			array('code,name', 'required'),

		);

	}



and here is my _form view:


<tr>

	<td><?php echo CHtml::activeLabelEx($model,'code'); ?></td>

	<td><?php echo CHtml::activeTextField($model,'code',array('size'=>5,'maxlength'=>5)); ?></td>

</tr>

<tr>

	<td><?php echo CHtml::activeLabelEx($model,'name'); ?></td>

	<td><?php echo CHtml::activeTextField($model,'name',array('size'=>50,'maxlength'=>50)); ?></td>

</tr>

Put this code in your view and check errors:




<?php echo CHtml::errorSummary($model); ?>



actually, here is my complete _form view:


<div class="yiiForm">


<?php echo CHtml::beginForm(); ?>


<?php echo CHtml::errorSummary($model); ?>

<table width='100%' cellspacing='2' cellpadding='2' border='0'>

<tr>

	<td><?php echo CHtml::activeLabelEx($model,'code'); ?></td>

	<td><?php echo CHtml::activeTextField($model,'code',array('size'=>5,'maxlength'=>5)); ?></td>

</tr>

<tr>

	<td><?php echo CHtml::activeLabelEx($model,'name'); ?></td>

	<td><?php echo CHtml::activeTextField($model,'name',array('size'=>50,'maxlength'=>50)); ?></td>

</tr>

<tr>

	<td colspan='2' align='right'><?php echo CHtml::submitButton($update ? 'Update' : 'Simpan'); ?></td>

</tr>

</table>

<?php echo CHtml::endForm(); ?>


</div><!-- yiiForm -->

When i save the form, code field allways be cleared automatically, here is my summary code


Silahkan betulkan kesalahan input berikut:

Code tidak boleh kosong. *



  • : code field can not be empty

so i could not save the form, any suggestions?

Try add this in model`s rules


array('code', 'safe'),

now it has error like this :


require(safe.php) [<a href='function.require'>function.require</a>]: failed to open stream: No such file or directory

It looks like you have 1.0.x version. Read this guide: http://www.yiiframew…uide/form.model especially Securing Attribute Assignments.

Always you can manualy save this field in you controller`s action:




...

$model=new Model;


...

$model->attributes=$_POST['Model'];

$model->code=$_POST['Model']['code'];


...


$model->save();

...




@qwerty :

yup, i’m using yii version 1.0.11

thanks for your answers, it works fine now.

best regards ;)