【问题标题】:Trim function not working Yii修剪功能不起作用 Yii
【发布时间】:2014-03-13 11:01:51
【问题描述】:

我在 Yii 中有一个表格。在这里,如果我输入预先存在的值,它会给我一个错误。那工作正常,但是如果我在字段前面添加一些空格,它不会给我一个错误,也不会保存数据,而是显示数据,就像它被保存一样。当我在末尾附加空格时,它给了我一个自定义错误。请帮我解决这个问题。

我的控制器:

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

我的看法:

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

我的模特:

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

【问题讨论】:

  • 你正在修剪数据库结果?
  • 不,我正在修剪用户给定的输入
  • 如果有非utf8编码的字符,修剪通常不起作用
  • 假设数据库中存在“hello”这个词,如果我输入“hello”它应该会显示ERROR,如果我输入“hello”,这个验证已经给出了错误。
  • 那么如何纠正这个错误

标签: php yii yii-extensions yii-components


【解决方案1】:

我昨天遇到了同样的问题。你的框架是 Yii,我的是 Laravel,但我认为问题是一样的。这就是我所做的-

Are laravel query results encoded?

我已经标记了对我有用的答案

【讨论】:

    【解决方案2】:

    如果你想在不去你的模型进行规则检查的情况下提示错误消息,你应该在你的控制器中调用 trim() 函数来删除空格。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-31
      • 2013-02-01
      • 1970-01-01
      • 2013-09-13
      • 1970-01-01
      • 2020-07-31
      • 1970-01-01
      相关资源
      最近更新 更多