开发者

Checkbox on Kohana form in gallery3 software

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("I开发者_StackOverflownternet 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).


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜