我在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>
<div class="row buttons">
<div> </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'),
);
}
答案 0 :(得分:0)
昨天我遇到了同样的问题。你的框架是Yii,我的是Laravel,但我认为问题是一样的。这就是我做的 -
Are laravel query results encoded?
我已经标出了对我有用的答案
答案 1 :(得分:0)
如果要在不进行规则检查的模型的情况下提示错误消息,则应调用控制器中的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));
}
}