Skip to content

Instantly share code, notes, and snippets.

@alexander-torosh
Created July 10, 2013 18:33
Show Gist options
  • Select an option

  • Save alexander-torosh/5968873 to your computer and use it in GitHub Desktop.

Select an option

Save alexander-torosh/5968873 to your computer and use it in GitHub Desktop.
Can't uncheck checkbox normaly
// My Entity
class News extends \Phalcon\Mvc\Model
{
public $pub; // boolean field, Checkbox in Form
public function getPub()
{
return $this->pub;
}
public function setPub($pub = null)
{
$this->pub = $pub;
}
}
// Form
class NewsForm extends \Phalcon\Forms\Form
{
public function initialize()
{
$pub = new \Phalcon\Forms\Element\Check("pub");
$pub->setLabel("Pub");
}
}
// Controller
public function editAction($id)
{
$id = $this->filter->sanitize($id, "int");
$news = News::findFirst($id);
$post = $_POST;
if (!isset($post['pub'])) $post['pub'] = 0; // @todo I must use it, because News::setPub() not called when I try uncheck checkbox!
$form = new NewsForm();
if ($this->request->isPost()) {
$form->bind($post, $news);
if ($form->isValid()) {
$news->save();
$this->response->redirect('news/admin/edit/' . $id);
}
} else {
$form->setEntity($news);
}
$this->view->setVar('form', $form);
$this->view->setVar('news', $news);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment