gallery3 软件中 Kohana 表单上的复选框

发布于 2024-10-12 22:56:57 字数 4068 浏览 2 评论 0原文

我使用的是gallery3 php软件,它基于kohana框架。有人知道如何在专辑信息表单中添加复选框吗?

我尝试这样:

static function get_edit_form($parent) { $form = 新锻造( "albums/update/{$parent->id}", "", "post", array("id" => "g-edit-album-form")); $form->hidden("from_id")->value($parent->id); $group = $form->group("edit_item")->label(t("编辑相册"));

$group->input("title")->label(t("Title"))->value($parent->title)
    ->error_messages("required", t("You must provide a title"))
  ->error_messages("length", t("Your title is too long"));
$group->textarea("description")->label(t("Description"))->value($parent->description);
/* MPK: information fields for albums */
$group->textarea("information")->label(t("Information text"))->value($parent->information);
$group->checkbox("info")->label(t("Informational"))->value($parent->info);
if ($parent->id != 1) {
  $group->input("name")->label(t("Directory Name"))->value($parent->name)
    ->error_messages("conflict", t("There is already a movie, photo or album with this name"))
    ->error_messages("no_slashes", t("The directory name can't contain a \"/\""))
    ->error_messages("no_trailing_period", t("The directory name can't end in \".\""))
    ->error_messages("required", t("You must provide a directory name"))
    ->error_messages("length", t("Your directory name is too long"));
  $group->input("slug")->label(t("Internet Address"))->value($parent->slug)
    ->error_messages(
      "conflict", t("There is already a movie, photo or album with this internet address"))
    ->error_messages(
      "not_url_safe",
      t("The internet address should contain only letters, numbers, hyphens and underscores"))
    ->error_messages("required", t("You must provide an internet address"))
    ->error_messages("length", t("Your internet address is too long"));
} else {
  $group->hidden("name")->value($parent->name);
  $group->hidden("slug")->value($parent->slug);
}

AND

公共函数更新($album_id) { 访问::verify_csrf(); $album = ORM::factory("item", $album_id); 访问::必需(“视图”,$专辑); 访问::必需(“编辑”,$专辑);

$form = album::get_edit_form($album);
try {
  $valid = $form->validate();
  $album->title = $form->edit_item->title->value;
  $album->description = $form->edit_item->description->value;
  /* MPK: information fields for albums */
  $album->information = $form->edit_item->information->value;
  $album->info = $form->edit_item->info->value;
  $album->sort_column = $form->edit_item->sort_order->column->value;
  $album->sort_order = $form->edit_item->sort_order->direction->value;
  if (array_key_exists("name", $form->edit_item->inputs)) {
    $album->name = $form->edit_item->inputs["name"]->value;
  }
  $album->slug = $form->edit_item->slug->value;
  $album->validate();
} catch (ORM_Validation_Exception $e) {
  // Translate ORM validation errors into form error messages
  foreach ($e->validation->errors() as $key => $error) {
    $form->edit_item->inputs[$key]->add_error($error, 1);
  }
  $valid = false;
}

if ($valid) {
  $album->save();
  module::event("item_edit_form_completed", $album, $form);

  log::success("content", "Updated album", "<a href=\"albums/$album->id\">view</a>");
  message::success(t("Saved album %album_title",
                     array("album_title" => html::purify($album->title))));

  if ($form->from_id->value == $album->id) {
    // Use the new url; it might have changed.
    json::reply(array("result" => "success", "location" => $album->url()));
  } else {
    // Stay on the same page
    json::reply(array("result" => "success"));
  }
} else {
  json::reply(array("result" => "error", "html" => (string)$form));
}

该字段

确实显示在表单上,​​但字段值不会保存到数据库中。在数据库中它是一个tinyint(1)。

I am using gallery3 php software, which is based on the kohana framework. Does anybody know how to add a checkbox to the album information form?

I tried like this:

static function get_edit_form($parent) {
$form = new Forge(
"albums/update/{$parent->id}", "", "post", array("id" => "g-edit-album-form"));
$form->hidden("from_id")->value($parent->id);
$group = $form->group("edit_item")->label(t("Edit Album"));

$group->input("title")->label(t("Title"))->value($parent->title)
    ->error_messages("required", t("You must provide a title"))
  ->error_messages("length", t("Your title is too long"));
$group->textarea("description")->label(t("Description"))->value($parent->description);
/* MPK: information fields for albums */
$group->textarea("information")->label(t("Information text"))->value($parent->information);
$group->checkbox("info")->label(t("Informational"))->value($parent->info);
if ($parent->id != 1) {
  $group->input("name")->label(t("Directory Name"))->value($parent->name)
    ->error_messages("conflict", t("There is already a movie, photo or album with this name"))
    ->error_messages("no_slashes", t("The directory name can't contain a \"/\""))
    ->error_messages("no_trailing_period", t("The directory name can't end in \".\""))
    ->error_messages("required", t("You must provide a directory name"))
    ->error_messages("length", t("Your directory name is too long"));
  $group->input("slug")->label(t("Internet Address"))->value($parent->slug)
    ->error_messages(
      "conflict", t("There is already a movie, photo or album with this internet address"))
    ->error_messages(
      "not_url_safe",
      t("The internet address should contain only letters, numbers, hyphens and underscores"))
    ->error_messages("required", t("You must provide an internet address"))
    ->error_messages("length", t("Your internet address is too long"));
} else {
  $group->hidden("name")->value($parent->name);
  $group->hidden("slug")->value($parent->slug);
}

AND

public function update($album_id) {
access::verify_csrf();
$album = ORM::factory("item", $album_id);
access::required("view", $album);
access::required("edit", $album);

$form = album::get_edit_form($album);
try {
  $valid = $form->validate();
  $album->title = $form->edit_item->title->value;
  $album->description = $form->edit_item->description->value;
  /* MPK: information fields for albums */
  $album->information = $form->edit_item->information->value;
  $album->info = $form->edit_item->info->value;
  $album->sort_column = $form->edit_item->sort_order->column->value;
  $album->sort_order = $form->edit_item->sort_order->direction->value;
  if (array_key_exists("name", $form->edit_item->inputs)) {
    $album->name = $form->edit_item->inputs["name"]->value;
  }
  $album->slug = $form->edit_item->slug->value;
  $album->validate();
} catch (ORM_Validation_Exception $e) {
  // Translate ORM validation errors into form error messages
  foreach ($e->validation->errors() as $key => $error) {
    $form->edit_item->inputs[$key]->add_error($error, 1);
  }
  $valid = false;
}

if ($valid) {
  $album->save();
  module::event("item_edit_form_completed", $album, $form);

  log::success("content", "Updated album", "<a href=\"albums/$album->id\">view</a>");
  message::success(t("Saved album %album_title",
                     array("album_title" => html::purify($album->title))));

  if ($form->from_id->value == $album->id) {
    // Use the new url; it might have changed.
    json::reply(array("result" => "success", "location" => $album->url()));
  } else {
    // Stay on the same page
    json::reply(array("result" => "success"));
  }
} else {
  json::reply(array("result" => "error", "html" => (string)$form));
}

}

The field does show up on the form, but the field value does not get saved to the DB. In the DB it is a tinyint(1).

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

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

发布评论

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

评论(2

帅的被狗咬 2024-10-19 22:56:57

Kohana 使用模型将数据保存在数据库中。由于 $album->save(); 您应该在应用程序中的某个位置有一个模型,具体取决于 Kohana 的版本。

转到/modules/gallery/models。有一个名为 item.php 的文件。这是应用程序用来保存/加载/创建项目(以及相册)的模型。第 447 行有一条命令,该命令实际上将专辑的内容保存在数据库中。您需要更改该行才能保存复选框的值。

Kohana uses models to save data in the database. Because of $album->save(); you should have a model somewhere in the application, depending of the version of Kohana.

Go to /modules/gallery/models. There is a file called item.php. This is the model used by the application to save/load/create items (and also albums). At line 447 there is the command which actually saves the contents of the album in the database. You need to change that line in order to save the value of the checkbox.

给不了的爱 2024-10-19 22:56:57

解决了。问题是您必须使用复选框的“选中”字段,而不是作业中的值字段。

在album.php中
 

$group->checkbox("info")->label(t("Informational"))->value($parent->info)->checked($parent->info);

在 albums.php 中:

$album->info = $form->edit_item->info->checked;

数据库中的字段也被命名为“info”,并且可以有点。

Solved. The problem was that you have to use 'checked' field of the check-box and not the value field in the assignment.

In album.php
 

$group->checkbox("info")->label(t("Informational"))->value($parent->info)->checked($parent->info);

In albums.php:

$album->info = $form->edit_item->info->checked;

The field in the DB is also named 'info' and can be a bit.

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