Cakephp input form view as unordered list - forms

I'm developing a website using cakephp 2.x.
Now, I create a form using Cakedc/search. This form has input(select/dropdown list).
But the list is too long, so I want the dropdown to be view as unordered list (< ul >< li >).
Like in the lazada (search for brand): http://www.lazada.com.my/womens-watches-bags-accessories/.
the code:
<?php echo $this->Form->create('Product', array(
'url' => array_merge(array('action' => 'search'), $this->params['pass'])));
echo $this->Form->input('brand_id', array('label' => 'Brand', 'options' => $brands, 'empty' => 'Select Brand'));
<?php echo $this->Form->submit(__('Search', true), array('div' => false));
echo $this->Form->end();
?>
Please someone help me. Thanks in advance..

You'll have to use javascript to get the id of the clicked li element or the link inside, use data attributes for that for example. Then set this value to a hidden form field or directly submit the whole form using ajax to update your results.
Your example URL is a simple list by the way that is just doing redirect on click.

Related

How can I add autocomplete functionality to an input field I've added in a .tpl template file?

I have a custom module called "visionart cart" working as expected and have enabled a page through hook_menu();
function visionart_cart_menu(){
/**
* Implements hook_menu()
*/
$items['build-order'] = array (
'title' => 'Build your order',
'menu_name' => 'main-menu',
'type' => MENU_NORMAL_ITEM,
'page callback' => 'visionart_cart_build_order',
'access arguments' => array ('access content'),
'access callback' => 'user_is_logged_in',
);
}
Page callback returns the template page, everything works:
function visionart_cart_build_order() {
return theme('build_order_template');
}
/** Implements hook_theme. */
function visionart_cart_theme() {
return array (
'build_order_template' => array (
'template' => 'build_order'
)
);
}
In build_order.tpl.php, I've added a bunch of custom HTML show up on the build_order.php page. Everything renders fine. But I'd like to add user autocomplete functionality to the input field that is created on this build_order.tpl.php file.
<li class="my-custom-item">
<input type="text" name="build_order_username" value="" />
</li>
How and where do I do that? I've seen how to create form fields programmatically through Drupal's form building functions, but i can't find where to indicate to Drupal that this custom created input should use autocomplete on the user base. Thanks!
You will require an API endpoint call, which will auto-complete the fields. Assuming you are on Drupal 7, here is a link from the official Drupal documentation on how to make autocomplete fields.

CakePHP Text as Form Submit

I've searched the web and have come up with nothing. (Multiple search engines too - I have looked!)
I'm trying to have a text link as the 'form submit' button. Any ideas if this is possible in CakePHP?
Current view code below!
<?php
echo $this->Form->create('trainees', array(
'action' => 'reassign'
));
echo $this->Form->input('emailaddress', array(
'value' => 'scott#something',
'type' => 'hidden',
));
echo $this->Form->submit('Re-Assign Mentor', array(
'class' => 'submit mid',
'before' => '<p>',
'after' => '</p>'
));
echo $this->Form->end();
?>
You need to use the HtmlHelper to output a link. In it's simplest form you use the text you want displayed with the URL that it should link to. In this case it will be JavaScript:
$this->Html->link('Submit Form', 'javascript:document.forms["myform"].submit();');
There are two additional parameters (a $options array and $confirmMessage boolean), but they along with the URL are optional.
You can also call your own JavaScript function if you need to do client side verification and call the submit function from there (also verify on the server as clients can lie).
http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::link

cakephp link outside form which access form input values

I have this form with a date input.
echo $this->Form->create('Nodata');
echo $this->Form->input('date1', array('type' => 'date', 'label' => 'From:'));
echo $this->Form->input('date2', array('type' => 'date', 'label' => 'To:'));
echo $this->Form->end('Get Hours');
When the form is submitted, I'm showing the results in the same view, below the form.
My problem is I have a link that is not part of the form and need to read the value (in the view) from the date field on the form to use it as a param
on this link.
// date1 is the param I need to take the value from date input
<th> <?php echo $this->Html->link(__('Agents Detail'), array('controller' => 'qcas', 'action' => 'hours', 'paramProject' => $hour['Qca']['dir_id'], 'date1' => $this->data)); ?> </th>
Note this link is outside the form and I need a way to read a input on the form to use as param in my link.
Instead of just using $this->data for your date1 element, you need to refer the field in the $this->data object.
CakePHP < 2.0
'date1' => $this->data['Nodata']['date1']
CakePHP 2.0+
'date1' => $this->request->data['Nodata']['date1']
I'm not sure what you're trying to do at the destination link, but you may need to format the date as well:
'date1' => date('Y-m-d', $this->request->data['Nodata']['date1']) // you may need strtotime

CakePHP: allowing database update with button click

I have a product search page with the form below. The search result is displayed on the same page with search bar at the top.
echo $this->Form->create('Searches', array('action'=>'products', 'type' => 'get', 'name' => 'textbox1'));
echo $form->input($varName1, array('label' => false));
echo $form->end('Locate');
I also have a little box next to the search result that allows (it doesn't work yet) the user to flag using checkboxes a product and accordingly update its database (table products and using model Product) with a button click. Note that I have a Searches controller for this search page.
<form method="link" action="/myapp/product/test_update_db>
<label><input type="checkbox" name="flag1" <?php echo $preCheckBox1; ?>>Flag 1</input></label>
<label><input type="checkbox" name="flag2" <?php echo $preCheckBox2; ?>>Flag 2</input></label>
<input type="submit" value="Update">
</form>
I'm having difficulty with this approach figuring out how to perform this check-box-and-DB-update routine. I'm getting to the link I'd like to go (/myapp/product/test_update_db), but I don't know how to take variables flag1 and flag2, along with row ID of this result ($results['Product']['id'])) to the new page.
Could someone guide me on how to perform this neatly? Is this general approach correct? If not, what route should I be taking? I'd prefer not to use javascript at this time, if possible.
EDIT: I think I can make this work if I use the URL for passing data.. but I'd still like to know how this could be done "under the hood" or in MVC. I feel like I'm hacking at the CakePHP platform.
UPDATE: So, I ended up using the URL parameters for retrieving information pieces like flag1 and flag2. I'm still looking for an alternative method.
To see where your is-checkbox-checked data is located, do the following in your controller:
// Cake 2.0+
debug($this->request->data);
// previous versions
debug($this->data);
If you want to pass data to your search controller from the current page, you can always add the data to your form:
$this->input
(
'Product.id',
array
(
'type' => 'hidden',
'value' => $yourProductId
)
);
I ended up using information embedded in the URL for getting submission data. Something like below..
In Products controller, when the form with flag1 and flag2 are submitted:
public function test_update_db() {
// Get variables from URL, if any, and save accordingly
$result = $this->Product->updateProduct($this->params['url'], 'url');
if ($result) {
$this->Session->setFlash('Successfully updated!', 'default', array('class' => 'success'));
$this->redirect($this->referer());
}
else {
$this->Session->setFlash('Update was unsuccessful!', 'default', array('class' => 'error'));
$this->redirect($this->referer());
}
}
This works for doing what I needed to do. I feel like there's a more proper way to do this though.
if ($result) {
$this->Session->setFlash('Successfully updated!', 'default', array('class' => 'success'));
$this->redirect($this->referer());
}

Form->end without a div in CakePHP

I'd like to create a submit button that does NOT have a <div> around it. I assumed using the inputDefaults would make this happen like it does for all of the forms inputs, but - no luck.
Obviously I could just create a submit button via HTML, without CakePHP, but - I was hoping there'd be a cake answer. Here's what I've tried:
$this->Form->create(false, array('inputDefaults' => array('div'=>false)));
$this->Form->end('Submit');
echo $this->Form->submit('Submit', array('div'=>false));
Should do what you are after. The other example may have been Cake 1.2 or something; not sure.
It also appears you can just do this instead:
<?php
$options = array(
'label' => 'Update',
'value' => 'Update!',
'div' => false
)
);
echo $this->Form->end($options);
That looks more cakey.