I'd like to add a login form to my navbar using Yiistrap.
However the documentation is lacking.
Does anyone know how to implement this?
You can extend TbHtml class with something like this:
class HHtml extends TbHtml {
/**
* #param $action
* #param string $method
* #param array $htmlOptions
* #return string
*/
public static function loginForm($action, $method = 'post', $htmlOptions = array()) {
if (isset($htmlOptions['visible']) && !$htmlOptions['visible'])
self::addCssClass('hide', $htmlOptions);
self::addCssClass('navbar-form', $htmlOptions);
$output = self::beginFormTb(self::FORM_LAYOUT_INLINE, $action, $method, $htmlOptions);
$output .= self::textField('UserLogin[username]', '', array('placeholder' => 'Username', 'size' => TbHtml::INPUT_SIZE_SMALL));
$output .= self::passwordField('UserLogin[password]', '', array('placeholder' => 'Password', 'size' => TbHtml::INPUT_SIZE_SMALL));
//$output .= self::checkBox('UserLogin[rememberMe]', false, array('label' => 'Remember me'));
$output .= self::submitButton('Sign in');
$output .= parent::endForm();
return $output;
}
}
So you can use it items section of TbNavbar that way:
array(
'class' => 'bootstrap.widgets.TbNav',
'htmlOptions' => array(
'class' => 'pull-right'
),
'items' =>array(
HHtml::loginForm('/site/login', 'post', array('visible' => Yii::app()->user->isGuest)),
array('label'=>'Logout ('.Yii::app()->user->name.')', 'url'=>array('/site/logout'), 'visible'=>!Yii::app()->user->isGuest),
),
),
<?php $this->widget('bootstrap.widgets.BsNavbar', array(
'collapse' => TRUE,
'color' => BsHtml::NAVBAR_COLOR,
'brandLabel' => BsHtml::icon(BsHtml::GLYPHICON_HOME),
'brandUrl' => Yii::app()->homeUrl,
'items' => array(
array(
'class' => 'bootstrap.widgets.BsNav',
'type' => 'navbar',
'activateParents' => true,
'items' => array(
//other items
),
),
BsHtml::navbarSearchForm(Yii::app()->createUrl('search'), 'get', array(
'class' => 'navbar-form navbar-right',
)),
))); ?>
Related
I'm new to prestashop and I worked the whole day on creating a back office interface that allows the user to write, edit, and delete articles. It is sort of a blog. I used Prestashop's Helpers (Form and List) and everything works great. I also added a new tab in the back office to access this tool.
The problem is that the layout is messy and doesn't look like the other forms and listing pages. The layout is really not sexy. Maybe I should look at some css file, or add any function in my controller ? You'll find the source code of the latter here (I can't insert images, not enough reputation --'):
<?php
class Article extends ObjectModel
{
/** #var string Name */
public $id_article;
public $titre;
public $contenu;
public $url_photo;
/**
* #see ObjectModel::$definition
*/
public static $definition = array(
'table' => 'article',
'primary' => 'id_article',
'fields' => array(
'titre' => array(
'type' => self::TYPE_STRING,
'validate' => 'isGenericName',
'required' => true,
'class' => 'lg'
),
'contenu' => array(
'type' => self::TYPE_STRING,
'validate' => 'isGenericName',
'required' => true
),
'url_photo' => array(
'type' => self::TYPE_STRING,
'validate' => 'isGenericName',
'required' => false,
),
),
);
}
class AdminBlogController extends AdminController{
public function initContent(){
parent::initContent();
}
public function __construct(){
$this->table = 'article';
$this->className = 'Article';
$this->lang = false;
// Building the list of records stored within the "article" table
$this->fields_list = array(
'id_article' => array(
'title' => 'ID',
'align' => 'center',
'width' => 25
),
'titre' => array(
'title' => 'Titre',
'width' => 'auto'
),
'contenu' => array(
'title' => 'Contenu',
'width' => 'auto'
)
);
// This adds a multiple deletion button
$this->bulk_actions = array(
'delete' => array(
'text' => $this->l('Delete selected'),
'confirm' => $this->l('Delete selected items?')
)
);
parent::__construct();
}
// This method generates the list of results
public function renderList(){
// Adds an Edit button for each result
$this->addRowAction('edit');
// Adds a Delete button for each result
$this->addRowAction('delete');
return parent::renderList();
}
// This method generates the Add/Edit form
public function renderForm(){
// Building the Add/Edit form
$this->fields_form = array(
'tinymce' => true,
'legend' => array(
'title' => 'Article'
),
'input' => array(
array(
'type' => 'text',
'label' => 'Titre',
'name' => 'titre',
'class' => 'lg',
'required' => true,
//'desc' => 'Nom de l\'article',
),
array(
'type' => 'textarea',
'label' => 'Contenu',
'name' => 'contenu',
'class' => 'lg',
'required' => true,
'autoload_rte' => true,
//'desc' => 'Contenu de l\'article',
),
array(
'type' => 'file',
'label' => 'Photo',
'name' => 'url_photo',
'class' => 'lg',
'required' => true,
//'desc' => 'Contenu de l\'article',
)
),
'submit' => array(
'title' => $this->l('Save'),
'class' => 'button'
)
);
return parent::renderForm();
}
}
?>
Thank you.
I just needed to set $this->bootstrap = true
I'm new with ZendFramework and Doctrine 2.
I try to implement a form with fieldset and Doctrine Hydrator.
The form and validators work good, but not Hydrator.
My source code:
AuthController.php
public function signupAction()
{
$userForm = new UserForm($this->getServiceLocator()->get('Doctrine\ORM\EntityManager'));
$request = $this->getRequest();
if ($request->isPost()) {
var_dump($request->getPost());
$userForm->setData($request->getPost());
if ($userForm->isValid()) {
$user = $userForm->getData();
var_dump($user);
}
}
return new ViewModel(array(
'form' => $userForm,
));
}
UserForm.php
class UserForm extends Form
{
public function __construct($em) {
parent::__construct('User');
$this->setAttribute('method', 'post')
->setHydrator(new DoctrineObject($em, 'User\Entity\User'));
$this->add(array(
'type' => 'User\Form\Fieldset\UserFieldset',
'options' => array(
'use_as_base_fieldset' => true
)
));
$this->add(array(
'name' => 'submit',
'attributes' => array(
'type' => 'submit',
'value' => 'Send'
)
));
}
}
UserFieldset.php
class UserFieldset extends Fieldset implements InputFilterProviderInterface, ObjectManagerAwareInterface
{
/**
* #var ObjectManager
*/
private $objectManager;
public function __construct()
{
parent::__construct('User');
// Id
$this->add(array(
'name' => 'id',
'type' => 'Zend\Form\Element\Hidden',
));
// FirstName
$this->add(array(
'name' => 'firstName',
'attributes' => array(
'required' => 'required',
),
'options' => array(
'label' => 'Prénom',
),
'type' => 'Zend\Form\Element\Text',
));
}
public function getInputFilterSpecification()
{
return array(
'firstName' => array(
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 2,
'max' => 100,
'messages' => array(
\Zend\Validator\StringLength::TOO_LONG => 'Le prénom est trop long (maximum %max% caractères)',
\Zend\Validator\StringLength::TOO_SHORT => 'Le prénom est trop court (minimum %min% caractères)',
),
),
),
),
),
'id' => array(
'required' => false,
),
);
}
public function setOption($key, $value) { }
/**
* Set the object manager
*
* #param ObjectManager $objectManager
*/
public function setObjectManager(ObjectManager $objectManager)
{
$this->objectManager = $objectManager;
}
/**
* Get the object manager
*
* #return ObjectManager
*/
public function getObjectManager()
{
return $this->getObjectManager();
}
}
I have not a User object, but a array...
A idea?
Thanks you!
Aurélien
so i have some entities and i want to validate my forms, i use Zend Framework 2 and Doctrine Orm, Update Thanks to Notuser : now i get this error :
Fatal error: Call to undefined method Zend\ServiceManager\ServiceManager::initForm() in C:\wamp2\www\test\module\Application\src\Application\Controller\BlogController.php
this is my model.config.php :
'doctrine' => array(
'driver' => array(
'application_entities' => array(
'class' =>'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(__DIR__ . '/../src/Application/Entity')
),
'application_forms' => array(
'class' =>'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(__DIR__ . '/../src/Application/Form')
),
'application_inputs_filters' => array(
'class' =>'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(__DIR__ . '/../src/Application/InputFilter')
),
'orm_default' => array(
'drivers' => array(
'Application\Entity' => 'application_entities',
'Application\Form' => 'application_forms',
'Application\InputFilter' => 'application_inputs_filters'
)
)
)
),
and this my controller :
namespace Application\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Application\Entity\Article;
use Application\Entity\Image;
use Application\Form\ArticleForm;
class BlogController extends AbstractActionController
{
protected $_objectManager;
public function addAction()
{
$form = $this->getServiceLocator('Application\Form\ArticleForm');
$form->initForm();
$request = $this->getRequest();
$form->setData($request->getPost());
$article = new Article();
if ($this->zfcUserAuthentication()->hasIdentity()) {
if ($form->isValid())
{
$file = $this->params()->fromFiles('url');
$adapter = new \Zend\File\Transfer\Adapter\Http();
$adapter->setDestination('public/upload');
if($adapter->receive($file['name'])){
$article->setTitle($this->getRequest()->getPost('title'));
$article->setDate(new \DateTime($this->getRequest()->getPost('date')));
$article->setContent($this->getRequest()->getPost('content'));
$article->setPublication($this->getRequest()->getPost('publication'));
$image = new Image();
$image->setUrl($file['name']);
$image->setAlt($this->getRequest()->getPost('alt'));
$article->setImage($image);
$this->getObjectManager()->persist($article);
$this->getObjectManager()->flush();
$newId = $article->getId();
return $this->redirect()->toRoute('blog');
}
}
}
else
{
return $this->redirect()->toRoute('user');
}
return new ViewModel(array('article' => $article));
}
And this is my ArticleForm :
class ArticleForm extends Form {
public function __construct()
{
parent::__construct('UserEntry');
$this->setAttribute('method', 'post');
$this->setAttribute('enctype', 'multipart/form-data');
$this->setAttribute('class', 'contact_form');
}
/**
*
*/
public function initForm()
{
$this->addFormFields(); //function where we added all fields
$articleInputFilter = new ArticleInputFilter();
$this->setInputFilter($articleInputFilter->getInputFilter()); //Asign input Filter to form
}
/**
*
*/
protected function addFormFields()
{
$this->addSubmit();
$this->addTitle();
$this->addContent();
$this->addDate();
$this->addPublication();
$this->addImage();
}
/**
*
*/
protected function addTitle()
{
$this->add(array(
'name' => 'title',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => _('Title')
),
));
}
/**
*
*/
protected function addContent()
{
$this->add(array(
'name' => 'content',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => _('Content')
),
));
}
/**
*
*/
protected function addDate()
{
$this->add(array(
'name' => 'date',
'attributes' => array(
'type' => 'date',
),
'options' => array(
'label' => _('Date'),
'id' => 'datepicker',
),
));
}
/**
*
*/
protected function addPublication()
{
$this->add(array(
'name' => 'publication',
'attributes' => array(
'type' => 'checkbox',
),
'options' => array(
'label' => _('Publication'),
'use_hidden_element' => true,
'checked_value' => 1,
'unchecked_value' => 'no',
),
));
}
/**
*
*/
protected function addImage()
{
$this->add(array(
'name' => 'Image',
'attributes' => array(
'type' => new ImageForm(),
),
'options' => array(
'label' => _('Image')
),
));
}
/**
*
*/
protected function addSubmit()
{
$this->add(array(
'name' => 'submit',
'attributes' => array(
'type' => 'submit',
'value' => _('Add'),
'class' => 'submit',
),
));
}
}
Finally this is my ArticleInputFilter :
class ArticleInputFilter extends InputFilter implements InputFilterAwareInterface
{
/**
* #var string
*/
public $title;
/**
* #var int
*/
public $image;
/**
* #var string
*/
public $content;
/**
* #var Date
*/
public $date;
/**
* #var Boolean
*/
public $publication;
/**
* #param $data
*/
public function exchangeArray($data)
{
$this->title = (isset($data['title'])) ? $data['title'] : $this->title;
$this->image = (isset($data['image'])) ? $data['image'] : $this->image;
$this->content = (isset($data['content'])) ? $data['content'] : $this->content;
$this->publication = (isset($data['publication'])) ? $data['publication'] : $this->publication;
$this->date = (isset($data['date'])) ? $data['date'] : $this->date;
}
/**
* #param InputFilterInterface $inputFilter
* #return void|InputFilterAwareInterface
* #throws \Exception
*/
public function setInputFilter(InputFilterInterface $inputFilter)
{
throw new \Exception("Not used");
}
/**
* #return InputFilter|InputFilterInterface
*/
public function getInputFilter()
{
if (!$this->inputFilter) {
$inputFilter = new InputFilter();
$factory = new InputFactory();
$inputFilter->add($factory->createInput(array(
'name' => 'title',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 6,
'max' => 100,
),
),
),
)));
$inputFilter->add($factory->createInput(array(
'name' => 'content',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 10,
),
),
),
)));
$inputFilter->add($factory->createInput(array(
'name' => 'publication',
'required' => false,
)));
$inputFilter->add($factory->createInput(array(
'name' => 'date',
'required' => true,
)));
$inputFilter->add($factory->createInput(array(
'name' => 'image',
'required' => true,
)));
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
}
That it i write my hole code except my ImageForm and ImageInputFilter, So please if someone has any idea how to do that i will be very appreciative ;)
In controller
$form = $this->getServiceLocator('Application\Form\BlogForm");//don't forger to add Application\Form\BlogForm to module.config.php and check your namespace
$form->initForm();//explained further
/** #var \Zend\Http\Request $request */
$request = $this->getRequest();//whole request fo #var for further using
if ($request->isPost()) { //there is something in post
$form->setData($request->getPost()); //data from post is added for further validation
if ($form->isValid()) { //Form is Valid lets save form
$files = $request->getFiles()->toArray(); //array witch files use var_dump($files);die; to show structure
$article = new Article();
$article->setTitle($request->getPost('title'));
...
if(!empty($files)&&$files['image']['error']==0) {
$article->setImage($functionToCreateImageEntityFromFile(($files['image']));
}
...
$this->getObjectManager()->persist($article);
$this->getObjectManager()->flush();
$newId = $article->getId();
return $this->redirect()->toRoute('blog');
}
//form is not valid so you can display form with Errors
} //there is no post so you can display clean form
Next let's take form:
(it's my proposal)
namespace Application\Form; //check your namespace
use Zend\Form\Form;
use Application\InputFilter\BlogInputFilter; //check your namespace
class BlogForm extends Form {
public function __construct()
{
parent::__construct('UserEntry');
$this->setAttribute('method', 'post');
$this->setAttribute('enctype', 'multipart/form-data');
}
/**
*
*/
public function initForm()
{
$this->addFormFields(); //function where we added all fields
$blogInputFilter = new BlogInputFilter(); //Input Filter for Validation
$this->setInputFilter($BlogInputFilter->getInputFilter()); //Asign input Filter to form
}
/**
*
*/
protected function addFormFields()
{
$this->addSubmit();
$this->addTitle();
$this->addImage();
...//add more here
}
/**
*
*/
protected function addTitle()
{
$this->add(array(
'name' => 'title',
'attributes' => array(
'type' => 'text',
'id' => 'title',
'class' => 'text'
),
'options' => array(
'label' => _('Title') //it's only for multilang you can put here any string
),
));
}
/**
*
*/
protected function addImage()
{
$this->add(array(
'name' => 'image',
'attributes' => array(
'type' => 'file',
'id' => 'image'
),
'options' => array(
'label' => _('Image'),
),
));
}
/**
*
*/
protected function addSubmit()
{
$this->add(array(
'name' => 'submit',
'attributes' => array(
'type' => 'submit',
'value' => _('Save'),
'class' => 're',
),
));
}
}
Now its Time for InputFilter
namespace Application\InputFilter;//check your namespace
use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;
class BlogInputFilter implements InputFilterAwareInterface
{
/**
* #var int
*/
public $title;
/**
* #var string
*/
public $image;
//add all fields
/**
* #param $data
*/
public function exchangeArray($data)
{
$this->title = (isset($data['title'])) ? $data['title'] : $this->title;
$this->image = (isset($data['image'])) ? $data['image'] : $this->image;
//add fields
}
/**
* #param InputFilterInterface $inputFilter
* #return void|InputFilterAwareInterface
* #throws \Exception
*/
public function setInputFilter(InputFilterInterface $inputFilter)
{
throw new \Exception("Not used");
}
/**
* #return InputFilter|InputFilterInterface
*/
public function getInputFilter()
{
if (!$this->inputFilter) {
$inputFilter = new InputFilter();
$factory = new InputFactory();
$inputFilter->add($factory->createInput(array(
'name' => 'title',
'required' => true,//required field
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,//limit from 1 to 100 chars
),
),
),
)));
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
}
to read:
Forms: http://framework.zend.com/manual/2.0/en/modules/zend.form.quick-start.html#factory-backed-form-extension
ImputFilter: http://framework.zend.com/manual/2.0/en/modules/zend.input-filter.intro.html
File upload: Can't find good tutorial
module.config.php should have key like this:
'service_manager' => array(
'invokables' => array(
'Application\Form\ArticleForm' => 'Application\Form\ArticleForm',
),
),
There is no need to add Application\Form\ArticleForm to use becouse you use ServiceLocator
This is your problem:
$form = $this->getServiceLocator('Application\Form\ArticleForm');
$form->initForm();
It should be:
$form = $this->getServiceLocator()->get('Application\Form\ArticleForm');
$form->initForm();
The way you have written it, the bracket contents are ignored and it becomes:
$form = $this->getServiceLocator()
which is why you get an "undefined method: ServiceManager::initForm()" error when you call initForm().
looking for some help. I want to pass the group id along with all the other zendesk ticket info from a Drupal form. I've tried everything and can't get the group-id to work. Everything else is working. This is what I currently have:
function zendesk_forms_support_form() {
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Your name'),
'#required' => TRUE,
);
$form['group_id'] = array(
'#title' => t('Group ID'),
'#type' => 'textfield',
);
$form['requester'] = array(
'#type' => 'textfield',
'#title' => t('Your e-mail address'),
'#required' => TRUE,
);
$form['subject'] = array(
'#type' => 'textfield',
'#title' => t('Subject'),
);
$form['description'] = array(
'#type' => 'textarea',
'#title' => t('Message'),
'#required' => TRUE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Send'),
);
$form['#attributes'] = array('class' => 'zendesk-support-form');
return $form;
}
AND
function zendesk_forms_support_form_submit($form, &$form_state) {
$ticket = array(
'description' => $form_state['values']['description'],
'requester' => array(
'name' => $form_state['values']['name'],
'email' => $form_state['values']['requester'],
'group_id' => $form_state['values']['group_id'],
),
);
if (!empty($form_state['values']['subject'])) {
$ticket['subject'] = $form_state['values']['subject'];
}
AND THIS IS THE WHOLE LOT!
<?php
/**
* #file
* Main file for the Zendesk Forms module.
*/
/**
* Implements hook_menu().
*/
function zendesk_forms_menu() {
$items = array();
$items['admin/config/services/zendesk-form'] = array(
'title' => 'Zendesk forms',
'description' => 'Configure Zendesk forms.',
'access arguments' => array('administer zendesk form'),
'page callback' => 'drupal_get_form',
'page arguments' => array('zendesk_forms_settings_form'),
'file' => 'zendesk_forms.admin.inc',
'file path' => drupal_get_path('module', 'zendesk_forms'),
);
return $items;
}
/**
* Implements hook_permission().
*/
function zendesk_forms_permission() {
return array(
'administer zendesk form' => array(
'title' => t('Administer Zendesk Form'),
),
);
}
/**
* Implements hook_block_info().
*/
function zendesk_forms_block_info() {
$blocks = array();
$blocks['zendesk_support_form'] = array(
'info' => t('Zendesk support form'),
'cache' => DRUPAL_CACHE_GLOBAL,
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function zendesk_forms_block_view($delta = '') {
$block = array();
switch ($delta) {
case 'zendesk_support_form':
$block['subject'] = t('Support');
$block['content'] = drupal_get_form('zendesk_forms_support_form');
break;
}
return $block;
}
/**
* Form to create a ticket with Zendesk.
*
* #ingroup forms
*/
function zendesk_forms_support_form() {
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Your name'),
'#required' => TRUE,
);
$form['group_id'] = array(
'#title' => t('Group ID'),
'#type' => 'textfield',
);
$form['requester'] = array(
'#type' => 'textfield',
'#title' => t('Your e-mail address'),
'#required' => TRUE,
);
$form['subject'] = array(
'#type' => 'textfield',
'#title' => t('Subject'),
);
$form['description'] = array(
'#type' => 'textarea',
'#title' => t('Message'),
'#required' => TRUE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Send'),
);
$form['#attributes'] = array('class' => 'zendesk-support-form');
return $form;
}
/**
* Validate the create ticket form.
*
* #ingroup forms
*/
function zendesk_forms_support_form_validate($form, &$form_state) {
if (!valid_email_address($form_state['values']['requester'])) {
form_set_error('requester', t('Supplied email is not valid'));
}
}
/**
* Submit handler for the create ticket form.
*
* #ingroup forms
*/
function zendesk_forms_support_form_submit($form, &$form_state) {
$ticket = array(
'description' => $form_state['values']['description'],
'requester' => array(
'name' => $form_state['values']['name'],
'email' => $form_state['values']['requester'],
'group_id' => $form_state['values']['group_id'],
),
);
if (!empty($form_state['values']['subject'])) {
$ticket['subject'] = $form_state['values']['subject'];
}
// Get extra fields that are prefixed with zendesk_. Right now the only way
// to add extra fields is by using form alter though.
$extra_fields = array_filter(array_keys($form_state['values']), create_function('$item', 'return strpos($item, "zendesk_") === 0;'));
if (!empty($extra_fields)) {
foreach ($extra_fields as $field_name) {
$ticket[substr($field_name, 8)] = $form_state['values'][$field_name];
}
}
// Wrap the ticket array in yet another array and call the API.
$response = zendesk_forms_api_call(array('ticket' => $ticket), 'tickets.json');
if (!isset($response->error)) {
drupal_set_message(t('Your message has been sent'));
}
else {
drupal_set_message(t('An error happened while sending the message.'), 'error');
}
}
/**
* Performs a call to the Zendesk API with the given data.
*
* #param array $data
* The data to be sent to the API endpoint.
* #param string $command
* The command to call at the endpoint. Will be a filename like "tickets.json"
* or similar.
* See http://developer.zendesk.com/documentation/rest_api/tickets.html and
* https://support.zendesk.com/entries/21562288-Creating-a-Custom-HTML-Form-for-Ticket-Submission
*
* #return string
* The json response string.
*/
function zendesk_forms_api_call($data, $command) {
$json = json_encode($data);
$zendesk_key = variable_get('zendesk_forms_api_key', '');
$zendesk_url = variable_get('zendesk_forms_url', '');
$zendesk_user = variable_get('zendesk_forms_user_name', '');
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_URL, $zendesk_url . '/api/v2/' . $command);
curl_setopt($ch, CURLOPT_USERPWD, "$zendesk_user/token:$zendesk_key");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($ch, CURLOPT_USERAGENT, 'MozillaXYZ/1.0');
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$output = curl_exec($ch);
curl_close($ch);
return json_decode($output);
}
$ticket = array(
'description' => $form_state['values']['description'],
'requester' => array(
'name' => $form_state['values']['name'],
'email' => $form_state['values']['requester'],
'group_id' => $form_state['values']['group_id'],
),
CHANGED TO
$ticket = array(
'description' => $form_state['values']['description'],
'group_id' => $form_state['values']['group_id'],
'requester' => array(
'name' => $form_state['values']['name'],
'email' => $form_state['values']['requester'],
),
I would like to create a fieldset under each radio/checkbox item.
e.g
Which animals do you like:
[x] Cats
[Fieldset of cat related questions]
[ ] Dogs
[Fieldset of dog related questions]
...
I can create Fieldsets and Forms with ease, but nesting them under each item is causing me some headaches.
Ultimately I had in mind something like this:
$this->add(array(
'type' => 'Zend\Form\Element\Radio',
'name' => 'profile_type',
'options' => array(
'label' => 'Which animals do you like:',
'label_attributes' => array('class'=>'radio inline'),
'value_options' => array(
'1' =>'cats',
'fieldsets' => array(
array(
'name' => 'sender',
'elements' => array(
array(
'name' => 'name',
'options' => array(
'label' => 'Your name',
),
'type' => 'Text'
),
array(
'type' => 'Zend\Form\Element\Email',
'name' => 'email',
'options' => array(
'label' => 'Your email address',
),
),
),
)),
'2'=>'dogs',
'fieldsets' => array(
array(
'name' => 'sender',
'elements' => array(
array(
'name' => 'name',
'options' => array(
'label' => 'Your name',
),
'type' => 'Text'
),
array(
'type' => 'Zend\Form\Element\Email',
'name' => 'email',
'options' => array(
'label' => 'Your email address',
),
),
),
))
),
),
'attributes' => array(
'value' => '1' //set checked to '1'
)
));
Ok I managed to get round this issue using a bit of a hack, but it works.
TestForm.php (note the additional attributes in the elements ('parent' and 'childOf')
class TestForm extends Form
{
public $user_agent;
public $user_ip;
public function __construct($name = null)
{
// we want to ignore the name passed
parent::__construct('profile');
$this->setAttributes(array(
'method'=>'post',
'class'=>'form-horizontal',
));
$this->add(array(
'type' => 'Zend\Form\Element\Radio',
'name' => 'profile_type',
'options' => array(
'label' => 'Which animals do you like:',
'label_attributes' => array('class'=>'radio inline'),
'value_options' => array(
array('label' =>'cats', 'value'=>1, 'parent'=>'cat'),
array('label'=>'dogs', 'value'=>2, 'parent'=>'dog'),
),
),
'attributes' => array(
'value' => '1' //set checked to '1'
)
));
$this->add(array(
'type' => 'Text',
'name' => 'name',
'attributes' => array(
'childOf' => 'cat',
),
'options' => array(
'label' => 'Your name',
),
));
$this->add(array(
'type' => 'Zend\Form\Element\Email',
'name' => 'email',
'attributes' => array(
'childOf' => 'dog',
),
'options' => array(
'label' => 'Your email address',
),
));
}
}
Then extended Zend\Form\View\Helper\FormMultiCheckbox and overwrote the RenderOptions Method (look out for the new optionSpec 'parent') this basically creates a tag e.g.{#cat#} after the parent element:
protected function renderOptions(MultiCheckboxElement $element, array $options, array $selectedOptions,
array $attributes)
{
$escapeHtmlHelper = $this->getEscapeHtmlHelper();
$labelHelper = $this->getLabelHelper();
$labelClose = $labelHelper->closeTag();
$labelPosition = $this->getLabelPosition();
$globalLabelAttributes = $element->getLabelAttributes();
$closingBracket = $this->getInlineClosingBracket();
if (empty($globalLabelAttributes)) {
$globalLabelAttributes = $this->labelAttributes;
}
$combinedMarkup = array();
$count = 0;
foreach ($options as $key => $optionSpec) {
$count++;
if ($count > 1 && array_key_exists('id', $attributes)) {
unset($attributes['id']);
}
$value = '';
$parent = '';
$label = '';
$inputAttributes = $attributes;
$labelAttributes = $globalLabelAttributes;
$selected = isset($inputAttributes['selected']) && $inputAttributes['type'] != 'radio' && $inputAttributes['selected'] != false ? true : false;
$disabled = isset($inputAttributes['disabled']) && $inputAttributes['disabled'] != false ? true : false;
if (is_scalar($optionSpec)) {
$optionSpec = array(
'label' => $optionSpec,
'value' => $key
);
}
if (isset($optionSpec['value'])) {
$value = $optionSpec['value'];
}
if (isset($optionSpec['parent'])) {
$parent = $optionSpec['parent'];
}
if (isset($optionSpec['label'])) {
$label = $optionSpec['label'];
}
if (isset($optionSpec['selected'])) {
$selected = $optionSpec['selected'];
}
if (isset($optionSpec['disabled'])) {
$disabled = $optionSpec['disabled'];
}
if (isset($optionSpec['label_attributes'])) {
$labelAttributes = (isset($labelAttributes))
? array_merge($labelAttributes, $optionSpec['label_attributes'])
: $optionSpec['label_attributes'];
}
if (isset($optionSpec['attributes'])) {
$inputAttributes = array_merge($inputAttributes, $optionSpec['attributes']);
}
if (in_array($value, $selectedOptions)) {
$selected = true;
}
$inputAttributes['value'] = $value;
$inputAttributes['checked'] = $selected;
$inputAttributes['disabled'] = $disabled;
$input = sprintf(
'<input %s%s',
$this->createAttributesString($inputAttributes),
$closingBracket
);
if (null !== ($translator = $this->getTranslator())) {
$label = $translator->translate(
$label, $this->getTranslatorTextDomain()
);
}
$tag = ($parent != '')? "{#*".$parent."*#}": "";
$label = $escapeHtmlHelper($label);
$labelOpen = $labelHelper->openTag($labelAttributes);
$template = $labelOpen . '%s%s%s' . $labelClose;
switch ($labelPosition) {
case self::LABEL_PREPEND:
$markup = sprintf($template, $label, $input, $tag);
break;
case self::LABEL_APPEND:
default:
$markup = sprintf($template, $input, $label, $tag);
break;
}
$combinedMarkup[] = $markup;
}
return implode($this->getSeparator(), $combinedMarkup);
}
Extended and overwrote Zend\Form\View\Helper\FormRow render method this is the same except for the return of the method:
..... more code .....
$child_of = $element->getAttribute('childOf');
if($child_of != '')
{
return array($child_of => sprintf('<div class="control-group%s">%s</div>', $status_type, $markup));
}
return sprintf('<div class="control-group%s">%s</div>', $status_type, $markup);
And finally extended and overwrote the render method of Zend\Form\View\Helper\FormCollection and changed the element foreach loop, basically overwriting the tags if the element is an array, and therefore has a ChildOf tag. Then a clean up of tags:
foreach ($element->getIterator() as $elementOrFieldset) {
if ($elementOrFieldset instanceof FieldsetInterface) {
$markup .= $fieldsetHelper($elementOrFieldset);
} elseif ($elementOrFieldset instanceof ElementInterface) {
$elementString = $elementHelper($elementOrFieldset);
if(!is_array($elementString))
{
$markup .= $elementString;
}
// is child of another element
else
{
foreach($elementString as $key => $value)
{
$match = "{#*".$key."*#}";
$replacement = $value.$match;
$markup = str_replace($match, $replacement, $markup);
}
}
}
}
$pattern = '/[{#\*]+[a-z0-0A-Z]*[\*#}]+/';
$markup = preg_replace($pattern, '', $markup);
This (although ugly) produces the desired results, also because we are just playing with the rendering, validation and form creation are untouched.
All the best,
Aborgrove