Created
November 5, 2012 21:16
-
-
Save theDisco/4020382 to your computer and use it in GitHub Desktop.
Custom Tag for Phalcon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Frontend\Controllers; | |
use App\Frontend\Models\Users; | |
class SessionController extends BaseController | |
{ | |
public function createAction () | |
{ | |
if ( !$this->request->isPost() ) { | |
$this->dispatcher->forward( array ( 'controller' => 'session', 'action' => 'register' ) ); | |
} | |
$username = $this->request->getPost( 'username', 'string' ); | |
$users = new Users; | |
$users->username = $this->filter->sanitize( $username, 'striptags' ); | |
$users->save(); | |
if ( $users->validationHasFailed() ) { | |
\Custom\Tag::setErrors($users->getMessages()); | |
$this->flashSession->error($this->intl->_('form_error')); | |
$this->dispatcher->forward( array ( 'controller' => 'session', 'action' => 'register' ) ); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Custom; | |
/** | |
* | |
*/ | |
class Tag extends \Phalcon\Tag | |
{ | |
/** | |
* @var array | |
*/ | |
private static $_errors = array(); | |
/** | |
* override Phalcon\Tag submitButton, cause Phalcon rendes an input tag instead of a button tag | |
* | |
* @param array $parameters | |
* @param string $text | |
* | |
* @return string | |
*/ | |
public static function submitButton(array $parameters = array(), $text) | |
{ | |
$button = '<button type="submit"'; | |
foreach ($parameters as $key => $value) { | |
$button .= ' ' . $key . '="' . $value . '"'; | |
} | |
$button .= '>'; | |
if ('' != $text) { | |
$button .= $text; | |
} | |
$button .= '</button>'; | |
return $button; | |
} | |
/** | |
* @param string $select | |
* @param array $parameters | |
* @param string $label | |
* | |
* @return string | |
*/ | |
protected static function _decorateSelect($select, $parameters, $label) | |
{ | |
$for = $parameters; | |
if (is_array($parameters)) { | |
$for = $parameters[0]; | |
} | |
$html = '<div class="control-group"> | |
<label class="control-label" for="' . $for . '">' . $label . '</label> | |
<div class="controls"> | |
' . $select . ' | |
</div> | |
</div>'; | |
return $html; | |
} | |
/** | |
* wrap the select tag with twitter bootstrap markup if $decorate !== false | |
* otherwise return only valid select | |
* | |
* @param array|string $parameters | |
* @param string $label | |
* @param boolean|null $decorate | |
* | |
* @return string|void | |
*/ | |
public static function select($parameters, $label, $decorate = true) | |
{ | |
$select = parent::select($parameters, ''); | |
if (false !== $decorate) { | |
$select = self::_decorateSelect($select, $parameters, $label); | |
} | |
return $select; | |
} | |
/** | |
* Custom implementation of textField | |
* | |
* @param array $parameters | |
* | |
* @return string | |
*/ | |
public static function textField(array $parameters) | |
{ | |
return static::_decorate('textField', $parameters); | |
} | |
/** | |
* Custom implementation of passwordField. | |
* | |
* @param array $parameters | |
* | |
* @return string | |
*/ | |
public static function passwordField(array $parameters) | |
{ | |
return static::_decorate('passwordField', $parameters); | |
} | |
/** | |
* Setter for errors used in generating form fields. | |
* TODO: Use model instead. This way required fields could be automatically marked and error messages automatically translated. | |
* | |
* @param array $errors | |
* | |
* @return void | |
*/ | |
public static function setErrors(array $errors) | |
{ | |
static::$_errors = $errors; | |
} | |
/** | |
* Decorate a field using bootstrap markup. | |
* | |
* @param $helper | |
* @param array $parameters | |
* | |
* @return string | |
*/ | |
private static function _decorate($helper, array $parameters) | |
{ | |
$name = static::_extractName($parameters); | |
$label = $parameters['label']; | |
unset($parameters['label']); | |
$html = sprintf('<div class="control-group%s">', static::_hasErrors($name) ? ' error' : ''); | |
$html .= sprintf('<label class="control-label" for="%s">%s</label>', $name, $label); | |
$html .= '<div class="controls">'; | |
$html .= parent::$helper($parameters); | |
$html .= '</div>'; | |
return $html; | |
} | |
/** | |
* Extracts a name from provided parameters. | |
* | |
* @param $parameters | |
* | |
* @return string | |
* @throws \Shelf\Exception | |
*/ | |
private static function _extractName($parameters) | |
{ | |
if (!isset($parameters[0])) { | |
throw new \Shelf\Exception('You need to provide at least one parameter to generate a valid field'); | |
} | |
return $parameters[0]; | |
} | |
/** | |
* Checks if errors array contains error message for provided field. | |
* | |
* @param string $field | |
* | |
* @return boolean | |
*/ | |
private static function _hasErrors($field) | |
{ | |
foreach (static::$_errors as $error) { | |
if ($error->getField() === $field) { | |
return true; | |
} | |
} | |
return false; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use \Custom\Tag as Tag; | |
echo Tag::textField(array('username', 'placeholder' => $this->intl->_('register_username'), 'label' => $this->intl->_('register_username'))); | |
/* | |
Renders: | |
------- | |
<div class="control-group error"> | |
<label class="control-label" for="username">Username</label> | |
<div class="controls"> | |
<input type="text" placeholder="Username" name="username" id="username" value=""> | |
</div> | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment