复合唯一键的有力验证 [英] Eloquent Validation for composite unique key

查看:125
本文介绍了复合唯一键的有力验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 https://github.com/illuminate/database 中使用Eloquent外部的Laravel框架。
以下是我的作曲家文件

I'm using Eloquent outside laravel framework from https://github.com/illuminate/database. The below is my composer file

{
    "require": {
        "illuminate/database": "*",
        "illuminate/validation": "*",
        "dhorrigan/capsule": "*"
    }
}

而且由于Validator :: make()中的问题。我正在使用stackoverflow中的一个建议的方法来使用它像

And because of problem in Validator::make(). I'm using one of the suggested method in stackoverflow to use it like

class Validator
{

    protected static $factory;

    public static function instance()
    {
        if (!static::$factory) {
            $translator = new Symfony\Component\Translation\Translator('en');
            static::$factory = new Illuminate\Validation\Factory($translator);
        }

        return static::$factory;
    }

    public static function __callStatic($method, $args)
    {

        $instance = static::instance();

        return call_user_func_array(array($instance, $method), $args);

    }
}

现在我可以验证所需,等等,但是我无法验证两三列的唯一键索引。我尝试了 https://github.com/felixkiss/uniquewith-validator 。但它从验证器扩展。它为我工作。而且我不确定是否有一个方法来处理复合唯一键的验证。他们的例子在文档中不清楚。

Now i can validate required, in, etc. But i'm not able to validate unique key index for two or three columns. I tried the https://github.com/felixkiss/uniquewith-validator. But its extended from Validator. It dint work for me. And i'm not sure if laravel has a way to handle validation for composite unique keys. Their examples are not clear in the documentation.

你可以提出一种解决复合唯一键验证的方法吗?

Can you suggest a way to solve the composite unique key validation ?

推荐答案

在我的情况下,某些规则(例如:unique)没有工作,因为您需要在工厂中设置一个existsValidator。我管理它,这是我的方法:

In my case some rules (e.g.: unique) didn't work since you need a presenceValidator set in your factory. I managed it and this is my approach:

创建您自己的类(MyModel)扩展Eloquent模型并添加此方法:

public static function getResolver()
    {
        return parent::$resolver;
    }

在命名空间(更容易的名称)之后的验证器类中: strong>

in your validator class after namespace (easier names):

use \Illuminate\Validation\Factory as Factory;

use \Illuminate\Validation\DatabasePresenceVerifier as DatabasePresenceVerifier;

use \Symfony\Component\Translation\Translator as Translator;

最后(我没有使用dhorrigan胶囊,但它可能会工作):

$translator = new Translator('en');
$container = $capsule->getContainer();            
$presenceVerifier = new DatabasePresenceVerifier(MyModel::getResolver());
static::$factory = new Factory($translator, $container);
static::$factory->setPresenceVerifier($presenceVerifier);

现在您可以使用基于DB的唯一验证规则和其他规则。

Now you can use unique validation rule and other rules based on DB.

希望有帮助!

这篇关于复合唯一键的有力验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆