drfrvbq736383 2013-12-19 10:54
浏览 66
已采纳

在yii中创建记录时模型验证不起作用

I really don't know why is it happening my model validations are not working while creating record in yii. doesn't display any errors . The thing is if any of the required field is empty though it passes to the display page not displaying errors but it doesn't insert the record as all required field a not filled.

My need is display errors in the same form i.e., validations should not pass if required fields are empty.

validation works with no issues in update, issues with create form

but it inserts the record if all required field are filled.

errors displayed in update are black not red as default by yii ...... is it due to the extension am using

model rules

        array('name, category, model, brand, description, price', 'required'),
        array('pimg', 'file','types'=>'jpg','on'=>'create'),
        array('pimg', 'file','types'=>'jpg','on'=>'update', 'allowEmpty'=>true),

controller for create

     $model=new controllername;

    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);

    if(isset($_POST['controllername']))
    {
        $model->attributes=$_POST['controllername'];
        $model->pimg=CUploadedFile::getInstance($model,'pimg');
        $fileName = $model->pimg;


        if($model->save())
            $model->pimg->saveAs('images/'.$fileName);
            $this->redirect(array('display','id'=>$model->productid));

    }

    $this->render('create',array(
      'model'=>$model,
    ));

view

  <?php $form=$this->beginWidget('CActiveForm',array(
   'id'=>'form_name',
     'enableAjaxValidation'=>false,
    'htmlOptions'=>array('enctype'=>'multipart/form-data'),
      )); ?>

        <p class="note">Fields with <span class="required">*</span> are required.</p>





    <?php echo $form->labelEx($model,'name'); ?>
    <?php echo $form->textField($model,'name',array('size'=>60,'maxlength'=>60)); ?>
    <?php echo $form->error($model,'name'); ?>

    <?php echo $form->labelEx($model,'model'); ?>
    <?php echo $form->textField($model,'model',array('size'=>30,'maxlength'=>30)); ?>
    <?php echo $form->error($model,'model'); ?>


  <?php echo $form->labelEx($model,'description'); ?>
    <?php echo $form->textField($model,'description',array('size'=>60,'maxlength'=>256)); ?>
    <?php echo $form->error($model,'description'); ?>



    <?php echo $form->labelEx($model,'pimg'); ?>
    <?php echo $form->hiddenField($model,'pimg',array('length'=>222)); ?>
    <?php echo $form->fileField($model, 'pimg',array('id'=>'imgInput',)); ?>

    <?php echo $form->error($model,'pimg'); ?>



    <?php echo $form->labelEx($model,'category'); ?>
    <?php echo $form->dropDownList($model,'category',$model->getCat()); ?>
    <?php echo $form->error($model,'category'); ?>


<?php echo $form->labelEx($model,'brand'); ?>
    <?php echo $form->textField($model,'brand',array('size'=>30,'maxlength'=>30)); ?>
    <?php echo $form->error($model,'brand'); ?>

    <?php echo $form->labelEx($model,'price'); ?>
    <?php echo $form->textField($model,'price'); ?>
    <?php echo $form->error($model,'price'); ?>






 <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>


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

can someone PLEASE tell me how can i achieve this . Thank you

  • 写回答

2条回答 默认 最新

  • douzhoulei8959 2013-12-20 10:40
    关注

    if you redirect a page, the error will not be shown,

    your code redirects anyway, if (save()) or not .

    add a {} after your if

    if($model->save())
    {
                $model->pimg->saveAs('images/'.$fileName);
                $this->redirect(array('display','id'=>$model->productid));
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 如何将下列的“无限压缩存储器”设计出来
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口