当我更新记录时,Codeigniter 4 IS_UNICER规则验证不是唯一错误
我有一个简单的表单,可以在数据库中存储一些数据,对于 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在 is_unique 规则中将行的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
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 itsvalue=$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 :(