Drupal7 Custom Form Submit Handler Not Working - forms

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!

Related

Muliple Dropdown Select option in Drupal8 Custom Form

I have to design the form with following fields
Country
State
District
If you select Country the Country value will be loaded in the Country Dropdown Field
Then if you select State the States belongs to Countries value will be loaded in the State Dropdown.
After that If you select District the Districts value belongs to States will be loaded in the District Dropdown field.
I have tried but not work properly
$form['Country'] = [
'#type' => 'select',
'#title' => t('Country'),
'#description' => t('Country'),
'#required' => TRUE,
'#options' => $countries,
// '#default_value' => setInConfigandGEtVAlue,
'#ajax' => ['callback' => [$this, 'getStates'], 'event' => 'change',
'method' => 'html',
'wrapper' => 'states-to-update',
'progress' => [
'type' => 'throbber',
'message' => NULL,
],
],
];
$states = [];
// if($default_country != "") { load default states of selected country by get the default va
lue of country (setInConfigandGEtVAlue)
// $states = $this->getStatesByCountry($default_country);
// }
$form['state'] = array(
'#title' => t('State'),
'#type' => 'select',
'#description' => t('Select the state'),
'#options' => $states,
// '#default_value' => setInConfigandGEtVAlue,
'#attributes' => ["id" => 'states-to-update'],
'#multiple' => TRUE,
'#validated' => TRUE
);
$district_opt_records = [];
$form['districts'] = array(
'#type' => 'select',
'#title' => t('District Name'),
'#options' => $district_opt_records,
'#required' => TRUE,
'#attributes' => ["id" => 'district-to-update'],
'#validated' => TRUE,);
public function getStates(array &$element, FormStateInterface $form_state) {
$triggeringElement = $form_state->getTriggeringElement();
$value = $triggeringElement['#value'];
$states = $this->getStatesByCountry($value);
$wrapper_id = $triggeringElement["#ajax"]["wrapper"];
$renderedField = '';
foreach ($states as $key => $value) {
$renderedField .= "<option value='".$key."'>".$value."</option>";
}
$response = new AjaxResponse();
$response->addCommand(new HtmlCommand("#".$wrapper_id, $renderedField));
return $response;
}
public function getStatesByCountry($default_country) {
//add you logic return states by country
return $states;
}
The above code working only Countries and State but not able to get District values

My ajax Drupal form shows up old values

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'];
}

Creating drupal form with wrappers

I have created a form in drupal programmatically. How could i wrap each elements of the form with div. Below is my sample code
function emailusers_compose_form($context, $account) {
$form['to'] = array(
'#type' => 'value',
'#value' => $account,
);
$form['message']['subject'] = array(
'#type' => 'textfield',
'#title' => t('Subject'),
'#size' => 50,
'#maxlengh' => 255,
'#description' => t('The subject of the email message.'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Send Mail'),
);
return $form;
}
Here is the answer
function emailusers_compose_form($context, $account) {
$form['to'] = array(
'#type' => 'value',
'#value' => $account,
);
$form['message']['subject'] = array(
'#type' => 'textfield',
'#title' => t('Subject'),
'#size' => 50,
'#maxlengh' => 255,
'#description' => t('The subject of the email message.'),
'#prefix' => '<div id="elementid">',
'#suffix' => '</div>'
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Send Mail'),
);
return $form;
}

How to populate multi array in zend form

I am trying to populate all the result from DB to zend form but cannot work.
This is my form
$configsForm= new Zend_Dojo_Form_SubForm();
$configsForm->setAttribs(array(
'name' => 'mandatory',
'legend' => 'mandatory',
'dijitParams' => array(
'title' => $this-> view -> __ ( 'Configs' ),
)
));
$configsForm->addElement(
'FilteringSelect',
'group_id',
array(
'label' => $this-> view -> __ ( 'Configs_Group Key' ),
'required' => true,
'value' => '',
'multiOptions' => $this->_getConfigOptions(),
'id' => 'group_id',
)
);
$configsForm->addElement(
'ValidationTextBox',
'option_type',
array(
'label' => $this-> view -> __ ( 'Configs_Option Type If Exist' ),
'trim' => true,
'required' => false,
'name' => 'option_type',
'id' => 'option_type',
'class' => 'lablvalue jstalgntop',
)
);
$configsForm->addElement(
'ValidationTextBox',
'option_title',
array(
'label' => $this-> view -> __ ( 'Configs_Option Title' ),
'trim' => true,
'required' => true,
'class' => 'lablvalue jstalgntop',
)
);
$configsForm->addElement(
'ValidationTextBox',
'option_value',
array(
'label' => $this-> view -> __ ( 'Configs_Option Value' ),
'trim' => true,
'required' => true,
'class' => 'lablvalue jstalgntop',
)
);
$configsForm->addElement(
'select',
'option_status',
array(
'label' => $this-> view -> __('Configs_Option Status'),
'required' => true,
'value' => '',
'multiOptions' => array('' => $this -> view -> __('Root'), 0 => 'Disabel', 1 => 'Enabel'),
)
);
$configsForm->addElement(
'FilteringSelect',
'locale_id',
array(
'label' => $this-> view -> __ ( 'Configs_Locale' ),
'class' => 'lablvalue jstalgntop',
'autocomplete'=>false,
'required' => true,
'multiOptions' => $this->_getLocaleOptions(),
'id' => 'locale_id',
)
);
$configsForm->addElement(
'ValidationTextBox',
'option_hint',
array(
'label' => $this-> view -> __ ( 'Configs_Option Hint' ),
'trim' => true,
'required' => false,
'class' => 'lablvalue jstalgntop',
)
);
$configsForm->addElement(
'ValidationTextBox',
'option_description',
array(
'label' => $this-> view -> __ ( 'Configs_Option Description' ),
'trim' => true,
'required' => false,
'class' => 'lablvalue jstalgntop',
)
);
$configsForm->addElement(
'ValidationTextBox',
'comments',
array(
'label' => $this-> view -> __ ( 'Configs_Option Comments' ),
'trim' => true,
'class' => 'lablvalue jstalgntop',
)
);
$configsForm->addElement(
'hidden',
'id'
);
$configsForm->addElement(
'SubmitButton',
'submit',
array(
'value' => 'submit',
'label' => $this-> view -> __ ( 'Object_Save' ),
'type' => 'Submit',
'ignore' => true,
'onclick' => 'dijit.byId("add-edit").submit()',
)
);
$configsForm->addElement(
'reset',
'reset',
array(
'label' => 'Reset',
'id' => 'reset',
'ignore'=> true,
)
);
$configsForm ->setDecorators ( array ('FormElements', array ('HtmlTag', array ('tag' => 'table', 'class'=>'formlist' ) ), 'ContentPane' ) );
$configsForm->setElementDecorators(array(
'DijitElement',
'Errors',
array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'lable jstalgntop')),
array('Label', array('tag' => 'td', 'class' => 'lable jstalgntop')),
array(array('row' => 'HtmlTag'), array('tag' => 'tr')),
));
$this->addSubForm($configsForm, 'configs');
}
And the code in my controller looks like
$form -> populate($configsObjResults);
The $configsObjResults containes
Array
(
[0] => Array
(
[id] => 11
[group_id] => 2
[group_key] => advanced
[option_title] => advanced.iptable.status
)
[1] => Array
(
[id] => 12
[group_id] => 2
[group_key] => advanced
[option_title] => advanced.memchache.iptable
)
)
Put your subform code in a method called addConfigSubForm() within your form code, like so:
class myForm extends Zend_Dojo_Form
{
...
// $number - 1st, 2nd, 3rd... subform
// $data - data to populate
public function addConfigSubForm($number, $data)
{
[ Create your subform here ]
// populate it with $data
$configsForm->populate($data);
// add it to the form
$this->addSubForm($configsForm, 'configs' . $i);
}
}
Then in your controller, do the following:
$myform = new myForm();
foreach ($configsObjResults as $i=>$config) {
$myform->addConfigSubForm($i, $config);
}
This will add a subform for each configs object in your array.

zend framework upload image false extension file upload

This is my code for a form and i am getting an error when i am uploading :"File 'swing-layout-1.0.4-src.zip' has a false extension"
<?php
class Admin_Form_Banner extends ZendX_Form_Designed {
public function init() {
$this->setEnctype(self::ENCTYPE_MULTIPART);
$this->setMethod(self::METHOD_POST);
$this->setMethod('post');
// Add an email element
$this->addElement('text', 'banner_title', array(
'label' => 'Banner Title',
'required' => true,
'filters' => array('StringTrim')
));
$this->addElement('text', 'banner_type', array(
'label' => 'Banner Type',
'required' => true,
'filters' => array('StringTrim')
));
$this->addElement('checkbox', 'is_active', array(
'label' => 'Is Active',
'required' => true,
'filters' => array('StringTrim')
));
$banner_position = new Zend_Form_Element_Select('banner_position');
$banner_position->setMultiOptions($this->getBannerPositions())->setLabel('Banner Position');
$this->addElement($banner_position, 'banner_position');
$file = new Zend_Form_Element_File('file');
$file->addValidator('Count', FALSE, 1);
$file->addValidator('Size', FALSE, 67633152);
$file->addValidator('Extension', false,'.zip,rar');
$file->setRequired(FALSE);
$file->setAllowEmpty(false);
$this->addElement($file, 'file_path', array('label' => 'Attacehd file'));
$this->addElement('submit', 'submit', array(
'ignore' => true,
'label' => ''
));
}
public function getBannerPositions() {
$db = Zend_Db_Table::getDefaultAdapter();
$bannerPosition = $db->fetchPairs($db
->select()
->from('banner_position'), array('id', 'banner_position'));
return $bannerPosition;
}
}
I think there should be 'zip,rar' rather than '.zip,rar'.