/** * Automatic creation of input forms. * @return view data */ public function action_fieldset() { $output = ""; //the generated output will be collected here $eventinstance = new Model_Orm_Event(); //a new form which is identified as "eventform" //in current view $inputform = Fuel\Core\Fieldset::forge("eventform"); //the fieldset class loads field information from our model $inputform->add_model($eventinstance); //since the location field should be a drop-down select box, //we add it manually. //The actual list of locations is loaded by the Model_Orm_Location model $inputform->add("location", "Select a location", array("type" => "select", "name" => "location_id", "options" => Model_Orm_Location::get_locations())); //the Fieldset does not automatically add buttons, hence we do $inputform->add("submit", "", array("type" => "submit", "value" => "Add", "class" => "btn medium primary")); //this re-loads data from $_POST array if the form is re-submitted. $inputform->repopulate(); //execute validations described in the model class if ($inputform->validation()->run()) { //this is the good case! $new_event = Model_Orm_Event::forge(); $fields = $inputform->validated(); $new_event->title = $fields["title"]; $new_event->description = $fields["description"]; $new_event->start = time(); $new_event->save(); //always do a redirect after saving an item! Response::redirect("event/orm"); } else { //there have been some problems, output them to the client foreach ($inputform->validation()->error() as $error) { $output .= "<p>" . $error . "</p>"; } } //the 'build' actually generates all HTML code $output .= $inputform->build(); return Response::forge($output); }
public function action_edit($id = null) { //looks up the even in the database //if anything is not OK - redirects back to the list of events is_null($id) and Response::redirect('event'); $event = Model_Orm_Event::find($id, array("related" => array("location"))); is_null($event) and Response::redirect('Event'); //not POST = just read from database if (Input::method() == 'POST') { $val = Model_Orm_Event::validate("edit"); if ($val->run()) { //validation is OK! $event->title = $val->validated("title"); $event->description = $val->validated("description"); $event->start = $val->validated("start"); $event->location = Model_Orm_Location::find(Input::post("location")); if ($event->save()) { Session::set_flash("success", "Changes saved successfuly!"); } else { Session::set_flash("error", "Somehow could not save the item."); } Response::redirect("event/view/" . $event->id); } else { //POST data passed, but something wrong with validation Session::set_flash("error", $val->error()); } } $data["event"] = $event; $data["locations"] = Model_Orm_Location::get_locations(); $this->add_rich_form_scripts(); $this->template->title = "Editing the event " . $event->title; $this->template->page_content = View::forge("event/edit", $data); }