在cakephp 3.0中验证字段是唯一的

时间:2015-07-11 03:21:12

标签: validation cakephp cakephp-3.0

如何在cakephp 3.0中验证字段是否是唯一的?似乎没有API中列出的验证功能。

5 个答案:

答案 0 :(得分:17)

您想使用规则validateUnique。例如,检查电子邮件地址在UsersTable上是唯一的: -

public function validationDefault(Validator $validator)
{
    $validator->add(
        'email', 
        ['unique' => [
            'rule' => 'validateUnique', 
            'provider' => 'table', 
            'message' => 'Not unique']
        ]
    );

    return $validator;
}

详情可在the API docs找到。

答案 1 :(得分:3)

你必须在桌子上使用cake的ORM规则...

在命名空间

之后将其添加到UsersTable的顶部
use Cake\ORM\Rule\IsUnique;

然后将您的规则准备好应用于您的领域,方法是将其置于公共职能部门

public function buildRules(RulesChecker $rules){
        $rules->add($rules->isUnique(['email']));
        return $rules;
    }

有关RULES

的更多信息,请参阅cakephp文档

答案 2 :(得分:1)

验证提供程序可以是对象或类名。如果使用类名,则方法必须是静态的。要使用“默认”以外的提供商,请务必在规则中设置provider键:

// Use a rule from the table provider
$validator->add('title', 'unique', [
    'rule' => 'uniqueTitle',
    'provider' => 'table'
]);

有关更多详细信息,请查看CakePHP3参考手册中的Adding Validation Providers部分。

答案 3 :(得分:0)

使用manual中描述的应用程序规则。

答案 4 :(得分:0)

请在Cakephp 3.8中进行唯一验证 go to site

@page "/cancel"
@using System.ComponentModel.DataAnnotations;

<h3>Cancel Validation</h3>

<button type="submit" class="btn btn-primary" @onclick="Login">Login</button>
<hr />
<p>Status: @status</p>

@if (showModal)
{
    <div class="modal" tabindex="-1" role="dialog" style="display:block" id="taskModal">
        <div class="modal-dialog shadow-lg bg-white rounded" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">Login</h5>
                </div>
                <div class="modal-body">
                    <EditForm Model="user" OnValidSubmit="HandleValidSubmit">
                        <DataAnnotationsValidator />
                        <ValidationSummary />
                        <div class="form-group row">
                            <label class="col-3 col-form-label">Email: </label>
                            <InputText class="col-8 form-control" @bind-Value="user.Email" />
                        </div>
                        <div class="form-group row">
                            <label class="col-3 col-form-label">Password: </label>
                            <InputText type="password" class="col-8 form-control" @bind-Value="user.Password" />
                        </div>
                        <div class="modal-footer">
                            <button type="submit" class="btn btn-primary">Submit</button>
                            <button type="button" class="btn btn-secondary" @onclick="CancelSubmit">Cancel</button>
                        </div>
                    </EditForm>
                </div>
            </div>
        </div>
    </div>
}

@code {

    public class UserLogin
    {
        [Required(ErrorMessage = "Email is required")]
        public string Email { get; set; }

        [Required(ErrorMessage = "Password is required")]
        public string Password { get; set; }
    }

    UserLogin user = new UserLogin();

    bool showModal;
    string status;

    protected override void OnInitialized()
    {
        showModal = false;
        status = "Init";
        // for demo purposes, if you delete user.Email in the login dialog, you'll need to press 'Cancel' 2 times.
        user.Email = "user@example.com";
        user.Password = "12345";
    }

    private void Login()
    {
        status = "Show Login Modal";
        showModal = true;
    }

    private void HandleValidSubmit()
    {
        status = "Valid Submit";
        showModal = false;
    }

    private void CancelSubmit()
    {
        status = "Cancelled"; 
        showModal = false;
    }
}