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'],
),
Related
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
I have developed ajax drupal and add a form (textfield and button) in it using #ajax key and callback function where I do my process and return my form new element.
So when I starts adding my data form, it works fine for me and form_state['values'] are updated fine.
The problem here is when I reload my form and I add some data, form_state['values'] are not the same in my form fields.
Here is my code:
function my_horoscope_menu() {
$items = array();
$items['admin/horoscopes'] = array(
'title' => 'Horoscopes',
'page callback' => 'drupal_get_form',
'page arguments' => array('my_horoscope_admin_form'),
'access arguments' => array('Administer site configuration '),
//'type' => MENU_LOCAL_TASK,
);
return $items;
}
function my_horoscope_admin_form($form, &$form_state) {
$form['settings']['horoscopes']['add_horoscopes'] = array(
'#type' => 'fieldset',
'#title' => t('Add horoscopes'),
'#collapsible' => TRUE,
'#collaspsed' => TRUE,
);
$form['settings']['horoscopes']['add_horoscopes']['name'] = array(
'#type' => 'textfield',
'#title' => t('Horoscope name'),
'#default_value' => t('Horoscope name'),
'#size' => 20,
'#maxlength' => 60,
'#required' => FALSE,
);
$form['settings']['horoscopes']['add_horoscopes']['beginning_date_rang'] = array(
'#type' => 'date',
'#title' => t('Horoscope beginning date rang'),
'#description' => t('Set the beginning date rang of this horoscope.'),
'#required' => FALSE,
);
$form['settings']['horoscopes']['add_horoscopes']['ending_date_rang'] = array(
'#type' => 'date',
'#title' => t('Horoscope ending date rang'),
'#description' => t('Set the ending date rang of this horoscope.'),
'#required' => FALSE,
);
$form['settings']['horoscopes']['add_horoscopes']['add_button'] = array(
'#type' => 'button',
'#value' => t('Add this horoscope'),
'#submit' => array(''),
'#ajax' => array(
'event' => 'click',
'callback' => 'add_horoscope_ajax_process',
'wrapper' => 'add_horoscope_wrapper',
),
);
$form['settings']['horoscopes']['add_horoscopes']['adding_horoscope_wrapper'] = array(
'#type' => 'markup',
'#prefix' => '<div id="add_horoscope_wrapper">',
'#suffix' => '</div>',
);
return $form;
}
function add_horoscope_ajax_process ($form, &$form_state) {
if (isset($form_state['values'])) {
if (isset($form_state['values']['name']) AND $form_state['values']['name'] != '') {
$name = $form_state['values']['name'];
}
if (isset($form_state['values']['beginning_date_rang']) AND $form_state['values']['beginning_date_rang'] != '') {
$beginning_date_rang = $form_state['values']['beginning_date_rang'];
$beginning_date_rang1 = sprintf("%04d-%02d-%02d", $beginning_date_rang['year'], $beginning_date_rang['month'], $beginning_date_rang['day']);
}
if (isset($form_state['values']['ending_date_rang']) AND $form_state['values']['ending_date_rang'] != '') {
$ending_date_rang = $form_state['values']['ending_date_rang'];
$ending_date_rang1 = sprintf("%04d-%02d-%02d", $ending_date_rang['year'], $ending_date_rang['month'], $ending_date_rang['day']);
}
// Prepare record to add
$record = array(
'h_name' => $name,
'h_date_begin' => $beginning_date_rang1,
'h_date_end' => $ending_date_rang1,
);
// Add the record
$res = drupal_write_record('my_horoscope_structure', $record);
if($res != FALSE) {
drupal_set_message(t('Horoscope #name is inserted successfully!', array('#name' => $name)));
}
}
// return $form
return $form['settings']['horoscopes']['add_horoscopes']['adding_horoscope_wrapper'];
}
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().
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',
)),
))); ?>
I am writing a custom module which has a form but the submit handler is just not working, the form just submits back to itself?
Any help much appreciated.
I have an add guest custom form and that is working fine.
The URL on my site is: /user/booking_editguest/LBD0413/1
The code is below:
function uUserBookings_editGuestForm($form, &$form_state, $ManageBooking) {
//var_dump($ManageBooking);
$guestSeq = arg(3);
$masterEventCode = $ManageBooking->Contact->{'MasterEventCode'};
$eventCode = $ManageBooking->Contact->{'EventCode'};
$attendeeContact = $ManageBooking->Contact->{'AttendeeContact'};
global $user;
$account = user_load($user->uid);
$memberCode = $account->name;
// booking
$form['booking'] = array(
//'#type' => 'vertical_tabs',
);
foreach($ManageBooking->guests as $Guest)
{
if ($guestSeq == $Guest->{'GuestSeq'} )
{
$form['guest_form']['FirstName'] = array(
'#required' => TRUE,
'#type' => 'textfield',
'#title' => t('GUEST FIRST NAME'),
'#default_value' => $Guest->{'Guest FirstName'}
);
$form['guest_form']['Surname'] = array(
'#required' => TRUE,
'#type' => 'textfield',
'#title' => t('GUEST LAST NAME'),
'#default_value' => $Guest->{'Guest Surname'}
);
$form['guest_form']['DietaryRequirements'] = array(
'#required' => FALSE,
'#type' => 'textfield',
'#title' => t('SPECIAL DIETARY REQUIREMENTS'),
'#default_value' => $Guest->{'Dietary Requirements'}
);
$form['guest_form']['CompanyName'] = array(
'#required' => TRUE,
'#type' => 'textfield',
'#title' => t('GUEST COMPANY NAME'),
'#default_value' => $Guest->{'Company Name'}
);
$form['guest_form']['Position'] = array(
'#required' => TRUE,
'#type' => 'textfield',
'#title' => t('GUEST POSITION'),
'#default_value' => $Guest->{'Attendee Position'}
);
$form['guest_form']['Email'] = array(
'#required' => TRUE,
'#type' => 'textfield',
'#title' => t('GUEST EMAIL'),
'#default_value' => $Guest->{'Guest Email'}
);
//MasterEventCode
$form['guest_form']['masterEventCode'] = array(
'#required' => TRUE,
'#type' => 'hidden',
'#default_value' => $masterEventCode
);
//EventCode
$form['guest_form']['eventCode'] = array(
'#required' => TRUE,
'#type' => 'hidden',
'#default_value' => $eventCode
);
//Member_code
$form['guest_form']['memberCode'] = array(
'#required' => TRUE,
'#type' => 'hidden',
'#default_value' => $memberCode
);
//Attendee_Contact
$form['guest_form']['Attendee_Contact'] = array(
'#required' => TRUE,
'#type' => 'hidden',
'#default_value' => $attendeeContact
);
//GuestSeq
$form['guest_form']['GuestSeq'] = array(
'#required' => TRUE,
'#type' => 'hidden',
'#default_value' => $Guest->{'GuestSeq'}
);
//GuestID
$form['guest_form']['GuestID'] = array(
'#required' => TRUE,
'#type' => 'hidden',
'#default_value' => $Guest->{'Guest Contact Counter'}
);
//EventNameDetailsID uniqueidentifier
$form['guest_form']['EventNameDetailsID'] = array(
'#required' => TRUE,
'#type' => 'hidden',
'#default_value' => $Guest->{'EventNameDetailsID'}
);
//ID uniqueidentifier
$form['guest_form']['ID'] = array(
'#required' => TRUE,
'#type' => 'hidden',
'#default_value' => $Guest->{'ID'}
);
$form['guest_form']['Submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
'#submit' => array('tmsUserBookings_editGuestForm_submit')
);
}
}
return $form;}
function userBookings_editGuestForm_validate($form, &$form_state) {
// Validation logic.
// Don't custom-validate if previous validation errors (still) exist
if (form_get_errors()) return;
// .. Otherwise, process custom form validation goes here }
function userBookings_editGuestForm_submit($form, &$form_state) {
//Get Form variables
$guestFirstname = $form_state['input']['FirstName'];
$guestSurname = $form_state['input']['Surname'];
$guestDietary = $form_state['input']['DietaryRequirements'];
$guestCompany = $form_state['input']['CompanyName'];
$guestPosition = $form_state['input']['Position'];
$guestEmail = $form_state['input']['Email'];
$memberCode = $form_state['input']['Member_code'];
$masterEventCode = $form_state['input']['MasterEventCode'];
$eventCode = $form_state['input']['EventCode'];
$bookerContactCounter = $form_state['input']['Attendee_Contact'];
$guestSeq = $form_state['input']['GuestSeq'];
$guestTitle = $form_state['input']['Title'];
$guestContactCounter = $form_state['input']['GuestID'];
$eventNameDetailsId = $form_state['input']['EventNameDetailsID'];
$id = $form_state['input']['ID'];
//Redirect back to the Booking.
$form_state['redirect'] = '/user/booking_guestupdated';}
For those using hidden textfields and have them set as required, make sure you check the values being entered!