当我更新记录时,Codeigniter 4 IS_UNICER规则验证不是唯一错误

发布于 2025-02-13 02:22:41 字数 746 浏览 2 评论 0原文

我有一个简单的表单,可以在数据库中存储一些数据,对于 title 字段,我有一个 is_nique 验证规则以及其他验证规则。以下是我从 taskmodel 的验证规则:

protected $validationRules = [
        'Title' => 'required|min_length[5]|max_length[15]|is_unique[tasks.Title]',
        'Description' => 'required|max_length[300]',
        'CreatedAt' => 'required',
        'UpdatedAt' => 'required',
        'DueDate' => 'required|ValidateDueDate[DueDate]',
        'AssignedTo' => 'required',
        'Author' => 'required'
    ];

现在,在将数据添加到数据库时,所有内容都按预期工作。 问题是当我尝试更新唱片时,可以说我更改了作者名称,当我提交表格时,标题应该是唯一的。我希望它忽略我正在编辑的数据库中的记录行,并与其他人一起检查输入的唯一性。您能帮我实现这一目标吗?我正在考虑使用表格传递记录ID并在检查唯一性时忽略它,但我不知道如何将ID传递给验证规则。

I have a simple form that stores some data in the database and for the Title field I have an is_unique validation rule along with others. Following are my validation rules from the TaskModel:

protected $validationRules = [
        'Title' => 'required|min_length[5]|max_length[15]|is_unique[tasks.Title]',
        'Description' => 'required|max_length[300]',
        'CreatedAt' => 'required',
        'UpdatedAt' => 'required',
        'DueDate' => 'required|ValidateDueDate[DueDate]',
        'AssignedTo' => 'required',
        'Author' => 'required'
    ];

Now everything is working as intended when adding data to the Database. The Problem is when I try to update a record let's say I change the author name and when I submit the form it says the title should be unique. I want it to ignore the record row from the database I am editing and check input's uniqueness with others. Can you help me achieve this? I am thinking of passing the record id with the form and ignoring it when checking uniqueness but I don't know how to pass the Id to the validation rule.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酸甜透明夹心 2025-02-20 02:22:41

您可以在 is_unique 规则中将行的ID作为参数传递。就像

is_unique[tasks.Title,Id,{Id}]

希望一样,:)

更新:更详细的说明

第二个参数 id 是数据库字段名称。第三个是从表单传递的 id 。为此,在编辑表单中添加一个隐藏的字段,然后设置其name = id及其value = $ data ['id']。其中$ data ['id']是从数据库获取并传递到视图的行的 id 。因此,当将提交表格并将 id 提交时,将在$ _ post中提交。然后将其传递给规则参数为:
{id}

希望这会有所帮助:(

You can pass the Id of row as parameter in the is_unique rule. Like

is_unique[tasks.Title,Id,{Id}]

Hope this helps :)

UPDATE: More Detailed Explanation

Second parameter Id is database field name. Third one is the Id that is passed from the form. For this purpose add a hidden field in the edit form then set its name = Id and its value=$data['Id']. Where $data['Id'] is the Id of the row fetched from the Database and passed to the view. So when the form will be submitted and Id will be submitted in $_POST. Which is then passed to rule parameter as:
{Id}

HOPE THIS HELPS :(

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文