1

I have a form in Yii. Here if I enter preexist values, it gives me an error. That is working fine, But if I add some spaces in front of the field, it does not gives me an error, nor save the data, but display the data as if it is saved. when I append spaces at the end, it gives me an custom error. Please help me solving this.

My controller:

public function actionUpdate($id)
    {
        $model=$this->loadModel($id);
                $user = Yii::app()->db->createCommand()
    ->select('cust_name')
    ->from('mst_customers')
    ->where('cust_id='.$model->host_customer_id)                    
    ->queryAll();
                //print_r($user);
                // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        if(isset($_POST['NimsoftHost']))
        {
                      $model->attributes=$_POST['NimsoftHost'];
                      echo $model->host_name;
        $criteria = new CDbCriteria;
                $criteria->condition = "host_name = '$model->host_name'";
                $exist=NimsoftHost::model()->findAll($criteria);
                if($exist)
                {
                    $model->addError(null, "Duplicate values entered");
                }
                else if($model->save())
                        {
                             $this->redirect(array('view','id'=>$model->host_id,'name'=>$user));
                         }
                }

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

My view:

<div id="content">
<div class="innerLR">
<div class="row-fluid">
<div class="form">

<?php
echo $id = $id;
$form = $this->beginWidget ( 'CActiveForm', array (
        'id' => 'nimsoft-host-form',
        // Please note: When you enable ajax validation, make sure the corresponding
        // controller action is handling ajax validation correctly.
        // There is a call to performAjaxValidation() commented in generated controller code.
        // See class documentation of CActiveForm for details on this.
        'enableAjaxValidation' => false 
) );
?>

    <fieldset>
    <legend>Customer Host Information:</legend>
        <?php echo $form->errorSummary($model); ?>

            <div id="add_details">
        <tr>
        <td style="text-align: left;" class="tdSpan">
                    <?php echo $form->labelEx($model, 'host_name'); ?>
                </td>
            <td class="tdSpan">
                <div class="row">
                                    <?php echo $form->textField($model, 'host_name', array('size' => 60, 'maxlength' => 88)); ?>
                                </div>
            </td>
        </tr>
                <tr>
            <td style="text-align: left;" class="tdSpan">
                        <?php echo $form->labelEx($model, 'host_serviceid'); ?>
                        </td>
            <td class="tdSpan">
                <div class="row">
                                    <?php echo $form->textField($model, 'host_serviceid', array('rows' => 6, 'cols' => 50)); ?>
                                </div>
                        </td>
        </tr>

        <tr class="tdSpan">
            <td></td>
            <td>
                            <div class="row">

                                <?php $model->isNewRecord ?$model->status = 'Enable': $model->status = $model->status;?>
                                <?php echo $form->labelEx($model,'status'); ?>
                                <?php echo $form->radioButtonList($model, 'status', array('Enable'=>'Enable', 'Disable'=>'Disable'),array(
    'labelOptions'=>array('style'=>'display:inline'),
    'separator'=>'')); ?>
                                <?php echo $form->error($model,'status'); ?>
                            </div>

            </div> 
                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <div class="row buttons">
                                    <div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div>
                                    <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save', array('onclick' => 'return checkForm();')); ?>
                                </div>
                        </td>
                    </tr>
                                        </tbody>
                    </table>
    <?php $this->endWidget(); ?>
    </fieldset>
</div>
</div>
</div>
</div>

<div id="footer" class="hidden-print">
    <?php $this->renderPartial('application.views.layouts._footer_inc'); ?>
</div>

My model:

public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
                        array('host_name, host_customer_id, status', 'required'),
                        array('host_name, host_serviceid, host_customer_id', 'length', 'max'=>255),
                        array('host_serviceid','numerical'),
                        array('host_name', 'unique','message'=>'HOST NAME already exists!'),
            array('host_name', 'filter', 'filter'=>'trim'),
                        // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('host_id, host_name, host_serviceid, host_customer_id, status,host_search', 'safe', 'on'=>'search'),
        );
    }
user3318138
  • 119
  • 1
  • 2
  • 18

2 Answers2

0

I faced the same problem yesterday. Your framework is Yii, mine was Laravel but the problem is same I think. This is what I did -

Are laravel query results encoded?

I have marked the answer that worked for me

Community
  • 1
  • 1
halkujabra
  • 2,844
  • 3
  • 25
  • 35
0

You should call the trim() function in your controller to remove whitespaces if you want to prompt an error message without going to your model for rule checking.

    if(isset($_POST['NimsoftHost']))
    {
      $model->attributes=$_POST['NimsoftHost'];
      $hostName = trim($model->host_name);

      $exist=NimsoftHost::model()->findByAttributes(array('host_name'=>$hostName));
            if($exist)
            {
                $model->addError(null, "Duplicate values entered");
            }
            else if($model->save())
            {
                $this->redirect(array('view','id'=>$model->host_id,'name'=>$user));
            }
     }
Eric Santos
  • 193
  • 1
  • 1
  • 11