Ajax replace method does not working error - ajax

Ajax replace method doesn't working in these code.But append method is working.
<?php
$currentDate = date("Y-m-d");
$currentTime = date("Y-m-d");
global $user;
function form_test_permission()
{
return array(
'submit_form_test' =>array(
'title' =>t('Submit_form_test'),
'description' => t('Submit the form_test form'),
),
);
}
function form_test_menu() {
$items = array();
$items['production'] = array(
'title' =>'Production',
'type' => MENU_NORMAL_ITEM,
'access arguments' => array('submit_form_test'),
'page callback' => 'drupal_get_form',
'page arguments' => array('form_test_form'),
);
return $items;
}
function form_test_form($form,&$form_state) {
$form['production'] = array(
'#title' => t('production'),
'#type' => 'hidden',
'#value'=> '1',
);
$form['production1'] = array(
'#title' => t('production1'),
'#type' => 'hidden',
);
$form['production_date'] = array(
'#title' => t('production_date'),
'#type' => 'hidden',
'#value' => date('Y-m-d'),
);
$form['production_time'] = array(
'#title' => t('production_time'),
'#type' => 'hidden',
'#value' => date('H:i:s'),
);
$form['production23'] = array(
'#title' => t('production23'),
'#type' => 'hidden',
'#value'=> '1',
);
$form['button2'] = array(
'#value' => 'UNDO',
'#type' => 'submit',
'#prefix' => '<div class="test">',
'#suffix' => '</div>',
'#submit' => array('form_test_form_button2'),
);
$form['button1'] = array(
'#value' => ' ADD ',
'#type' => 'submit',
'#prefix' => '<div class="test1">',
'#suffix' => '</div>',
'#submit' => array('form_test_form_button1'),
'#ajax' => array(
'callback' => 'ajax_example_submit_driven_callback',
'method' => 'replace',
'effect' => 'fade',
'event' =>'click',
'wrapper' => 'countvalue',
),
);
$form['start_counter'] = array(
'#type' => 'select',
'#options' => array (
'Start' => 'Start',
'2501 ' => '2501 ',
'302 ' => '302 ',
'1107 ' => '1107',
'1104' => '1104',
'0106' => '0106',
'0305' => '0305',
'0103' => '0103',
),
'#prefix' => ' <div class="box chart gradient" style="width:30%;float:left;margin-top: -186px;">
<div class="title">
<h4>
<span style="text-align:center;">Downtime</span>
</h4>
</div>
<div class="content" style="padding-bottom:0;">
<p class="dowtime-show" id="counter"></p>',
);
$form['start_counter1'] = array(
'#type' => 'textfield',
);
$form['stop_counter'] = array(
'#type' => 'select',
'#options' => array (
'Stop'=>'Stop',
'Mechanical' => 'Mechanical',
'Electrical' => 'Electrical',
'Programming' => 'Programming',
'Setup' => 'Setup',
'PlannedMaintenance' => 'PlannedMaintenance'
),
'#suffix' => '</div> </div>',
);
$form['counter'] = array(
'#type' => 'textfield',
);
$form['button3'] = array(
'#value' => 'submit',
'#type' => 'submit',
'#submit' => array('form_test_form_button3'),
);
return $form;
}
function ajax_example_submit_driven_callback($form,$form_state){
$result = db_query("SELECT * FROM form_test WHERE production_date='2014-02-10' and production_time between '01:00:00' and '02:00:00'");
$prod_1= $result->rowcount();
$form['#tree'] = TRUE;
$form_state['rebuild'] = TRUE;
return 'count value is:'.$prod_1.'';
}
function form_test_form_button1($form,&$form_state){
$ft_id = db_insert('form_test')
->fields(array(
'production' => $form_state['values']['production'],
'production_date' => $form_state['values']['production_date'],
'production_time' => $form_state['values']['production_time'],
'production1' => $form_state['values']['production1'],
))
->execute();
$form_state['rebuild'] = true;
}
function form_test_form_button2($form,&$form_state){
$ft_id = db_insert('form_test1')
->fields(array(
'production23' => $form_state['values']['production23'],
'production_date' => $form_state['values']['production_date'],
'production_time' => $form_state['values']['production_time'],
))
->execute();
}
function form_test_form_button3($form,&$form_state){
$ft_id = db_insert('downtime1')
->fields(array(
'start_counter1' => $form_state['values']['start_counter1'],
'stop_counter' => $form_state['values']['stop_counter'],
'counter' => $form_state['values']['counter'],
'production_date' => $form_state['values']['production_date'],
'production_time' => $form_state['values']['production_time'],
))
->execute();
}
I spend a lot of hours to get the result but cannot able to get it.Anybody know how to solve these kind of Errors.The main problem is that it doesn't change the value while using replace method.

Related

Drupal 8 add ajax form element after ajax callback

I am building a drupal form with multiple ajax enabled form elements.
I have one select list that does an ajax callback after change. The problem is that it adds a new select list to the page, which is also ajax enabled. This does not seem to work, which seems logical to me because the ajax is actually bundled an added to the page so it is lost in the replacecommand.
Is there anyone experienced with this, and does anyone have a solution ?
This is my code
/**
* {#inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state)
{
$form['city'] = [
'#type' => 'select',
'#title' => $this->t('Station'),
'#description' => $this->t('City'),
'#options' => array(
'Aalst' => $this->t('Aalst'),
'Brussel' => $this->t('Brussel'),
'Hasselt' => $this->t('Hasselt'),
'Leuven' => $this->t('Leuven'),
),
'#ajax' => [
'callback' => array($this, 'extendFormAjax'),
'event' => 'change',
'progress' => array(
'type' => 'throbber',
'message' => t('Choose City'),
),
],
'#suffix' => '<div id="extended-form"></div>',
];
$form['submit'] = [
'#type' => 'submit',
'#value' => t('Submit'),
];
return $form;
}
/**
* Ajax callback to validate the email field.
*/
public function extendFormAjax(array &$form, FormStateInterface $form_state)
{
$parking = [
'#type' => 'select',
'#title' => $this->t('Parking'),
'#description' => $this->t('Parking'),
'#options' => [
'P1' => $this->t('P1'),
'P2' => $this->t('P2'),
],
'#ajax' => [
'callback' => array($this, 'extendFormAjax'),
'event' => 'change',
'progress' => array(
'type' => 'throbber',
'message' => t('Choose parking'),
),
],
];
$response = new AjaxResponse();
$response->addCommand(new InsertCommand('#extended-form', $parking));
return $response;
}
I had a similar issue and i resolved it by adding the element in buildForm and adding a wrapper for it and sending the form element via HtmlCommand
/**
* {#inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state)
{
$form['city'] = [
'#type' => 'select',
'#title' => $this->t('Station'),
'#description' => $this->t('City'),
'#options' => array(
'Aalst' => $this->t('Aalst'),
'Brussel' => $this->t('Brussel'),
'Hasselt' => $this->t('Hasselt'),
'Leuven' => $this->t('Leuven'),
),
'#ajax' => [
'callback' => array($this, 'extendFormAjax'),
'event' => 'change',
'progress' => array(
'type' => 'throbber',
'message' => t('Choose City'),
),
],
];
$form['parking'] = [
'#prefix' => '<div id="extended-form">',
'#suffix' => '</div>',
'#type' => 'select',
'#title' => $this->t('Parking'),
'#description' => $this->t('Parking'),
'#options' => [
'P1' => $this->t('P1'),
'P2' => $this->t('P2'),
],
'#ajax' => [
'callback' => array($this, 'extendFormAjax'),
'event' => 'change',
'progress' => array(
'type' => 'throbber',
'message' => t('Choose parking'),
),
],
];
$form['submit'] = [
'#type' => 'submit',
'#value' => t('Submit'),
];
return $form;
}
/**
* Ajax callback to validate the email field.
*/
public function extendFormAjax(array &$form, FormStateInterface $form_state)
{
$parking = [
'#type' => 'select',
'#title' => $this->t('Parking'),
'#description' => $this->t('Parking'),
'#options' => [
'P1' => $this->t('P1'),
'P2' => $this->t('P2'),
],
'#ajax' => [
'callback' => array($this, 'extendFormAjax'),
'event' => 'change',
'progress' => array(
'type' => 'throbber',
'message' => t('Choose parking'),
),
],
];
$response = new AjaxResponse();
$response->addCommand(new HtmlCommand('#extended-form', $parking));
return $response;
}
Try it like this. I have not tested the code.
Try call somewhere in JS Drupal.attachBehaviors();
I experienced this issue and resolved it by this way :
For each element which were populated by Ajax, I add the property "#validated" => true and in the callback, the returned field must have the same attributes (id, name) that the original field :
$form['example_field'] = array(
'#type' => 'select',
'#required' => FALSE,
'#options' => getDynamicOptions(),
'#prefix' => '<div id="etablissement-type-wrapper">',
'#suffix' => '</div>',
'#attributes' => [
'data-drupal-selector' => "edit-example",
'id' => "edit-example",
'name' => "example",
],
'#validated' => TRUE,
);
You need add ajax elements in buildForm methods and rebuild form. Something like this:
/**
* {#inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, $no_js_use = FALSE) {
// We want to deal with hierarchical form values.
$form['#tree'] = TRUE;
$form['step'] = [
'#type' => 'value',
'#value' => !empty($form_state->getValue('step')) ? $form_state->getValue('step') : 1,
];
switch ($form['step']['#value']) {
case 1:
$limit_validation_errors = [['step']];
$form['step1'] = [
'#type' => 'fieldset',
'#title' => $this->t('Step 1: Personal details'),
];
$form['step1']['name'] = [
'#type' => 'textfield',
'#title' => $this->t('Your name'),
'#default_value' => $form_state->hasValue(['step1', 'name']) ? $form_state->getValue(['step1', 'name']) : '',
'#required' => TRUE,
];
break;
case 2:
$limit_validation_errors = [['step'], ['step1']];
$form['step1'] = [
'#type' => 'value',
'#value' => $form_state->getValue('step1'),
];
$form['step2'] = [
'#type' => 'fieldset',
'#title' => t('Step 2: Street address info'),
];
$form['step2']['address'] = [
'#type' => 'textfield',
'#title' => $this->t('Your street address'),
'#default_value' => $form_state->hasValue(['step2', 'address']) ? $form_state->getValue(['step2', 'address']) : '',
'#required' => TRUE,
];
break;
case 3:
$limit_validation_errors = [['step'], ['step1'], ['step2']];
$form['step1'] = [
'#type' => 'value',
'#value' => $form_state->getValue('step1'),
];
$form['step2'] = [
'#type' => 'value',
'#value' => $form_state->getValue('step2'),
];
$form['step3'] = [
'#type' => 'fieldset',
'#title' => $this->t('Step 3: City info'),
];
$form['step3']['city'] = [
'#type' => 'textfield',
'#title' => $this->t('Your city'),
'#default_value' => $form_state->hasValue(['step3', 'city']) ? $form_state->getValue(['step3', 'city']) : '',
'#required' => TRUE,
];
break;
}
$form['actions'] = ['#type' => 'actions'];
if ($form['step']['#value'] > 1) {
$form['actions']['prev'] = [
'#type' => 'submit',
'#value' => $this->t('Previous step'),
'#limit_validation_errors' => $limit_validation_errors,
'#submit' => ['::prevSubmit'],
'#ajax' => [
'wrapper' => 'ajax-example-wizard-wrapper',
'callback' => '::prompt',
],
];
}
if ($form['step']['#value'] != 3) {
$form['actions']['next'] = [
'#type' => 'submit',
'#value' => $this->t('Next step'),
'#submit' => ['::nextSubmit'],
'#ajax' => [
'wrapper' => 'ajax-example-wizard-wrapper',
'callback' => '::prompt',
],
];
}
if ($form['step']['#value'] == 3) {
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this->t("Submit your information"),
];
}
$form['#prefix'] = '<div id="ajax-example-wizard-wrapper">';
$form['#suffix'] = '</div>';
return $form;
}
public function prompt(array $form, FormStateInterface $form_state) {
return $form;
}
public function nextSubmit(array $form, FormStateInterface $form_state) {
$form_state->setValue('step', $form_state->getValue('step') + 1);
$form_state->setRebuild();
return $form;
}
public function prevSubmit(array $form, FormStateInterface $form_state) {
$form_state->setValue('step', $form_state->getValue('step') - 1);
$form_state->setRebuild();
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$messenger = \Drupal::messenger();
$messenger->addMessage($this->t('Your information has been submitted:'));
}

filter_condition_callback not working in Magento observer

I am working on a custom extension, when i use filter_condition_callback in observer inside addColumnAfter() its not working kindly give me some solution to this.
class CustomGrid_GeneProductGrid_Model_Observer
{
public function beforeCollectionLoad(Varien_Event_Observer $observer)
{
$collection = $observer->getCollection();
if (!isset($collection)) {
return;
}
if ($collection instanceof Mage_Catalog_Model_Resource_Product_Collection) {
$store = Mage::app()->getRequest()->getParam('store');
$collection->joinAttribute('special_price', 'catalog_product/special_price', 'entity_id', null, 'left', $store);
$collection->addAttributeToSelect('cost');
$collection->addAttributeToSelect('m_cost');
}
}
public function customGridHtmlBefore(Varien_Event_Observer $observer)
{
$store = Mage::app()->getRequest()->getParam('store');
$grid = $observer->getBlock();
if ($grid instanceof Mage_Adminhtml_Block_Catalog_Product_Grid) {
if ($store == '') {
$grid->addColumnAfter(
'expend_stores', array(
'header' => 'Expend Stores',
'filter' => false,
//'index' => 'color',
'width' => '25px',
'renderer' => 'customgrid_geneproductgrid/adminhtml_product_ExpendCollapse',
'align' => 'center',
), 'massaction'
);
}
$storeId = (int)Mage::app()->getRequest()->getParam('store', 0);
$store = Mage::app()->getStore($storeId);
$grid->addColumnAfter(
'cost', array(
'header' => 'V-Cost',
'index' => 'cost',
'type' => 'price',
'currency_code' => $store->getBaseCurrency()->getCode(),
'width' => '25px',
'align' => 'center',
), 'sku'
);
$grid->addColumnAfter(
'm_cost', array(
'header' => 'M-Cost',
'index' => 'm_cost',
'type' => 'price',
'currency_code' => $store->getBaseCurrency()->getCode(),
'width' => '25px',
'align' => 'center',
), 'cost'
);
$grid->addColumnAfter(
'special_price', array(
'header' => 'Special Price',
'index' => 'special_price',
'type' => 'price',
'currency_code' => $store->getBaseCurrency()->getCode(),
'width' => '25px',
'align' => 'center',
), 'price'
);
$grid->addColumnAfter('accessories_category_list', array(
'header' => Mage::helper('catalog')->__('Category'),
'index' => 'accessories_category_list',
'sortable' => false,
'type' => 'options',
'options' => Mage::getSingleton('customgrid_geneproductgrid/system_config_source_Category')->toOptionArray(),
'renderer' => 'customgrid_geneproductgrid/adminhtml_catalog_product_grid_render_category',
'filter_condition_callback' => array(Mage::getSingleton('customgrid_geneproductgrid/observer'), 'filterCallback'),
), 'set_name');
$grid->sortColumnsByOrder();
}
}
public function filterCallback($collection, $column)
{
$value = $column->getFilter()->getValue();
$_category = Mage::getModel('catalog/category')->load($value);
$collection->addCategoryFilter($_category);
return $collection;
}
}

Codeigniter: Display multiple images from database for

I'm running a real estate website. I'm trying to list all images in the database for a particular listing on their respective page. I have the following code but it just repeats the same default image instead of showing all images for that listing.
Model:
public function getDetails($listing_id)
{
$this->db->select(
'listings.listing_id AS id,
listings.account_id AS account_id,
listings.nbrhood_id AS nbrhood_id,
listings.price AS price,
listings.convertible AS convertible,
listings.available AS available,
images.file_name AS file_name,
listings.sqft AS sqft,
listings.db_tag AS db_tag,
listings.web_id AS web_id,
listings.description AS description,
bedrooms.beds_num AS beds_val,
bedrooms.beds_name AS beds_text,
bathrooms.baths_num AS baths_val,
bathrooms.baths_name AS baths_text,
nbrhoods.nbrhood_name AS nbrhood,
nbrhoods.image AS nbrhood_image,
types_listings.type_name AS type,
pets.pet_name AS pets'
);
$this->db->join('images', 'images.image_id = listings.image_id', 'left');
$this->db->join('bedrooms', 'bedrooms.beds_id = listings.beds_id');
$this->db->join('bathrooms', 'bathrooms.baths_id = listings.baths_id');
$this->db->join('nbrhoods', 'nbrhoods.nbrhood_id = listings.nbrhood_id');
$this->db->join('types_listings', 'types_listings.type_id = listings.type_id');
$this->db->join('pets', 'pets.pet_id = listings.pet_id');
$this->db->where('listings.listing_id', $listing_id);
$this->db->limit(1);
return $this->db->get($this->table_name)->row();
}
PHP:
<?php foreach ($listing AS $listing_id): ?>
<li data-image-id="<?=$image -> image_id ?>" class="uploaded-image" id="image_<?=$image -> image_id ?>">
<div class="sortableimagewrapper">
<!-- <span class="label label-inverse image-name"> </span> -->
<img src="<?=images_url('110x68/' . $listing -> file_name) ?>" alt="" width="110px" height="68px" class="img-polaroid" />
</div>
</li>
<?php endforeach; ?>
class Listing_Model extends CW_Model {
protected $table_name = 'listings';
protected $order_by = 'listing_id';
protected $primary_key = 'listing_id';
protected $display_val = '';
public $defaults = array(
'status_id' => 1
);
public $qualifiers = array('listing_id', 'account_id', 'status_id', 'image_id', 'file_name', 'featured', 'feature_id', 'deal_id', 'nbrhood_id', 'beds_id', 'baths_id', 'pet_id', 'type_id', 'owner_id', 'web_id', 'price', 'convertible', 'available', 'sqft', 'address', 'unit', 'zip', 'description', 'notes');
public $rules = array(
'new' => array(
'listing_id' => array(
'field' => 'listing_id',
'label' => '',
'rules' => 'trim|required|xss_clean'
),
'account_id' => array(
'field' => 'account_id',
'label' => '',
'rules' => 'trim|required|xss_clean'
),
'status_id' => array(
'field' => 'status_id',
'label' => '',
'rules' => 'trim|required|xss_clean'
),
'image_id' => array(
'field' => 'image_id',
'label' => '',
'rules' => 'trim|xss_clean'
),
'file_name' => array(
'field' => 'file_name',
'label' => '',
'rules' => 'trim|xss_clean'
),
'featured' => array(
'field' => 'featured',
'label' => '',
'rules' => 'trim|xss_clean'
),
'feature_id' => array(
'field' => 'feature_id',
'label' => '',
'rules' => 'trim|xss_clean'
),
'deal_id' => array(
'field' => 'deal_id',
'label' => '',
'rules' => 'trim|xss_clean'
),
'nbrhood_id' => array(
'field' => 'nbrhood_id',
'label' => 'Neighborhood',
'rules' => 'trim|required|xss_clean'
),
'beds_id' => array(
'field' => 'beds_id',
'label' => 'Bedrooms',
'rules' => 'trim|required|xss_clean'
),
'baths_id' => array(
'field' => 'baths_id',
'label' => 'Bathrooms',
'rules' => 'trim|required|xss_clean'
),
'pet_id' => array(
'field' => 'pet_id',
'label' => 'Pets',
'rules' => 'trim|required|xss_clean'
),
'type_id' => array(
'field' => 'type_id',
'label' => 'Listing Type',
'rules' => 'trim|required|xss_clean'
),
'owner_id' => array(
'field' => 'owner_id',
'label' => 'Owner/Landlord',
'rules' => 'trim|required|xss_clean'
),
'web_id' => array(
'field' => 'web_id',
'label' => 'Web ID',
'rules' => 'trim|alpha_dash|xss_clean'
),
'price' => array(
'field' => 'price',
'label' => 'Price',
'rules' => 'trim|required|min_length[3]|integer|xss_clean'
),
'convertible' => array(
'field' => 'convertible',
'label' => 'Convertible',
'rules' => 'trim|is_natural|xss_clean'
),
'dateAvailable' => array(
'field' => 'dateAvailable',
'label' => '',
'rules' => 'trim|xss_clean'
),
'available' => array(
'field' => 'available',
'label' => 'Date Available',
'rules' => 'trim|required|xss_clean'
),
'sqft' => array(
'field' => 'sqft',
'label' => 'Square Feet',
'rules' => 'trim|min_length[3]|integer|xss_clean'
),
'address' => array(
'field' => 'address',
'label' => 'Street Address',
'rules' => 'trim|required|min_length[12]|xss_clean'
),
'unit' => array(
'field' => 'unit',
'label' => 'Unit Number',
'rules' => 'trim|required|max_length[5]|alpha_dash|xss_clean'
),
'zip' => array(
'field' => 'zip',
'label' => 'Zip Code',
'rules' => 'trim|required|exact_length[5]|integer|xss_clean'
),
'description' => array(
'field' => 'description',
'label' => 'Description',
'rules' => 'trim|required|min_length[175]|xss_clean'
),
'notes' => array(
'field' => 'notes',
'label' => 'Notes',
'rules' => 'trim|xss_clean'
),
'amenity_ids[unit][]' => array(
'field' => 'amenity_ids[unit][]',
'label' => 'Unit Amenities',
'rules' => 'trim|xss_clean'
),
'amenity_ids[property][]' => array(
'field' => 'amenity_ids[property][]',
'label' => 'Property Amenities',
'rules' => 'trim|xss_clean'
),
'image_ids[]' => array(
'field' => 'image_ids[]',
'label' => '',
'rules' => 'trim|xss_clean'
)
),
'edit' => array(
'listing_id' => array(
'field' => 'listing_id',
'label' => '',
'rules' => 'trim|required|xss_clean'
),
'account_id' => array(
'field' => 'account_id',
'label' => '',
'rules' => 'trim|required|xss_clean'
),
'status_id' => array(
'field' => 'status_id',
'label' => '',
'rules' => 'trim|required|xss_clean'
),
'image_id' => array(
'field' => 'image_id',
'label' => '',
'rules' => 'trim|xss_clean'
),
'file_name' => array(
'field' => 'file_name',
'label' => '',
'rules' => 'trim|xss_clean'
),
'featured' => array(
'field' => 'featured',
'label' => '',
'rules' => 'trim|xss_clean'
),
'feature_id' => array(
'field' => 'feature_id',
'label' => '',
'rules' => 'trim|xss_clean'
),
'deal_id' => array(
'field' => 'deal_id',
'label' => '',
'rules' => 'trim|xss_clean'
),
'nbrhood_id' => array(
'field' => 'nbrhood_id',
'label' => 'Neighborhood',
'rules' => 'trim|required|xss_clean'
),
'price' => array(
'field' => 'price',
'label' => 'Price',
'rules' => 'trim|required|min_length[3]|integer|xss_clean'
),
'beds_id' => array(
'field' => 'beds_id',
'label' => 'Bedrooms',
'rules' => 'trim|required|xss_clean'
),
'convertible' => array(
'field' => 'convertible',
'label' => 'Convertible',
'rules' => 'trim|is_natural|xss_clean'
),
'baths_id' => array(
'field' => 'baths_id',
'label' => 'Bathrooms',
'rules' => 'trim|required|xss_clean'
),
'dateAvailable' => array(
'field' => 'dateAvailable',
'label' => '',
'rules' => 'trim|xss_clean'
),
'available' => array(
'field' => 'available',
'label' => 'Date Available',
'rules' => 'trim|required|xss_clean'
),
'pet_id' => array(
'field' => 'pet_id',
'label' => 'Pets',
'rules' => 'trim|required|xss_clean'
),
'sqft' => array(
'field' => 'sqft',
'label' => 'Square Feet',
'rules' => 'trim|min_length[3]|integer|xss_clean'
),
'type_id' => array(
'field' => 'type_id',
'label' => 'Listing Type',
'rules' => 'trim|required|xss_clean'
),
'description' => array(
'field' => 'description',
'label' => 'Description',
'rules' => 'trim|required|min_length[175]|xss_clean'
),
'amenity_ids[unit][]' => array(
'field' => 'amenity_ids[unit][]',
'label' => 'Unit Amenities',
'rules' => 'trim|xss_clean'
),
'amenity_ids[property][]' => array(
'field' => 'amenity_ids[property][]',
'label' => 'Property Amenities',
'rules' => 'trim|xss_clean'
),
'owner_id' => array(
'field' => 'owner_id',
'label' => 'Owner/Landlord',
'rules' => 'trim|required|xss_clean'
),
'address' => array(
'field' => 'address',
'label' => 'Street Address',
'rules' => 'trim|required|min_length[12]|xss_clean'
),
'unit' => array(
'field' => 'unit',
'label' => 'Unit Number',
'rules' => 'trim|required|max_length[5]|alpha_dash|xss_clean'
),
'zip' => array(
'field' => 'zip',
'label' => 'Zip Code',
'rules' => 'trim|required|exact_length[5]|integer|xss_clean'
),
'notes' => array(
'field' => 'notes',
'label' => 'Notes',
'rules' => 'trim|xss_clean'
),
'image_ids[]' => array(
'field' => 'image_ids[]',
'label' => '',
'rules' => 'trim|xss_clean'
),
'web_id' => array(
'field' => 'web_id',
'label' => 'Web ID',
'rules' => 'trim|alpha_dash|xss_clean'
)
)
);
public function __construct() {
parent::__construct();
}
public function getNew() {
// Remember everytime you add a field you have to add it to form validation...
$listing = new stdClass();
$listing->listing_id = $this->_get_identifier();
$listing->account_id = $this->session->userdata('account_id');
$listing->status_id = 0;
$listing->feature_id = '';
$listing->image_id = 0;
$listing->nbrhood_id = ''; // Dropdown
$listing->beds_id = ''; // Dropdown
$listing->baths_id = ''; // Dropdown
$listing->pet_id = ''; // Dropdown
$listing->type_id = ''; // Dropdown
$listing->owner_id = ''; // Dropdown
$listing->web_id = 'CWA' . random_string('numeric', 7); // varchar
$listing->featured = ''; // bool
$listing->deal_id = '0'; // bool
$listing->price = ''; // int
$listing->convertible = ''; // bool
$listing->dateAvailable = ''; // Only to populate visible jQuery datapicker
$listing->available = ''; // date
$listing->sqft = ''; // int
$listing->address = ''; // varchar
$listing->unit = ''; // varchar
$listing->description = ''; // text
$listing->zip = ''; // int
$listing->notes = ''; // text
$listing->amenity_ids['unit'] = array(); // Dropdown
$listing->amenity_ids['property'] = array(); // Dropdown
$listing->image_ids = array();
return $listing;
}
I don't know about your data model. You should post it here. If your data model has been done correctly, then this is the line that would change:
$this->db->join('images', 'images.image_id = listings.image_id', 'left');
to
$this->db->join('images', 'images.listing_id = listings.id');
You should store the listing ID in the Images table rather than the other way round.
EDIT:
From data model, I mean database structure. If there are n images for every single 'listing', then you need to store a reference to the 'listing ID' in the 'images' table.
Took over this project from someone else. Where would I add fields to the image table. Right now in CMS it shows multiple images for a listing. The code is:
<?php foreach ($images as $image): ?>
<li data-image-id="<?=$image -> image_id ?>" class="uploaded-image" id="image_<?=$image -> image_id ?>">
<div class="sortableimagewrapper">
<!-- <span class="label label-inverse image-name"> </span> -->
<img src="<?=images_url('110x68/' . $image -> file_name) ?>" alt="" width="110px" height="68px" class="img-polaroid" />
<div class="btn-group">
<button class="btn <?php
if ($image -> image_id == $listing -> image_id)
echo 'btn-warning';
?> image-default"><span class="icon-star <?php
if ($image -> image_id == $listing -> image_id)
echo 'icon-white';
?>"></span></button>
<button class="btn image-remove"><span class="icon-remove"></span></button>
</div>
</div>
</li>
<?php endforeach; ?>
</ol>

ajax function in drupal 6 not working

I need to filter the territory field upon the value of the region field im using ajax function like it's mentioned in the below code but it's not working. THE AJAX CALL on the fonction is not getting into the function 'territory_filter_callback' anyway would know where is the error?
function form_search_menu() {
$items['form/search'] = array(
'title' => t('Search'),
'page callback' => 'drupal_get_form',
'page arguments' => array('_search'),
'access callback' => TRUE,
'description' => t('search'),
);
return $items;
}
function _search(&$form_state) {
drupal_add_js(drupal_get_path('module', 'form_search') .'/script.js');
$form['description'] = array(
'#type' => 'item',
'#title' => t('Search page'),
);
//FILL THE LIST OF REGIONS
$conn = oci_connect('webuser', 'website', '172.16.1.1');
//regions
$stid = oci_parse($conn, "SELECt code,descr1 FROM table1.region");
oci_execute($stid);
$cidades = array(); while (($row = oci_fetch_array($stid, OCI_ASSOC))) {
$cidades[$row['CODE']]= $row['DESCR1'];
} $cidades ['']='Select';
//city
$stid1 = oci_parse($conn, "SELECT code,descr1 from table1.city ORDER BY DESCR1"); oci_execute($stid1); $types = array(); while (($row = oci_fetch_array($stid1, OCI_ASSOC))) {
$types [$row['CODE']]= $row['DESCR1'];
} $types['']='Select';
//territory
$stid2 = oci_parse($conn, "SELECT code,descr1 FROM table1.territory"); oci_execute($stid2);
$territory = array();
while (($row = oci_fetch_array($stid2, OCI_ASSOC))) {
$territory [$row['CODE']]= $row['DESCR1'];
}
$territory ['']='Select';
oci_free_statement($stid);
oci_close($conn);
$form['name'] = array(
'#type' => 'fieldset',
//'#title' => t('Name'),
// Make the fieldset collapsible.
'#collapsible' => false, // Added
'#collapsed' => FALSE, // Added
);
$form['name']['Region'] = array(
'#type' => 'select',
'#title' => t('Region'),
'#options' => $cidades,
'#required' => FALSE, '#default_value' => isset($form_state['values']['name']['Region']) ? $form_state ['values']['name']['Region'] : '',
'#ajax' => array(
'event' => 'change',
'callback' => 'territory_filter_callback',
'wrapper' => 'dropdown_second_replace'
),
// Added
);
$form['name']['Territory'] = array(
'#type' => 'select',
'#title' => t('Territory'), '#prefix' => '<div id="dropdown_second_replace">', '#suffix' => '</div>',
'#options' => $territory,
'#required' => FALSE, '#default_value' => isset($form_state['values']['name']['Territory']) ? $form_state['values']['name']['Territory'] : '',
// Added );
$form['name']['City'] = array(
'#type' => 'select',
'#title' => t('City'),
'#options' => $types,
'#required' => FALSE,
// Added
);
$form['link'] = array(
'#type' => 'markup',
'#value' => '<a href="#" onclick="navigate()" ><input type="button" value="Search" style="background-color:#2A64A9;color:#FFFFFF;width:80px; height:25px; border:1;CURSOR:POINTER;float:right;border-color:#FFFFFF;" ></a>', //
return $form;
}
function territory_filter_callback(&$form,&$form_state)
{
$territory_options=array();
if(isset($form['name']['Region']['#default_value']['0'])
{ $Region=$form['name']['Region']['#default_value']['0'];
}
else
{ $Region=0;
}
$territory_options=selected_territory($Region);
$form['name']['Region']['#ajax']=
array('event' => 'change',
'wrapper' => 'territory_wrapper',
'callback' => 'filter_territory_callback',
'method' =>replace,
);
$form ['name']['territory']['prefix']='<div id="territory_wrapper">';
$form ['name']['territory']['prefix']='</div>';
$form ['territory']['#options']=$territory_options;
}
function filter_territory_callback($form,$form_state)
{
$Region=$form['name']['Region']['#value'];
$form['name']['territory']['#options']=selected_territory($Region);
return $form['territory'];
}
drupal 6 not support '#ajax'...'#ajax' is working with drupal 7...in drupal 6 you can use '#ahah'
http://drupal.org/node/331941
good luck

Validation Errors not showing

I am trying to validate a user when they register to my application. Nothing is getting set to validationErrors, which is strange can anyone help me out?
Here is my MembersController
<?php
class MembersController extends AppController {
var $name = 'Members';
var $components = array('RequestHandler','Uploader.Uploader');
function beforeFilter() {
parent::beforeFilter();
$this->layout = 'area';
$this->Auth->allow('register');
$this->Auth->loginRedirect = array('controller' => 'members', 'action' => 'dashboard');
$this->Uploader->uploadDir = 'files/avatars/';
$this->Uploader->maxFileSize = '2M';
}
function login() {}
function logout() {
$this->redirect($this->Auth->logout());
}
function register() {
if ($this->data) {
if ($this->data['Member']['psword'] == $this->Auth->password($this->data['Member']['psword_confirm'])) {
$this->Member->create();
if ($this->Member->save($this->data)) {
$this->Auth->login($this->data);
$this->redirect(array('action' => 'dashboard'));
} else {
$this->Session->setFlash(__('Account could not be created', true));
$this->redirect(array('action' => 'login'));
pr($this->Member->invalidFields());
}
}
}
}
}
?>
Member Model
<?php
class Member extends AppModel {
var $name = 'Member';
var $actsAs = array('Searchable');
var $validate = array(
'first_name' => array(
'rule' => 'alphaNumeric',
'required' => true,
'allowEmpty' => false,
'message' => 'Please enter your first name'
),
'last_name' => array(
'rule' => 'alphaNumeric',
'required' => true,
'allowEmpty' => false,
'message' => "Please enter your last name"
),
'email_address' => array(
'loginRule-1' => array(
'rule' => 'email',
'message' => 'please enter a valid email address',
'last' => true
),
'loginRule-2' => array(
'rule' => 'isUnique',
'message' => 'It looks like that email has been used before'
)
),
'psword' => array(
'rule' => array('minLength',8),
'required' => true,
'allowEmpty' => false,
'message' => 'Please enter a password with a minimum lenght of 8 characters.'
)
);
var $hasOne = array('Avatar');
var $hasMany = array(
'Favourite' => array(
'className' => 'Favourite',
'foreignKey' => 'member_id',
'dependent' => false
),
'Friend' => array(
'className' => 'Friend',
'foreignKey' => 'member_id',
'dependent' => false
),
'Guestbook' => array(
'className' => 'Guestbook',
'foreignKey' => 'member_id',
'dependent' => false
),
'Accommodation'
);
var $hasAndBelongsToMany = array('Interest' => array(
'fields' => array('id','interest')
)
);
function beforeSave($options = array()) {
parent::beforeSave();
if (isset($this->data[$this->alias]['interests']) && !empty($this->data[$this->alias]['interests'])) {
$tagIds = $this->Interest->saveMemberInterests($this->data[$this->alias]['interests']);
unset($this->data[$this->alias]['interests']);
$this->data[$this->Interest->alias][$this->Interest->alias] = $tagIds;
}
$this->data['Member']['first_name'] = Inflector::humanize($this->data['Member']['first_name']);
$this->data['Member']['last_name'] = Inflector::humanize($this->data['Member']['last_name']);
return true;
}
}
?>
login.ctp
<div id="login-form" class="round">
<h2>Sign In</h2>
<?php echo $form->create('Member', array('action' => 'login')); ?>
<?php echo $form->input('email_address',array('class' => 'login-text',
'label' => array('class' => 'login-label')
));?>
<?php echo $form->input('psword' ,array('class' => 'login-text',
'label' => array('class' => 'login-label','text' => 'Password')
))?>
<?php echo $form->end('Sign In');?>
</div>
<div id="signup-form" class="round">
<h2>Don't have an account yet?</h2>
<?php echo $form->create('Member', array('action' => 'register')); ?>
<?php echo $form->input('first_name',array('class' => 'login-text',
'label' => array('class' => 'login-label')
));?>
<?php echo $form->input('last_name',array('class' => 'login-text',
'label' => array('class' => 'login-label')
));?>
<?php echo $form->input('email_address',array('class' => 'login-text',
'label' => array('class' => 'login-label')
));?>
<?php echo $form->input('psword' ,array('class' => 'login-text',
'label' => array('class' => 'login-label','text' => 'Password')
))?>
<?php echo $form->input('psword_confirm' ,array('class' => 'login-text',
'label' => array('class' => 'login-label','text' => 'Confirm'),
'div' => array('style' => ''),
'type' => 'password'
))?>
<?php echo $form->end('Sign In');?>
</div>
I believe your problem is here:
$this->redirect(array('action' => 'login'));
pr($this->Member->invalidFields());
The validation errors are designed to show on the form, underneath the appropriate field. However, instead of continuing and trying to display the form, you are redirecting the user to a different page.
If you remove the two lines above, it should show the validation errors beneath their fields on the form when the validation fails.

Resources