1

新しいレコードを作成するために使用されている次のフォームがあります

<?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 -->

Components_original_asset_numberフォームのフィールドにエラーを送信したい。どうすればこれを達成できますか。コントローラー関数では、フォームまたはメッセージにエラーを表示したい場合、存在するかどうかを確認しています。original asset numberこのフォームに表示している別のモデルの一部です。

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,
    ));
}
4

1 に答える 1

9

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>
于 2012-10-25T03:30:19.727 に答える