codeigniter 打印表单特定的验证错误

发布于 2025-01-02 01:55:47 字数 254 浏览 0 评论 0原文

我在一个页面上有 2 个表单,例如:“create_user”和“update_user”,我想在表单上方显示特定于每个表单的验证错误。我的意思是,我想在其上方显示“create_user”表单的验证错误,以及“update_user”的验证错误。

现在,如果我提交更新,两个表单上方都会显示表单错误。 有没有类似的东西

<? echo validation_errors("create_user");   ?>

I have 2 forms on a page eg: 'create_user' and 'update_user' i want to show validation errors specific to each form just above the form. i mean, i want to show validation errors for 'create_user' form just above it and same for 'update_user'.

Right now if i submit update, form errors are showing above both of the forms.
is there something like

<? echo validation_errors("create_user");   ?>

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

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

发布评论

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

评论(2

江挽川 2025-01-09 01:55:47

您可以执行以下操作:


//pass validation_errors as data to your view from your controller, like
//in your controller
if ($this->form_validation->run()) {  //form create_user form
  //process
}
else {
   $data['create_user_form_errors'] = validation_errors();
}
//and for second form update_user
if ($this->form_validation->run()) {  //form create_user form
  //process
}
else {
   $data['update_user_form_errors'] = validation_errors();
}

//then in your view
if (isset($create_user_form_errors)) echo $create_user_form_errors; //show above first form
//and
if (isset($update_user_form_errors)) echo $update_user_form_errors; //show above second form

类似的操作应该适用于显示基于多种表单的错误。希望有帮助

You could do something like:


//pass validation_errors as data to your view from your controller, like
//in your controller
if ($this->form_validation->run()) {  //form create_user form
  //process
}
else {
   $data['create_user_form_errors'] = validation_errors();
}
//and for second form update_user
if ($this->form_validation->run()) {  //form create_user form
  //process
}
else {
   $data['update_user_form_errors'] = validation_errors();
}

//then in your view
if (isset($create_user_form_errors)) echo $create_user_form_errors; //show above first form
//and
if (isset($update_user_form_errors)) echo $update_user_form_errors; //show above second form

Something like that should work for showing errors based on multiple forms. Hope it helps

一刻暧昧 2025-01-09 01:55:47

最简单的方法是这样的:

  1. 向每个表单传递某种表单识别隐藏字段,
  2. 将相同的隐藏字段传递给视图错误状态,并使用基本 IF 条件在正确的位置运行 validation_errors()

您可能可以以某种方式扩展验证类来执行您所要求的操作,但这将是一个非常具体的实现,并且在框架中几乎没有用(这就是为什么 CI 默认情况下“不知道如何”执行此操作)。使用扩展类所增加的复杂性可能会抵消不简单地在视图中使用基本逻辑的任何好处。

Simplest way is this:

  1. Pass some sort of form-identifying hidden field with each form
  2. Pass that same hidden field to the view error state, and use a basic IF condition to run validation_errors() in the correct location

You could probably extend the validation class somehow to do what you're asking, but it would be a very specific implementation, and hardly useful in a framework (which is why CI doesn't "know how" to do this by default). The added complexity of working with the extended class would likely counteract any benefit of not simply using basic logic in the view.

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