【问题标题】:How to Use unique rules in active record yii2如何在活动记录 yii2 中使用唯一规则
【发布时间】:2015-10-12 08:06:18
【问题描述】:

我想将我的表列的值设置为唯一值,如果在插入表单中,我如何使用设置错误,我在我的数据库中插入与数据相同的值?

是真的吗?

    public function rules()
{
    return [
        [['nama_barang', 'harga', 'stok', 'id_satuan'], 'required'],
        [['harga', 'stok', 'id_satuan'], 'integer'],
        ['nama_barang', 'unique', 'targetAttribute' => ['nama_barang' => 'nama_barang']],
        [['foto'], 'safe']
    ];
}

【问题讨论】:

  • 您是否尝试插入相同的值?
  • 是的,我尝试插入相同的值,如果我插入相同的值,我想显示错误。我该怎么做?
  • 您是否想让唯一的验证器纯粹在前端工作?因为只有后端才能进行实际的数据库查找。这意味着这个验证器仅用于 ajax 和后端验证,而不用于前端验证。另外,看看skipOnError 属性,默认情况下只显示第一个错误。
  • 所以,它只是在后端?它不能在前端使用?

标签: validation yii2 unique


【解决方案1】:

记住:模型、视图、控制器。

型号 在您的模型规则中添加唯一验证器,例如

...
 [['nama_barang'], 'unique'],
...

查看

在表单视图中启用 ajax 验证

...
<?php $form = ActiveForm::begin(['enableAjaxValidation' => true]); ?>
...

控制器

在控制器中添加 ajax 验证 创建动作

...
    public function actionCreate()
    {
        $model = new Product();
        if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
            Yii::$app->response->format = Response::FORMAT_JSON;
            return ActiveForm::validate($model);
        }
        if ($model->load(Yii::$app->request->post())) {
...

和更新操作

...
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);
        if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
            Yii::$app->response->format = Response::FORMAT_JSON;
            return ActiveForm::validate($model);
        }
        if ($model->load(Yii::$app->request->post())) {
...

PS:如果不存在,请在控制器中添加所需的类。

use yii\web\Response;
use yii\widgets\ActiveForm;

【讨论】:

  • 这对我有用,但我有一个问题。我还有另一列名为evidence,我将其设置为required。在我遵循您的建议后,尽管我输入了值,但表单中的证据字段始终标记为空显示错误。谢谢
【解决方案2】:

试试这个方法

public function rules()
{
return [
    [['nama_barang', 'harga', 'stok', 'id_satuan'], 'required'],
    [['harga', 'stok', 'id_satuan'], 'integer'],
    ['nama_barang', 'unique', 'targetAttribute' => ['nama_barang'], 'message' => 'Username must be unique.'],
    [['foto'], 'safe']
  ];
}

【讨论】:

  • 感谢隐身,但表单错误未显示在表单中。如何显示表单错误?
【解决方案3】:

只需在规则中设置唯一[['name'], 'unique'],

下面是完整的功能。

public function rules()
    {
        return [
            [['name', 'description', 'comp_id'], 'required'],
            [['description'], 'string'],
            [['comp_id'], 'integer'],
            [['name'], 'string', 'max' => 100,],
            [['name'], 'unique'],
            [['comp_id'], 'exist', 'skipOnError' => true, 'targetClass' => Company::className(), 'targetAttribute' => ['comp_id' => 'comp_id']],
        ];
    }

【讨论】:

    【解决方案4】:

    我遇到了类似的问题,当我插入具有现有唯一字段的记录时,框架保持沉默,返回我的视图而没有任何错误。 因此,解决此问题的技巧是仅当 $model->save() 的布尔值为 true 时才进行成功重定向,否则通过 view.php 渲染回 _form.php

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-24
      • 2016-11-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多