CakePHP Text as Form Submit - forms

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

Related

Disable autocomplete in CakePHP form input box

I am creating form inputs with the CakePHP Form helper and some inputs (Most of the time 'username' and 'password') are being autocompleted on create actions, login actions, etc.. this is annoying. I am guessing those are just more common so the browser is using its cookies to try to complete the inputs.
Anyways.. how do I disable this?
In my view:
...
echo $this->Form->input('username', array(
'label' => 'Please enter your username',
'class' => 'pure-u-1-2'
));
echo $this->Form->input('password', array(
'label' => 'Please enter your password',
'class' => 'pure-u-1-2'
));
...
What am I missing?
You can specify attributes to be sent to the form helper. Specify the attribute 'autocomplete' and set its value to 'off'.
...
echo $this->Form->input('username', array(
'label' => 'Please enter your username',
'class' => 'pure-u-1-2',
'autocomplete' => 'off'
));
echo $this->Form->input('password', array(
'label' => 'Please enter your password',
'class' => 'pure-u-1-2',
'autocomplete' => 'off'
));
...
Which results in something like this for your HTML:
<input name="data[Model][username]" autocomplete="off" class="pure-u-1-2" id="ModelUsername" type="text">
You may also do this on the whole form instead of just each input. Just specify the same attribute and value in the form create like so:
...
echo $this->Form->create('Model', array(
'class' => 'class',
'autocomplete' => 'off'
));
This will give you something like this in your HTML:
<form action=".../Model/Action" class="class" autocomplete="off" id="ModelActionForm" method="post" accept-charset="utf-8">
NOTE Several browsers will now ignore autocomplete="off" or autocomplete="false". The workaround is to place a hidden text and password field before all other inputs on your form. The browsers will fill those instead of the ones you want to leave alone.
The best solution is to use autocomplete = new-password
It works great in Chrome and Firefox
Like this:
$this->Form->input('password', array('type' => 'password', 'autocomplete' => 'new-password'));

Drupal 7: Show fields on a form based on another field and a database lookup

I have a form I created in a custom module using the Form API. It is a fairly basic form with only 4 fields. It basically signs a user up for a job alert system. We are basing it only by email address with a few search parameters. We want people to be able to setup a search agent quickly and anonymously meaning they will NOT be creating a Drupal user account as we don't want them to have to deal with a password etc. They will just put in their email address, check off a few preferences and we will save the data.
Now the issue I need to deal with is allowing the user to edit their preferences later on and/or unsubscribe. Again this is not high security and it doesn't need to be. What I would like to do is initially ask ONLY for their email address in the form and allow them to submit it. I would then check the database to see if we already have an entry for that email address and if so, display the pre-filled form for them to edit or unsubsribe, other wise just show them the blank form. So I am just trying to figure out the best way to go about this. I'm thinking I just have one form with all of the fields including email address, but somehow only display the other fields besides the email address after a successful call to the database. I'm just tripping up on how to accomplish this.
EDIT:
I'm wondering if I can use Drupal's AJAX functionality to accomplish this? I tried this, but I couldn't get it to work. I put an Ajax attribute on my submit button with a wrapper ID and a callback function. I created a form element in my form with blank markup and used a prefix and suffix that created a wrapper div with the ID I used in my AJAX parameter. Then I am thinking in my callback function I can do the database lookup and then return the form elements I need either pre-filled or not into the wrapper div that was created, but when I do this, the form does submit via AJAX and I get the spinning wheel, but no matter what I return in my callback, it does not appear in my output wrapper div. Am I going about this the right way? I also made sure I have $form_state['rebuild'] = TRUE; on my original form.
Here is what I tried and it didn't work.
/**
* Implements hook_form().
*/
function _vista_form($form, &$form_state) {
$form = array();
$form_state['rebuild'] = TRUE;
$form['email'] = array(
'#type' => 'textfield',
'#title' => 'Email',
'#required' => TRUE,
);
$form['render_area'] = array(
'#type' => 'markup',
'#markup' => '',
'#prefix' => '<div id="job-agent-form">',
'#suffix' => '</div>',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
'#attributes' => array('class' => array('submit')),
'#ajax' => array(
'callback' => '_display_form',
'wrapper' => 'job-agent-form',
'method' => 'replace',
'effect' => 'fade',
),
return $form;
}
function _display_form($form, &$form_state) {
// there are other form elements that would go here also, I just added two for example
$type_options = array(
'VISTA-HealthCare-Partners-Government' => 'Vista Healthcare Partners',
'International' => 'International Locum Tenens',
'Permanent' => 'Permanent Physician',
'US-Locum-Tenens' => 'US Locum Tenens',
);
$form['job_type'] = array(
'#type' => 'checkboxes',
'#multiple' => TRUE,
'#title' => 'Type of Job',
'#options' => $type_options,
'#empty_option' => 'Choose a placement type',
'#empty_value' => 'all',
//'#default_value' => $type_selected,
);
$form['active'] = array(
'#type' => 'checkbox',
'#title' => 'Subscribe/Unsubscribe',
'#default_value' => 1,
);
return $form;
}
I would go for creating all fields in the form than use hook_form_alter() to hide the unneeded ones with ['#access'] = FALSE.

Cakephp input form view as unordered list

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.

Display boolean as radio button in CakePHP form

Does anyone know how to display two radio buttons in a form in CakePHP and have them save to a boolean field in the DB?
I can get my boolean value to display as a checkbox (default behaviour) but I need to display two radio buttons, however when I do this the changes don't get saved to the DB.
I feel like it's something very simple. Here's my code:
<h1>Edit Content</h1>
<?php
$thisVal = $this->data['LessonContent']['is_exercise'] ? "1" : "0";
$options = array(
'0' => 'Learning Material',
'1' => 'Exercise'
);
echo $this->Form->create('LessonContent', array('action'=>'edit'));
echo $this->Form->input('name', array('label' => 'Name'));
echo $this->Form->input('description', array('label' => 'Description'));
echo $this->Form->input('is_exercise', array(
'type' => 'radio',
'class' => 'radio',
'legend' => false,
'name' => 'Type',
'options' => $options,
'value' => $thisVal
));
echo $this->Form->input('id', array('type'=>'hidden'));
echo $this->Form->input('lesson_id', array('type'=>'hidden'));
echo $this->Form->end('Save Content');
echo $this->Html->link('Cancel', array('controller'=>'Lessons', 'action'=>'view', $this->data['Lesson']['id']));
?>
Thanks
You're overriding the name of the input, therefore the value for is_excercise will be sent as Type.
Remove the name option;
echo $this->Form->input('is_exercise', array(
'type' => 'radio',
'class' => 'radio',
'legend' => false,
'options' => $options,
'value' => $thisVal
));
note after making this change, you probably don't even have to manually set the value of the radio-button.
debugging
In situations like this, always check/debug the posted form-data, either via FireBug or by debugging it in CakePHP, by putting this in your controller;
debug($this->request);
Even better, install the CakePHP DebugKit Plugin this plugin shows all the information (request data, queries, session variables etc.) without having to add debug-lines

Cakephp form creation gives me missing database table

According to the manual, I should be able to do this which is to leave Model as null
<?php
echo $this->Form->create(null, array('url' => '/recipes/add'));
// or
echo $this->Form->create(null, array(
'url' => array('controller' => 'recipes', 'action' => 'add')
));
But in reality, I got error saying missing database table? Why?
I have my Model which is not directly mapping to any table. Why can't I leave it as null? Something like this:
<?php echo $this->Form->create(null,array('url' => array('my_account','action' => 'change_avatar'),'type' => 'file'));?>
Try passing false for the model instead of null:
<?php
echo $this->Form->create(false, array('url' => '/recipes/add'));
// or
echo $this->Form->create(false, array(
'url' => array('controller' => 'recipes', 'action' => 'add')
));
From the manual:
You can also pass false for $model. This will place your form data into the array: $this->request->data (instead of in the sub-array: $this->request->data['Model']). This can be handy for short forms that may not represent anything in your database.
make a table with name recipes in your database. The submitted date from the form will be saved at recipes table.