Laravel-admin: -表單驗證- unique

Created on 21 Nov 2016  ·  10Comments  ·  Source: z-song/laravel-admin

參考https://laravel.tw/docs/5.3/validation#rule-unique
該如何使用 unique的驗證

$form->text('username', trans('admin::lang.username'))->rules("required|unique:admin_users,username");

目前使用unique,在資料新增後,再"修改"的話,會出現資料已經存在資料庫。
因為找不到教學,目前暫時解法

$form->text('username', trans('admin::lang.username'))->rules("required|unique:admin_users,username,@key_id");

我暫時去修改Form.php,把@key_id,str_replace為目前的form id。
請問有正確的做法嗎?

Most helpful comment

Ideally, $id should be passed along to the formmethod when possible:

So overwrite the update function from the ModelForm trait in your controller (and the destroy if necessary)
This should be added in the EncoreAdminControllersModelForm if possible

    public function update($id)
    {
        return $this->form($id)->update($id);
    }
public function edit($id)
    {
        return Admin::content(
            function (Content $content) use ($id) {

                $content->header($this->headerText);
                $content->description('edit');

                $content->body($this->form($id)->edit($id));
            }
        );
    }
 protected function form($id = null)
    {
        return Admin::form(
            Administrator::class,
            function (Form $form) use ($id){

                $form->text('username')->rules('required|unique:admin_users,username,'.$id);

            }
        );
    }

All 10 comments

@yang5664 没有比较优雅的办法

不过我想到一个不用修改Form.php的方法

$method = Request::method();

$rules = ($method === 'PUT') ? 'required' : 'required|unique:demo_users,name';

你可以试试看

Ideally, $id should be passed along to the formmethod when possible:

So overwrite the update function from the ModelForm trait in your controller (and the destroy if necessary)
This should be added in the EncoreAdminControllersModelForm if possible

    public function update($id)
    {
        return $this->form($id)->update($id);
    }
public function edit($id)
    {
        return Admin::content(
            function (Content $content) use ($id) {

                $content->header($this->headerText);
                $content->description('edit');

                $content->body($this->form($id)->edit($id));
            }
        );
    }
 protected function form($id = null)
    {
        return Admin::form(
            Administrator::class,
            function (Form $form) use ($id){

                $form->text('username')->rules('required|unique:admin_users,username,'.$id);

            }
        );
    }

$form->text('username')->rules('required|unique:admin_users,username,'.$id);
这样做根本并不起作用
提交表单之前$id=1,在提交表单之后获取不到$id的值,所以会一直验证unique

$form->text('username')->rules('required|unique:admin_users,username,'.$id);
是的 这样不起作用
原因 ModelForm
public function update($id) { return $this->form()->update($id); }
中没有带id
还需要在controller 中覆盖update方法
` public function update($id)
{
return $this->form($id)->update($id);
}
protected function form($id = false){
//.....
}

`
这样就可以的哈
@z-song 希望大神能更新下 ModelForm trait。你上面给出的解决方案不能解决修改的时候,唯一字段更新为其他已存在的记录的唯一字段(如防止更新绑定的电话号码 到其他人已注册的电话号码这种情况)

可以試試看這樣

"encore/laravel-admin": "1.5.*",
"laravel/framework": "5.5.*",
$form->text('account', 'Account')->rules(function ($form) {
        return 'required|unique:users,account,'.$form->model()->id.',id';
});

@yang5664 是正解 赞赞哒

$form->text('account', 'Account')->rules(function ($form) {
return 'required|unique:users,account,'.$form->model()->id.',id';
});
Thanks @yang5664

可以試試看這樣

"encore/laravel-admin": "1.5.*",
"laravel/framework": "5.5.*",
$form->text('account', 'Account')->rules(function ($form) {
        return 'required|unique:users,account,'.$form->model()->id.',id';
});

this works good for me. thx

->rules(function ($form){
return Rule::unique('users','email_address')->where($role,'admin');
})

可以試試看這樣

"encore/laravel-admin": "1.5.*",
"laravel/framework": "5.5.*",
$form->text('account', 'Account')->rules(function ($form) {
        return 'required|unique:users,account,'.$form->model()->id.',id';
});

建議作法:
https://medium.com/@yang5664/laravel-admin-%E6%8A%80%E5%B7%A7-unique%E9%A9%97%E8%AD%89-dac3e8d7dcde

Was this page helpful?
0 / 5 - 0 ratings