javascript - model not displaying validation errors -
i new yii. activerecord doesn't validate when save() called.
model codes:
public function rules() { // note: should define rules attributes // receive user inputs. return array( array('firstname, secondname, lastname, gender, phonenumber, email, address, education_background, basicsalary, departmentid, roleid, contractid, employed_by, reports_to, date_employed', 'required'), array('phonenumber, departmentid, roleid, contractid', 'numerical', 'integeronly'=>true), array('firstname, secondname, lastname', 'length', 'max'=>20), array('firstname, secondname, lastname','type', 'type'=>'char','message'=>'this field accepts characters only'), array('email','email'), ); } controller codes:
public function actioncreate() { $model=new employee; $message="email exists"; // uncomment following line if ajax validation needed // $this->performajaxvalidation($model); if(isset($_post['employee'])) { $model->attributes=$_post['employee']; $model->password=md5(strtolower($model->firstname.'123')); $to=$model->email; $mailmessage="dear ".$model->lastname." ".$model->firstname."<br> interest in working xxx company. <br>please note login details :- email:".$model->email." password: ".$model->firstname."123<br> can change password preference choice when login.<br> kindly regards<br> hrms administrator"; //echo $model->password; exit; $criteria=new cdbcriteria(); $criteria->select='email'; $criteria->condition='email=:email'; $criteria->params=array(':email'=>$model->email); if($check=employee::model()->exists($criteria)){ $this->render('create',array( 'model'=>$model,'message'=>$message)); exit; }else{ $model->save(); /*if($model->validate()){ echo 'valideted'; exit; } else { echo 'not valid'; print_r($model->geterrors()); } exit; //$model->geterrors();*/ /*if(){ echo 'saved'; } else{ print_r($model->geterrors()); } exit;*/ $this->mailsend($to,$mailmessage); // $cmd=yii::app()->db->createcommand('select max(employee.employeeid),role.rolename,employee.departmentid employee join role on employee.roleid=role.roleid'); $cmd=yii::app()->db->createcommand('select employee.employeeid,role.rolename,department.departmentname employee join department on employee.departmentid=department.departmentid join role on employee.roleid=role.roleid order employee.employeeid desc limit 1'); $rstid=$cmd->queryrow(false); $id=$rstid[0]; $role=$rstid[1]; $dep=$rstid[2]; $history=yii::app()->db->createcommand(); $history->insert('history',array( 'employeeid'=>$id, 'action_to'=>'as '.$role, 'department'=>$dep, 'reports_to'=>$model->reports_to, 'date'=>$model->date_employed, )); yii::app()->session['id']=$model->employeeid; $this->redirect(array('view','id'=>$model->employeeid)); } } $this->render('create',array( 'model'=>$model,'message'=>'' )); } view codes
<div class="form"> <?php $form=$this->beginwidget('cactiveform', array( 'id'=>'employee-form', // please note: when enable ajax validation, make sure corresponding // controller action handling ajax validation correctly. // there call performajaxvalidation() commented in generated controller code. // see class documentation of cactiveform details on this. 'enableajaxvalidation'=>false, )); ?> <p class="note">fields <span class="required">*</span> required.</p> <?php echo $form->errorsummary($model); ?> <table> <tr><td><?php echo $form->labelex($model,'firstname'); ?></td> <td><?php echo $form->textfield($model,'firstname',array('size'=>20,'maxlength'=>20)); ?></td> <td><?php echo $form->error($model,'firstname'); ?></td></tr> <tr><td><?php echo $form->labelex($model,'secondname'); ?></td> <td><?php echo $form->textfield($model,'secondname',array('size'=>20,'maxlength'=>20)); ?></td> <td><?php echo $form->error($model,'secondname'); ?></td></tr> <tr><td><?php echo $form->labelex($model,'lastname'); ?></td> <td><?php echo $form->textfield($model,'lastname',array('size'=>20,'maxlength'=>20)); ?></td> <td><?php echo $form->error($model,'lastname'); ?></td></tr> <tr><td><?php echo $form->labelex($model,'gender'); ?></td> <td><?php echo $form->dropdownlist($model,'gender',array('male'=>'male','female'=>'female')); ?></td> <td><?php echo $form->error($model,'gender'); ?></td></tr> <tr><td><?php echo $form->labelex($model,'phonenumber'); ?></td> <td><?php echo $form->textfield($model,'phonenumber'); ?></td> <td><?php echo $form->error($model,'phonenumber'); ?></td></tr> <tr><td><?php echo $form->labelex($model,'email'); ?> </td> <td><?php echo $form->textfield($model,'email',array('size'=>50,'maxlength'=>50)); ?></td> <td style='color:red;'><?php echo $form->error($model,'email'); ?> <?php echo $message; ?></td></tr> <tr><td><?php echo $form->labelex($model,'address'); ?></td> <td><?php echo $form->textarea($model,'address',array('size'=>60,'maxlength'=>100)); ?></td> <td><?php echo $form->error($model,'address'); ?></td></tr> <tr><td><?php echo $form->labelex($model,'education_background'); ?></td> <td><?php echo $form->textarea($model,'education_background',array('size'=>60,'maxlength'=>300)); ?></td> <td><?php echo $form->error($model,'education_background'); ?></td></tr> <tr><td><?php echo $form->labelex($model,'roleid'); ?></td> <td><?php echo $form->dropdownlist($model,'roleid',chtml::listdata(role::model()->findall(),'roleid','rolename'), array( 'ajax' => array( 'type'=>'post', 'datatype'=>'json', 'url'=>ccontroller::createurl('employee/getsalary'), 'success'=>'function(data){ $("#employee_basicsalary").val(data.data1); }'), 'prompt'=>'select' )); ?></td> <td><?php echo $form->error($model,'roleid'); ?></td></tr> <tr> <td><?php echo $form->labelex($model,'basicsalary'); ?></td> <td> <?php echo $form->textfield($model,'basicsalary',array('size'=>10,'maxlength'=>10)); ?> </td> <td><?php echo $form->error($model,'basicsalary'); ?></td> </tr> <tr><td><?php echo $form->labelex($model,'departmentid'); ?></td> <td><?php echo $form->dropdownlist($model,'departmentid',chtml::listdata(department::model()->findall(),'departmentid','departmentname')); ?></td> <td><?php echo $form->error($model,'departmentid'); ?></td></tr> <tr><td><?php echo $form->labelex($model,'date_employed'); ?></td> <td> <?php $this->widget('zii.widgets.jui.cjuidatepicker',array( 'name'=>'employee[date_employed]', 'id'=>'employee_date_employed', 'value'=>$model->date_employed, 'options'=>array( 'dateformat'=>'yy-mm-dd', 'showanim'=>'fold', ), 'htmloptions'=>array( 'style'=>'height:20px;' ), )); ?></td> <td><?php echo $form->error($model,'date_employed'); ?> </td></tr> <tr><td><?php echo $form->labelex($model,'contractid'); ?></td> <td> <?php echo $form->dropdownlist($model,'contractid',chtml::listdata(contract::model()->findall(),'contractid','contractname'), array( 'ajax' => array( 'type'=>'post', 'datatype'=>'json', 'url'=>ccontroller::createurl('employee/getcontdate'), 'success'=>'function(data){ $("#employee_endcontract").val(data.data1); }'), 'prompt'=>'select' )); ?></td> <td><?php echo $form->error($model,'contractid'); ?></td></tr> <tr><td><?php echo $form->labelex($model,'endcontract'); ?></td><td><?php echo $form->textfield($model,'endcontract');?></td></tr> <tr><td><?php echo $form->labelex($model,'employed_by'); ?></td> <td><?php echo $form->textfield($model,'employed_by',array('size'=>50,'maxlength'=>50)); ?></td> <td><?php echo $form->error($model,'employed_by'); ?></td></tr> <tr><td><?php echo $form->labelex($model,'reports_to'); ?></td> <td><?php echo $form->dropdownlist($model,'reports_to',array('senior manager'=>'senior manager','assistant manager'=>'assistant manager','system administrator'=>'system administrator')); ?></td> <td><?php echo $form->error($model,'reports_to'); ?></td></tr> <tr><td></td><td> <?php echo chtml::submitbutton($model->isnewrecord ? 'create' : 'save'); ?></td><td></td></tr> </table> <?php $this->endwidget(); ?> </div><!-- form --> this model not validate , doesn't display errors. please?
- you can check uniqueness of email in model (will use scenarios).
- you can create relations department , role tables in model.
model code:
public function rules() { // use scenarios return array( array('firstname, secondname, lastname, gender, phonenumber, email, address, education_background, basicsalary, departmentid, roleid, contractid, employed_by, reports_to, date_employed', 'required'), array('phonenumber, departmentid, roleid, contractid', 'numerical', 'integeronly'=>true), array('firstname, secondname, lastname', 'length', 'max'=>20), array('firstname, secondname, lastname','type', 'type'=>'char','message'=>'this field accepts characters only'), array('email','email'), array('email','unique'), ); } public function relations() { return array( 'department' => array(self::belongs_to, 'department', 'departmentid'), // department cactivrecord model of `department` table 'role' => array(self::belongs_to, 'role', 'roleid'), // role cactivrecord model of `role` table ); } controller create action code:
public function actioncreate() { $model=new employee; // uncomment following line if ajax validation needed // $this->performajaxvalidation($model); if(isset($_post['employee'])) { $model->attributes=$_post['employee']; $model->password=md5(strtolower($model->firstname.'123')); if($model->save()) { $mailmessage="dear ".$model->lastname." ".$model->firstname."<br> interest in working xxx company. <br>please note login details :- email:".$model->email." password: ".$model->firstname."123<br> can change password preference choice when login.<br> kindly regards<br> hrms administrator"; $this->mailsend($model->email, $mailmessage); yii::app()->db->createcommand()->insert('history',array( 'employeeid'=>$model->employeeid, 'action_to'=>'as '.$model->role->rolename, 'department'=>$model->department->departmentname, 'reports_to'=>$model->reports_to, 'date'=>$model->date_employed, )); yii::app()->session['id']=$model->employeeid; $this->redirect(array('view','id'=>$model->employeeid)); } } $this->render('create',array( 'model'=>$model, )); } also can set enableclientvalidation true in cactiveform widget enable js validation.
Comments
Post a Comment