Skip to content

Commit

Permalink
Merge pull request #1671 from sjinks/issue-1646
Browse files Browse the repository at this point in the history
Add Phalcon\Forms\Element\Radio
  • Loading branch information
Phalcon committed Dec 11, 2013
2 parents 6950c44 + 771630e commit f26d0cd
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
- Added support for HTML attributes to Phalcon\Forms\Form::label() (#1029)
- Phalcon\Forms\Form::getMessages() does not generate a fatal error if the form is valid (#1349)
- Improvements to Phalcon\Forms\Form::add() (#1386)
- Added Phalcon\Forms\Element\Radio class (#1646)
- Phalcon\Http:
- Phalcon\Http\Cookie can be used without sessions (#875)
- Phalcon\Http\Cookie does not ignore expire time (#897)
Expand Down
1 change: 1 addition & 0 deletions ext/config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ forms/element/text.c \
forms/element/select.c \
forms/element/textarea.c \
forms/element/check.c \
forms/element/radio.c \
forms/element/numeric.c \
forms/element/submit.c \
forms/element/date.c \
Expand Down
2 changes: 1 addition & 1 deletion ext/config.w32
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ if (PHP_PHALCON != "no") {
ADD_SOURCES("ext/phalcon/db/adapter/pdo", "sqlite.c mysql.c oracle.c postgresql.c", "phalcon")
ADD_SOURCES("ext/phalcon/db/adapter", "pdo.c", "phalcon")
ADD_SOURCES("ext/phalcon/forms", "form.c manager.c exception.c element.c elementinterface.c", "phalcon")
ADD_SOURCES("ext/phalcon/forms/element", "file.c email.c hidden.c password.c text.c select.c textarea.c check.c numeric.c submit.c date.c", "phalcon")
ADD_SOURCES("ext/phalcon/forms/element", "file.c email.c hidden.c password.c text.c select.c textarea.c check.c numeric.c submit.c date.c radio.c", "phalcon")
ADD_SOURCES("ext/phalcon/http", "response.c requestinterface.c request.c cookie.c responseinterface.c", "phalcon")
ADD_SOURCES("ext/phalcon/http/request", "file.c exception.c fileinterface.c", "phalcon")
ADD_SOURCES("ext/phalcon/http/cookie", "exception.c", "phalcon")
Expand Down
78 changes: 78 additions & 0 deletions ext/forms/element/radio.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
+------------------------------------------------------------------------+
| Phalcon Framework |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2013 Phalcon Team (http://www.phalconphp.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file docs/LICENSE.txt. |
| |
| If you did not receive a copy of the license and are unable to |
| obtain it through the world-wide-web, please send an email |
| to license@phalconphp.com so we can send you a copy immediately. |
+------------------------------------------------------------------------+
| Authors: Andres Gutierrez <andres@phalconphp.com> |
| Eduar Carvajal <eduar@phalconphp.com> |
| Vladimir Kolesnikov <vladimir@extrememember.com> |
+------------------------------------------------------------------------+
*/

#include "php_phalcon.h"

#include "forms/element/radio.h"
#include "forms/element.h"
#include "forms/elementinterface.h"

#include "kernel/main.h"
#include "kernel/memory.h"
#include "kernel/fcall.h"

/**
* Phalcon\Forms\Element\Radio
*
* input[type="radio"] for forms
*/
zend_class_entry *phalcon_forms_element_radio_ce;

PHP_METHOD(Phalcon_Forms_Element_Radio, render);

static const zend_function_entry phalcon_forms_element_radio_method_entry[] = {
PHP_ME(Phalcon_Forms_Element_Radio, render, arginfo_phalcon_forms_elementinterface_render, ZEND_ACC_PUBLIC)
PHP_FE_END
};

/**
* Phalcon\Forms\Element\Radio initializer
*/
PHALCON_INIT_CLASS(Phalcon_Forms_Element_Radio) {

PHALCON_REGISTER_CLASS_EX(Phalcon\\Forms\\Element, Radio, forms_element_radio, phalcon_forms_element_ce, phalcon_forms_element_radio_method_entry, 0);

zend_class_implements(phalcon_forms_element_radio_ce TSRMLS_CC, 1, phalcon_forms_elementinterface_ce);

return SUCCESS;
}

/**
* Renders the element widget returning html
*
* @param array $attributes
* @return string
*/
PHP_METHOD(Phalcon_Forms_Element_Radio, render){

zval *attributes = NULL, *widget_attributes;

PHALCON_MM_GROW();

phalcon_fetch_params(1, 0, 1, &attributes);

if (!attributes) {
attributes = PHALCON_GLOBAL(z_null);
}

PHALCON_OBS_VAR(widget_attributes);
phalcon_call_method_p2_ex(widget_attributes, &widget_attributes, this_ptr, "prepareattributes", attributes, PHALCON_GLOBAL(z_true));
phalcon_return_call_static_p1("phalcon\\tag", "radiofield", widget_attributes);
RETURN_MM();
}
29 changes: 29 additions & 0 deletions ext/forms/element/radio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
+------------------------------------------------------------------------+
| Phalcon Framework |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2013 Phalcon Team (http://www.phalconphp.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file docs/LICENSE.txt. |
| |
| If you did not receive a copy of the license and are unable to |
| obtain it through the world-wide-web, please send an email |
| to license@phalconphp.com so we can send you a copy immediately. |
+------------------------------------------------------------------------+
| Authors: Andres Gutierrez <andres@phalconphp.com> |
| Eduar Carvajal <eduar@phalconphp.com> |
| Vladimir Kolesnikov <vladimir@extrememember.com> |
+------------------------------------------------------------------------+
*/

#ifndef PHALCON_FORMS_ELEMENT_RADIO_H
#define PHALCON_FORMS_ELEMENT_RADIO_H

#include "php_phalcon.h"

extern zend_class_entry *phalcon_forms_element_radio_ce;

PHALCON_INIT_CLASS(Phalcon_Forms_Element_Radio);

#endif /* PHALCON_FORMS_ELEMENT_RADIO_H */
1 change: 1 addition & 0 deletions ext/phalcon.c
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,7 @@ static PHP_MINIT_FUNCTION(phalcon){
PHALCON_INIT(Phalcon_Forms_Element_Submit);
PHALCON_INIT(Phalcon_Forms_Element_Password);
PHALCON_INIT(Phalcon_Forms_Element_TextArea);
PHALCON_INIT(Phalcon_Forms_Element_Radio);
PHALCON_INIT(Phalcon_Crypt);
PHALCON_INIT(Phalcon_Translate_Exception);
PHALCON_INIT(Phalcon_Translate_Adapter_NativeArray);
Expand Down
1 change: 1 addition & 0 deletions ext/phalcon.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@
#include "forms/element/submit.h"
#include "forms/element/password.h"
#include "forms/element/textarea.h"
#include "forms/element/radio.h"
#include "crypt.h"
#include "translate/exception.h"
#include "translate/adapter/nativearray.h"
Expand Down
4 changes: 4 additions & 0 deletions unit-tests/FormsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,12 @@ public function testFormElementRender()
$element1 = new Text("name");
$element1->setAttributes(array('class' => 'big-input'));

$element2 = new \Phalcon\Forms\Element\Radio('radio');
$element2->setAttributes(array('value' => 0));

$this->assertEquals('<input type="text" id="name" name="name" value="" class="big-input" />', $element1->render());
$this->assertEquals('<input type="text" id="name" name="name" value="" class="big-input" />', (string) $element1);
$this->assertEquals('<input type="radio" id="radio" name="radio" value="0" />', (string)$element2);
}

public function testForm()
Expand Down

0 comments on commit f26d0cd

Please sign in to comment.