diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..187adb2 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/nbproject/ \ No newline at end of file diff --git a/Test/Case/View/Helper/Bs3FormHelperTest.php b/Test/Case/View/Helper/Bs3FormHelperTest.php index 5f7ad61..59d0d89 100644 --- a/Test/Case/View/Helper/Bs3FormHelperTest.php +++ b/Test/Case/View/Helper/Bs3FormHelperTest.php @@ -1095,4 +1095,138 @@ public function testInputGroup() { ); $this->assertTags($result, $expected); } + +/** + * testPostLink method + * + * @return void + */ + public function testPostLink() { + $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), array('confirm' => 'Confirm?')); + $expected = array( + array('form' => array('method' => 'post', 'action' => '/posts/delete/1','name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;')), + array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')), + '/form', + array('a' => array('href' => '#', 'onclick' => 'event.returnValue = false; return false;', 'data-toggle' => 'modal', 'data-target' => 'preg:/#post_\w+-modal/')), + 'Delete', + '/a', + array('div' => array('class' => 'modal fade text-left','id' => 'preg:/post_\w+\-modal/','tabindex' => '-1','role' => 'dialog','aria-labelledby' => 'preg:/#post_\w+-label/','aria-hidden' => 'true')), + array('div' => array('class' => 'modal-dialog')), + array('div' => array('class' => 'modal-content')), + array('div' => array('class' => 'modal-header')), + array('button' => array('type' => 'button','class' => 'close','data-dismiss' => 'modal','aria-hidden' => 'true')), + '×', + '/button', + array('h4' => array('class' => 'modal-title','id' => 'preg:/#post_\w+-label/')), + 'Confirmation Dialog', + '/h4', + '/div', + array('div' => array('class' => 'modal-body')), + 'Confirm?', + '/div', + array('div' => array('class' => 'modal-footer')), + array('button' => array('type' => 'button','class' => 'btn btn-default','data-dismiss' => 'modal')), + 'No', + '/button', + array('a' => array('href' => '#','class' => 'btn btn-primary','onclick' => 'preg:/document\.post_\w+\.submit\(\)\;/')), + 'Yes', + '/a', + '/div', + '/div', + '/div', + '/div' + ); + $this->assertTags($result, $expected); + + $result = $this->Form->postLink('Delete', '/posts/delete/1', array('escape' => false), '\'Confirm\' this "deletion"?'); + $expected = array( + array('form' => array('method' => 'post', 'action' => '/posts/delete/1','name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;')), + array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')), + '/form', + array('a' => array('href' => '#', 'onclick' => 'event.returnValue = false; return false;', 'data-toggle' => 'modal', 'data-target' => 'preg:/#post_\w+-modal/')), + 'Delete', + '/a', + array('div' => array('class' => 'modal fade text-left','id' => 'preg:/post_\w+\-modal/','tabindex' => '-1','role' => 'dialog','aria-labelledby' => 'preg:/#post_\w+-label/','aria-hidden' => 'true')), + array('div' => array('class' => 'modal-dialog')), + array('div' => array('class' => 'modal-content')), + array('div' => array('class' => 'modal-header')), + array('button' => array('type' => 'button','class' => 'close','data-dismiss' => 'modal','aria-hidden' => 'true')), + '×', + '/button', + array('h4' => array('class' => 'modal-title','id' => 'preg:/#post_\w+-label/')), + 'Confirmation Dialog', + '/h4', + '/div', + array('div' => array('class' => 'modal-body')), + '\'Confirm\' this "deletion"?', + '/div', + array('div' => array('class' => 'modal-footer')), + array('button' => array('type' => 'button','class' => 'btn btn-default','data-dismiss' => 'modal')), + 'No', + '/button', + array('a' => array('href' => '#','class' => 'btn btn-primary','onclick' => 'preg:/document\.post_\w+\.submit\(\)\;/')), + 'Yes', + '/a', + '/div', + '/div', + '/div', + '/div' + ); + $this->assertTags($result, $expected); + + $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1))); + $this->assertContains('', $result); + + $result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank')); + $expected = array( + array('form' => array('method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1','name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;')), + array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')), + '/form', + array('a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\)\; event\.returnValue \= false\; return false\;/')), + 'Delete', + '/a', + ); + $this->assertTags($result, $expected); + + $result = $this->Form->postLink( + 'Title', + array('controller' => 'items', 'action' => 'delete', 10), + array('class' => 'btn btn-danger', 'escape' => false), + array('text' => 'Confirm thing', 'title' => 'Thing Title') + ); + $expected = array( + array('form' => array('method' => 'post', 'action' => '/items/delete/10','name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;')), + array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')), + '/form', + array('a' => array('href' => '#', 'class' => 'btn btn-danger','onclick' => 'event.returnValue = false; return false;', 'data-toggle' => 'modal', 'data-target' => 'preg:/#post_\w+-modal/')), + 'Title', + '/a', + array('div' => array('class' => 'modal fade text-left','id' => 'preg:/post_\w+\-modal/','tabindex' => '-1','role' => 'dialog','aria-labelledby' => 'preg:/#post_\w+-label/','aria-hidden' => 'true')), + array('div' => array('class' => 'modal-dialog')), + array('div' => array('class' => 'modal-content')), + array('div' => array('class' => 'modal-header')), + array('button' => array('type' => 'button','class' => 'close','data-dismiss' => 'modal','aria-hidden' => 'true')), + '×', + '/button', + array('h4' => array('class' => 'modal-title','id' => 'preg:/#post_\w+-label/')), + 'Thing Title', + '/h4', + '/div', + array('div' => array('class' => 'modal-body')), + 'Confirm thing', + '/div', + array('div' => array('class' => 'modal-footer')), + array('button' => array('type' => 'button','class' => 'btn btn-default','data-dismiss' => 'modal')), + 'No', + '/button', + array('a' => array('href' => '#','class' => 'btn btn-primary','onclick' => 'preg:/document\.post_\w+\.submit\(\)\;/')), + 'Yes', + '/a', + '/div', + '/div', + '/div', + '/div' + ); + $this->assertTags($result, $expected); + } } diff --git a/View/Helper/Bs3FormHelper.php b/View/Helper/Bs3FormHelper.php index 08f171a..482473a 100644 --- a/View/Helper/Bs3FormHelper.php +++ b/View/Helper/Bs3FormHelper.php @@ -924,6 +924,131 @@ protected function _detectFormStyle($options) { return $options; } + +/** +* Creates an HTML link, but access the URL using the method you specify (defaults to POST). +* Requires javascript to be enabled in browser. +* +* This method creates a `