1

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

4
  • what is your view code? its the same as update view? can you write it here ? Dec 19, 2013 at 11:01
  • 3
    your view and controller code is Important to us to answer your question. IF you want answer Dec 19, 2013 at 11:16
  • sorry for the delay ya your are absolutely correct my update and create view is the same
    – Tested
    Dec 20, 2013 at 4:04
  • what have i done is in the controller added if($model->validate()){//save to db} which now displays the error but without the above condition it doesnt
    – Tested
    Dec 20, 2013 at 4:06

2 Answers 2

1

try with

array('name, category, model, brand, description, price', 'required'),
array('pimg', 'file','types'=>'jpg','on'=>'insert', 'allowEmpty'=>false),
array('pimg', 'file','types'=>'jpg','on'=>'update', 'allowEmpty'=>true),
1
  • ohh.. i faced same problem but when i replace 'on'=>'create' with 'on'=>'insert' it works for me. Try with array('name, category, model, brand, description, price', 'required'), array('pimg', 'file','types'=>'jpg','on'=>'insert', 'allowEmpty'=>false), don't write validation for update
    – M Gaidhane
    Dec 20, 2013 at 6:00
0

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));
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.