/** * Creates a NewNoteForm. * @param int|NULL $pad Id of the default pad. Can be NULL. * @return Form */ public function create($pad) { $form = new Form(); $form->addText('name', 'Name')->setRequired('%label is required'); $form->addTextArea('text', 'Text')->setRequired('%label is required'); $form->addSelect('pad', 'Pad', $this->padManager->findAll()->fetchPairs('id', 'name'))->setPrompt('Select pad')->setDefaultValue($pad); $form->addSubmit('submit', 'Save'); $form->onSuccess[] = [$this, 'formSucceeded']; return $form; }
/** * Creates an EditPadForm. * @param int $id Id of the pad to be edited. * @param string $name Current name of the pad. * @return Form */ public function create($id, $name) { $form = new Form(); $form->addText('name', 'Name')->setDefaultValue($name)->setRequired('%label is required'); $form->addSubmit('submit', 'Save'); $form->onSuccess[] = function (Form $form, $values) use($id) { if (!$this->padManager->update($id, $values->name)) { $form->addError("Failed to edit pad"); } }; return $form; }
/** * Creates a DeletePadForm. * @param int $id Id of the pad to be deleted. * @return Form */ public function create($id) { $form = new Form(); $form->addProtection(); // Adds CSRF protection $form->addSubmit('submit', 'Yes, I want to delete this pad'); $form->onSuccess[] = function (Form $form) use($id) { if (!$this->padManager->delete($id)) { $form->addError("Failed to delete pad"); } }; return $form; }
/** * Creates an EditNoteForm. * @param int $id * @param string $name * @param string $text * @param int $pad * @return Form */ public function create($id, $name, $text, $pad) { $form = new Form(); $form->addText('name', 'Name')->setDefaultValue($name)->setRequired('%label is required'); $form->addTextArea('text', 'Text')->setDefaultValue($text)->setRequired('%label is required'); $form->addSelect('pad', 'Pad', $this->padManager->findAll()->fetchPairs('id', 'name'))->setPrompt('Select pad')->setDefaultValue($pad); $form->addSubmit('submit', 'Save'); $form->onSuccess[] = function (Form $form, $values) use($id) { if (!$this->noteManager->update($id, $values->name, $values->text, $values->pad)) { $form->addError("Failed to edit pad"); } }; return $form; }
/** * Loads pad with given id and if not found, throws BadRequestException. * @param int $id * @throws BadRequestException */ private function loadPad($id) { $this->id = $id; $this->pad = $this->padManager->find($this->id); if (!$this->pad) { throw new BadRequestException("Pad with given id not found"); } }
/** * Callback for NewPadForm onSuccess event. * @param Form $form * @param ArrayHash $values */ public function formSucceeded(Form $form, $values) { if (!$this->padManager->add($values->name)) { $form->addError("Failed to create new pad"); } }
/** * @return \App\Components\Pads\Pads */ protected function createComponentPads() { $this->pads = $this->padManager->findAll(); return $this->padsFactory->create($this->pads); }