Added empty line Yii textFieldRow - php

I created form with yii booster, but added one empty line before textFieldRow label.
There is my code:
<div class="form">
<?php
$form=$this->beginWidget('bootstrap.widgets.TbActiveForm', array(
'id'=>'location-form',
'type'=>'horizontal',
));?>
<fieldset>
<pre><span class="note text-info"><?php echo Yii::t('Site', 'requiredFieldsHint'); ?></span></pre>
<?php echo $form->textFieldRow($promocode,'code',
array('maxlength'=>200,'id'=>'code'));?>
<div id='username_availability_result'></div>
</fieldset>
<fieldset>
<?php echo $form->textFieldRow($promocode,'code',array('maxlength'=>200,'id'=>'code'));?>
<?php echo $form->textFieldRow($promocode,'code',array('maxlength'=>200,'id'=>'code'));?>
<?php echo $form->textFieldRow($company,'isActive',array('maxlength'=>180,'id'=>'code'));?>
<?php echo $form->textFieldRow($company,'name',array('maxlength'=>180,'id'=>'code'));?>
<?php echo $form->textFieldRow($company,'name',array('maxlength'=>180,'id'=>'code'));?>
<?php echo $form->textFieldRow($promocode,'code',array('maxlength'=>200,'id'=>'code'));?>
<?php echo $form->textFieldRow($promocode,'code',array('maxlength'=>200,'id'=>'code'));?>
</fieldset>
<div class="form-actions">
<?php $this->widget('bootstrap.widgets.TbButton', array(
'buttonType'=>'submit',
'type'=>'primary',
'label'=>Yii::t('actions', 'Submit'),
'htmlOptions' => array('style'=>'align:center;padding: 4px 58px;'),
));
?>
</div>
<?php $this->endWidget(); ?>
</div>
Result:
On firebug:

<fieldset>
<?php echo $form->textFieldRow($promocode,'code',array('maxlength'=>200,'id'=>'code'));
echo $form->textFieldRow($promocode,'code',array('maxlength'=>200,'id'=>'code'));
echo $form->textFieldRow($company,'isActive',array('maxlength'=>180,'id'=>'code'));
echo $form->textFieldRow($company,'name',array('maxlength'=>180,'id'=>'code'));
echo $form->textFieldRow($company,'name',array('maxlength'=>180,'id'=>'code'));
echo $form->textFieldRow($promocode,'code',array('maxlength'=>200,'id'=>'code'));
echo $form->textFieldRow($promocode,'code',array('maxlength'=>200,'id'=>'code')); ?>
</fieldset>
use these change to merge all php in one PHP block or for easy HOTFIX use padding in CSS to fix this issue ;)
OR merge all echo in one echo

Related

Error when trying to store the id of the multiple checkbox in database in yii

I had multiple check box to view the Hobby names from database, and it's working. But i need to store the selected id of the hobby names in the database. But when i select any hobby name it stores only 0 in my database table.
I had one Hobbies table and i created one model for that also. Please anybody help.
This is in views/sample/register.php file
<?php
/* #var $this SampleController */
/* #var $model Sample */
/* #var $form CActiveForm */
?>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'sample-register-form',
'htmlOptions' => array(
'enctype' => 'multipart/form-data',
),
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,)
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'username');
echo $form->textField($model,'username');
echo $form->error($model,'username'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'email');
echo $form->textField($model,'email');
echo $form->error($model,'email'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'password');
echo $form->passwordField($model,'password');
echo $form->error($model,'password'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'confirm password');
echo $form->passwordField($model,'password');
echo $form->error($model,'password'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'address');
echo $form->textArea($model,'address',array('rows'=>6, 'cols'=>22));
echo $form->error($model,'address'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'country');
$opts = CHtml::listData(Country::model()->findAll(),'countryid','cname');
echo $form->dropDownList($model,'country_id',$opts,
array(
'prompt'=>'Select Country',
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('Sample/Substate'),
'update'=>'#state_name',
'data'=>array('country_id'=>'js:this.value'),
)));
echo $form->error($model,'country_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'state_id');
echo CHtml::dropDownList('state_name','', array('prompt'=>'Select Country First'),
array(
'ajax'=>array(
'type'=>'POST',
'url'=>CController::createUrl('Sample/Subcity'),
'update'=>'#city_name',
'data'=>array('state_id'=>'js:this.value' ))));
echo $form->error($model,'state_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'city_id');
echo CHtml::dropDownList('city_name','', array('prompt'=>'Select State First'));
echo $form->error($model,'city_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'url');
echo CHtml::activeFileField($model, 'url'); // by this we can upload image
echo $form->error($model,'url'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'hobby');
//echo $form->checkBoxList($model,'hobby',CHtml::listData(Hobbies::model()->findAll(),'hobby_id','hobby_name'));
$data = Hobbies::model()->findAll();
foreach($data as $button)
{
//echo $button->course_name;
echo $form->checkBox($model,'hobby');
echo $button->hobby_name .'<br>';
}
echo $form->error($model,'hobby'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'gender');
echo $form->radioButtonList($model,'gender',array('Male'=>'Male','Female'=>'Female'),array('labelOptions'=>array('style'=>'display:inline'), // add this code
'separator'=>' ',
) );
echo $form->error($model,'gender'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'dob');
$form->widget('zii.widgets.jui.CJuiDatePicker', array(
'model'=>$model,
'attribute'=>'dob',
'name'=>$model->dob,
'value'=>$model->dob,
'options'=>array('dateFormat'=>'yy-mm-dd',
'altFormat'=>'yy-mm-dd',
'changeMonth'=>'true',
'changeYear'=>'true',
'yearRange'=>'1600:3000',
// which shows for both textfield and button
//'showOn'=>'both',
// 'buttonText'=>'choose date'
),
'htmlOptions'=>array('size'=>'10')
));
echo $form->error($model,'dob');
?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Register'); ?>
</div>
<?php $this->endWidget(); ?>
This is in controller/SampleController.php file
public function actionRegister()
{
$model=new Sample;
if(isset($_POST['Sample']))
{
$model->attributes=$_POST['Sample'];
if($model->validate())
{
$model->hobby = implode(",",$model->hobby);
if($model->save())
{
$this->redirect(array('site/login'));
}
return;
}
}
$this->render('register',array('model'=>$model));
}
If you view the html generated by your foreach loop you will see that all your checkboxes have the same name and id. As such only one of them will be present in the $_POST array since the name is used as the key.
To fix this change the name to an array:
echo $form->checkBox($model, 'hobby[]');
Alternatively you can use CActiveForm::checkBoxList as in your commented code and use template to style it:
template: string, specifies how each checkbox is rendered. Defaults to "{input} {label}", where "{input}" will be replaced by the generated check box input tag while "{label}" will be replaced by the corresponding check box label.
echo $form->checkBoxList(
$model,
'hobby',
CHtml::listData(Hobbies::model()->findAll(), 'hobby_id', 'hobby_name'),
array('template' => '{input}{label}<br/>')
);

Yii app how to make hidden field for date current date in create view?

I created model view and controller for advertise.
i want a date in add table as current date.
in view generated by gii it displays text input field.
how can i make it hidden so that it can get current date without taking input from user??
what should I change?
help me please. this is not a complex but i getting confused.. thanks
view/_form.php
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'add-form',
'enableAjaxValidation'=>false,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'username'); ?>
<?php echo $form->textField($model,'username',array('size'=>50,'maxlength'=>50)); ?>
<?php echo $form->error($model,'username'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'addname'); ?>
<?php echo $form->textField($model,'addname',array('size'=>60,'maxlength'=>100)); ?>
<?php echo $form->error($model,'addname'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'category'); ?>
<?php echo $form->textField($model,'category',array('size'=>60,'maxlength'=>100)); ?>
<?php echo $form->error($model,'category'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'description'); ?>
<?php echo $form->textField($model,'description',array('size'=>60,'maxlength'=>1000)); ?>
<?php echo $form->error($model,'description'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'city'); ?>
<?php echo $form->textField($model,'city',array('size'=>50,'maxlength'=>50)); ?>
<?php echo $form->error($model,'city'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'address'); ?>
<?php echo $form->textField($model,'address',array('size'=>60,'maxlength'=>500)); ?>
<?php echo $form->error($model,'address'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'mobile'); ?>
<?php echo $form->textField($model,'mobile',array('size'=>60,'maxlength'=>100)); ?>
<?php echo $form->error($model,'mobile'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'email'); ?>
<?php echo $form->textField($model,'email',array('size'=>60,'maxlength'=>100)); ?>
<?php echo $form->error($model,'email'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'image'); ?>
<?php echo $form->textField($model,'image',array('size'=>60,'maxlength'=>100)); ?>
<?php echo $form->error($model,'image'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'price'); ?>
<?php echo $form->textField($model,'price',array('size'=>60,'maxlength'=>100)); ?>
<?php echo $form->error($model,'price'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'createtime'); ?>
<?php echo $form->textField($model,'createtime'); ?>
<?php echo $form->error($model,'createtime'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
You can do that in Your controller. For example in yuor Controller` create action
public function actionCreate()
{
$model=new Product;
$model->createtime = strtotime('Now');
if(isset($_POST['Product']))
{........}
.......
}
And remove from _from.php
<div class="row">
<?php echo $form->labelEx($model,'createtime'); ?>
<?php echo $form->textField($model,'createtime'); ?>
<?php echo $form->error($model,'createtime'); ?>
</div>
Instead of
<?php echo $form->textField($model,'createtime'); ?>
Use
<?php echo $form->hiddenField($model,'createtime'); ?>
Alternatively in your model, you can set default values in afterConstruct():
protected function afterConstruct() {
parent::afterConstruct();
if($this->hasAttribute('createtime') && empty($this->createtime))
$this->createtime = new Date();
Another way to do it is set the default value in the database to CURRENT_TIMESTAMP.
Use simply:
date('Y-m-d H:i:s')
You can simply CURRENT_TIMESTAMP in your database, but unfortunately it only works for 1 row in a table
I think better put assign value on Model.
you can use beforeSave() to assign value into 'createtime' before model saved.
on # protected/models/YourModel.php add method like this :
public function beforeSave(){
if($this->isNewRecord){
$this->createtime = date('Y-m-d H:i:s');
}else{
$this->modifiedtime = date('Y-m-d H:i:s');
//just case if you have modifiedtime field
}
return parent::beforeSave();
}
after that, just removed this code from view form.
<div class="row">
<?php echo $form->labelEx($model,'createtime'); ?>
<?php echo $form->textField($model,'createtime'); ?>
<?php echo $form->error($model,'createtime'); ?>
</div>

How to use printout extension in my view page to print view page?

I have created a yii based project. I have a view where we show data from a database. I want to print that page. I searched and found the printout extension for printing and I copied the 'print' directory to /protected/extensions, but we can not understand how we could use it on my view page to print it.
My view page:
<div class="row">
<div class="row-column1">
<?php echo CHtml::label('Reg. Number. :','student_enroll_no'); ?>
<?php echo $info->student_enroll_no;?>
</div>
<div class="row-column2">
<?php echo CHtml::label('Adm. Date :','student_adm_date'); ?>
<?php if($info->student_adm_date != NULL)
echo date('d-m-Y',strtotime($info->student_adm_date));?>
</div>
</div>
<div class="row">
<div class="row-column1">
<?php echo CHtml::label('Student Name :','student_first_name'); ?>
<?php echo $info->student_first_name;?>
</div>
<div class="row-column2">
<?php echo CHtml::label('Enroll No. :','student_mobile_no'); ?>
<?php echo $info->student_mobile_no;?>
</div>
</div>
<div class="row">
<div class="row-column1">
<?php echo CHtml::label('Fathers Name :','student_last_name'); ?>
<?php echo $info->student_last_name;?>
<!--<?php echo CHtml::label('Gender :','student_gender'); ?>
<?php echo $info->student_gender;?>-->
</div>
<div class="row-column3">
<?php echo CHtml::label('Mothers Name :','student_mother_name'); ?>
<?php echo $info->student_mother_name;?>
<!-- <?php echo CHtml::label('Date of Birth :','student_dob'); ?>
<?php if($info->student_dob != NULL)
echo date('d-m-Y',strtotime($info->student_dob));?>-->
</div>
</div>
<div class="row">
<div class="row-column1">
<?php echo CHtml::label('Gender :','student_gender'); ?>
<?php echo $info->student_gender;?>
<!--<?php echo CHtml::label('Course :','student_transaction_course_id'); ?>
<?php
echo !empty($model->student_transaction_course_id) ? $model->relCourse->course_name : 'N/A';
?>-->
</div>
<div class="row-column2">
<?php echo CHtml::label('Date of Birth :','student_dob'); ?>
<?php if($info->student_dob != NULL)
echo date('d-m-Y',strtotime($info->student_dob));?>
<!--<?php echo CHtml::label('Year :','student_academic_term_period_tran_id'); ?>
<?php echo $model->Rel_student_academic_terms_period_name->academic_term_period;?>-->
</div>
</div>
<div class="row">
<div class="row-column1">
<?php echo CHtml::label('Category :','title'); ?>
<?php echo $info->title;?>
</div>
<div class="row-column2">
<?php echo CHtml::label('Email ID :','student_email_id_1'); ?> <?php echo $info->student_email_id_1; ?>
</div>
</div>
<div class="row">
<div class="row-left">
<?php echo CHtml::label('Course :','student_transaction_course_id'); ?>
<?php
echo !empty($model->student_transaction_course_id) ? $model->relCourse->course_name : 'N/A';
?>
<!-- <?php echo CHtml::label('Blood Group :','student_living_status'); ?>
<?php echo $info->student_living_status; ?>-->
</div>
<div class="row-column3">
<!-- <?php echo CHtml::label('Nationality :','student_transaction_nationality_id'); ?>
<?php if($model->student_transaction_nationality_id!=null)
echo $model->Rel_Nationality->nationality_name;
?>-->
<?php echo CHtml::label('Year :','student_academic_term_period_tran_id'); ?>
<?php echo $model->Rel_student_academic_terms_period_name->academic_term_period;?>
</div>
</div>
<div class="row">
<div class="row-left">
<?php echo CHtml::label('Subjects :','languages_known1'); ?>
<?php
$knwLang = "";
if($lang->languages_known1)
$knwLang = $lang->Rel_Langs1->languages_name;
if($lang->languages_known2)
$knwLang .= ", ".$lang->Rel_Langs2->languages_name;
if($lang->languages_known3)
$knwLang .= ", ".$lang->Rel_Langs3->languages_name;
echo $knwLang;
?>
</div>
</div>
<div class="row last">
<?php echo ('_____________________Note -Click on edit and update DoB , Subjects and upload photo ,Enroll no. .'); ?>
</div>
The documentation for this extension says it is tuned for the dataGrid widget. However it seems you are not using the dataGrid widget so what you can do is override the default printedElement property.
For example:
Wrap all the elements you wish to print in a div and give it an id of #printme and add the following to your view.
<?php
$this->widget('application.extensions.print.printWidget', array(
'cssFile' => 'print.css',
'printedElement'=>'#printme',
)
)
);
?>
NOTE: Don't forget to add/edit the print.css file.
Read the documentation for more options:
http://www.yiiframework.com/extension/printout

internal server Error when load ajax in yii

I had problem in loading Ajax , I knew that by got screanshot of request by
Tamper Data :
http://up.arabseyes.com/uploads/02_12_1213544389861.jpg
result was : internal server Error .
What is the problem in my code ?
View :
<?php
/* #var $this UsersController */
/* #var $model Users */
/* #var $form CActiveForm */
?>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'users-index-form',
'enableAjaxValidation'=>false,
)); ?>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'الأسم الأول'); ?>
<?php echo $form->textField($model,'first_name'); ?>
<?php echo $form->error($model,'الأسم الاول'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'العائلة'); ?>
<?php echo $form->textField($model,'last_name'); ?>
<?php echo $form->error($model,'العائلة'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'كلمة المرور'); ?>
<?php echo $form->passwordField($model,'password'); ?>
<?php echo $form->error($model,'password'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'تأكيد كلمة المرور'); ?>
<?php echo $form->passwordField($model,'passwordconfirm'); ?>
<?php echo $form->error($model,'passwordconfirm'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'البريد الإلكتروني '); ?>
<?php echo $form->textField($model,'email'); ?>
<?php echo $form->error($model,'email'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'تأكيد البريد الإلكتروني'); ?>
<?php echo $form->textField($model,'emailconfirm'); ?>
<?php echo $form->error($model,'emailconfirm'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'تاريخ الميلاد'); ?>
<?php
$this->widget(
'ext.jui.EJuiDateTimePicker',
array(
'model' => $model,
'attribute' => 'birth_date',
'language'=> 'en',//default Yii::app()->language
'mode' => 'date',//'datetime' or 'time' ('datetime' default)
'options' => array(
'dateFormat' => 'yy-mm-dd',
//'timeFormat' => '',//'hh:mm tt' default
),
)
);
?>
<?php echo $form->error($model,'birth_date'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'الجنس'); ?>
<?php echo chtml::activeDropDownList($model,'gender',$model->getStatusOption(),array('prompt'=>'أختر جنسك')
); ?>
<?php echo $form->error($model,'gender'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'البلد'); ?>
<?php
/*
echo chtml::activeDropDownList($model,'country',$model->getcountry(),array('prompt'=>'اختر بلدك ') ,
array(
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('current/dynamiccities'), //url to call.
//Style: CController::createUrl('currentController/methodToCall')
'update'=>'#city', //selector to update
//'data'=>'js:javascript statement'
//leave out the data key to pass all form values through
))
);
*/
echo CHtml::activedropDownList($model,'country',$model->getcountry(),
array(
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('Register/dynamiccities'), //url to call.
//Style: CController::createUrl('currentController/methodToCall')
'update'=>'#city', //selector to update
//'data'=>'js:javascript statement'
//leave out the data key to pass all form values through
))
);
?>
<?php echo $form->error($model,'country'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'المدينة'); ?>
<?php
//empty since it will be filled by the other dropdown
echo CHtml::dropDownList('city','', array());
?>
<?php echo $form->error($model,'city'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'رمز الاتصال'); ?>
<?php echo $form->textField($model,'mobile_code'); ?>
<?php echo $form->error($model,'mobile_code'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'رقم الجوال'); ?>
<?php echo $form->textField($model,'mobile_number'); ?>
<?php echo $form->error($model,'mobile_number'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('تسجيــــــــــــل'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
Controller :
....
public function actionDynamiccities() /// call Ajax
{
$data=Cities::model()->findAll('country=:country_id',
array(':country_id'=>(int) $_POST['country']));
$data=CHtml::listData($data,'id','city_name_e');
foreach($data as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
};
}
....
Thanks in Advance .

php yii send error to form on actionCreate

I have the following form which is being used to create new records
<?php
/* #var $this ComponentsController */
/* #var $model Components */
/* #var $form CActiveForm */
?>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'components-form',
'enableAjaxValidation'=>false,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'component_id'); ?>
<?php echo $form->textField($model,'component_id'); ?>
<?php echo $form->error($model,'component_id'); ?>
</div>
<div class="row">
<label class="required" for="FixedAsset_original_asset_number">
Asset Number
</label>
<input id="Components_original_asset_number" type="text" name="Components[original_asset_number]">
<?php //echo Chtml::textField('fixed_asset_id', FixedAsset::model()->FindByPk($model2->fixed_asset_id)->fixed_asset_id); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'description'); ?>
<?php echo $form->textField($model,'description',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'description'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'manufacturer'); ?>
<?php //echo $form->textField($model,'manufacturer'); ?>
<?php $manufacturer = Manufacturers::model()->findAll(array("order"=>"name"));
$list = CHtml::listData($manufacturer, 'manufacturer_id', 'name');
echo $form->dropDownList($model,'manufacturer', $list,array());
?>
<?php echo $form->error($model,'manufacturer'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'medium'); ?>
<?php //echo $form->textField($model,'medium'); ?>
<?php $medium = Medium::model()->findAll();
$list = CHtml::listData($medium, 'medium_id', 'type');
echo $form->dropDownList($model,'medium', $list,array());
?>
<?php echo $form->error($model,'medium'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'version'); ?>
<?php echo $form->textField($model,'version',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'version'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'serial_no'); ?>
<?php echo $form->textField($model,'serial_no',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'serial_no'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'purchase_date'); ?>
<?php //echo $form->textField($model,'purchase_date'); ?>
<?php
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
'id'=>'Components_purchase_date',
'name'=>'Components[purchase_date]',
//'value'=>CTimestamp::formatDate('d/m/Y',$item->validFrom),
// additional javascript options for the date picker plugin
'options'=>array(
'showAnim'=>'fold',
'dateFormat'=>'yy-mm-dd',
),
'htmlOptions'=>array(
'style'=>'height:20px;'
),
));
?>
<?php echo $form->error($model,'purchase_date'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'disposal_date'); ?>
<?php //echo $form->textField($model,'disposal_date'); ?>
<?php
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
'id'=>'Components_disposal_date',
'name'=>'Components[disposal_date]',
//'value'=>CTimestamp::formatDate('d/m/Y',$item->validFrom),
// additional javascript options for the date picker plugin
'options'=>array(
'showAnim'=>'fold',
'dateFormat'=>'yy-mm-dd',
),
'htmlOptions'=>array(
'style'=>'height:20px;'
),
));
?>
<?php echo $form->error($model,'disposal_date'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'model'); ?>
<?php echo $form->textField($model,'model',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'model'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'nol'); ?>
<?php echo $form->textField($model,'nol'); ?>
<?php echo $form->error($model,'nol'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
I want to send an error to the field Components_original_asset_number on the form. How would I achieve this. In the controller function I am checking to see if it exists if not I want to display error on form or message. original asset number is part of a separate model which I am displaying on this form.
public function actionCreate()
{
$model = new Components;
$model_fixedAsset = new FixedAsset;
$model_comAsset = new ComAsset;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Components']))
{
$model->attributes=$_POST['Components'];
$fixedAssetId = null;
// Check if asset exist and get PK
if( $_POST['Components']['original_asset_number'] != "" ){
//print_r($_POST['Components']);
$criteria = new CDbCriteria;
$criteria->condition = "(original_asset_number = :original_asset_number)";
$criteria->params = array(":original_asset_number" => $_POST['Components']['original_asset_number'] );
$fixedAssetRow = FixedAsset::model()->find($criteria);
//print_r($fixedAssetRow);
if($fixedAssetRow){
$fixedAssetId = $fixedAssetRow->fixed_asset_id;
}
//echo $fixedAssetId;
}
if($fixedAssetId){
/*if($model->save())
$this->redirect(array('view','id'=>$model->component_id));*/
// Create com_asset record
}else{
//no asset found return error message to input corect asset number or create asset in navision and run php script to update mysql db
}
}
$this->render('create',array(
'model'=>$model,
'model_fixedAsset'=>$model_fixedAsset,
'model_comAsset'=>$model_comAsset,
));
}
You should add the 'original_asset_number' in your model as variable and set required validation rule. Then you can use the following line to set error
$model->addError('original_asset_number', " no asset found return error message to input corect asset number or create asset in navision and run php script to update mysql ");
Also, you use the following way to display the field and error.
<?php echo $form->errorSummary($model); ?>
or
<div class="row">
<?php echo $form->labelEx($model,'original_asset_number'); ?>
<?php echo $form->textField($model,'original_asset_number',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'original_asset_number'); ?>
</div>

Categories