I am having an issue with my submit button and I think its because I just left it in the regular format echo $this->Form->end('Submit') but actually I want to translate these properties into CakePHP <input type="submit" value="Submit" name="submitrating" class="button"/> .
How can I create those properties on the submit button in CakePHP html helper form?, I am switching from a conventional php website to cake php.
Thank you
You can add attributes by passing them as an array.
$this->Form->end(
array(
'label' => 'Submit',
'name' => 'submitrating',
'class' => 'button'
)
);
Related
I'm building a custom settings form in Drupal 7, with an image upload field. This image field should allow multiple uploads.
After some research I found out you can do this with managed_file and '#attributes' => array('multiple' => 'multiple'). However this doesn't seem to do anything.
This is the code I currently have:
$form['frontpage_banner_images'] = array(
'#type' => 'managed_file',
'#title' => t('Frontpage Images'),
'#name' => 'files[]',
'#attributes' => array(
'multiple' => 'multiple',
'class' => 'testclass',
),
'#upload_location' => 'public://homepage-banners/',
'#default_value' => variable_get('frontpage_banner_images'),
);
With this as result:
<div class="form-item form-type-managed-file form-item-files-">
<label for="edit-frontpage-banner-images-upload">Frontpage Images</label>
<div id="edit-frontpage-banner-images-upload" class="testclass form-managed-file">
<input type="file" id="edit-frontpage-banner-images-upload" name="files[frontpage_banner_images]" size="22" class="form-file">
<input type="submit" id="edit-frontpage-banner-images-upload-button" name="frontpage_banner_images_upload_button" value="Upload" class="form-submit ajax-processed">
<input type="hidden" name="frontpage_banner_images[fid]" value="0">
</div>
</div>
As you can see, the testclass from my #attributes is being applied on the wrapping div and not on the file input. So the multiple attribute isn't doing anything.
This is what I'm trying to achieve (Photoshopped):
Any help on how to achieve this is appreciated.
The multiple option does not seem to be supported. Then again you are using it under the #attributes. This would cause IMO the html element to have an atttribute multiple, but I don't see anything in /modules/file/file.js to pick up on that, so that's presumably why it's not doing anything.
You're probably better off using plupload, as suggested here.
I'm developing an app in cakePHP and I'd like to use Foundation Tooltips into it.
I need to add data-tooltip to the input in order to make it work, but I don't know how to do it with the cakePHP form helper.
Right now I'm using:
<?php echo $this->Form->input('title', (array( 'label' => __('title'),
'class' => 'has-tip',
'title' => __('Tooltip for initiative title'),
)));
And is returning this:
<input name="data[Initiative][title]" class="has-tip" title="Tooltip for initiative title" maxlength="255" type="text" id="InitiativeTitle" required="required">
When I need this:
<input data-tooltip name="data[Initiative][title]" class="has-tip" title=...>
I've tried adding 'data-tooltip'to the input array with no luck, but I'm sure it has to be an easy way of doing it.
Thanks in advance and sorry for my English.
<?php echo $this->Form->input('title', array( 'label' => __('title'),
'class' => 'has-tip',
'title' => __('Tooltip for initiative title'),
'data-tooltip'=>""
));
I am trying to create a very complex form with ZF2, and I can't find a solution to implement array based names in input elements.
This is a chunck of code from my actual form:
<input type=text name=table[name] value="Table Name">
<input type=text name=table[title] value="Table Title">
<input type=text name=table[columns][names][] value="Column Name 1">
<input type=text name=table[columns][names][] value="Column Name 2">
<input type=text name=table[columns][names][] value="Column Name 3">
<input type=text name=table[columns][labels][] value="Column Label 1">
<input type=text name=table[columns][labels][] value="Column Label 2">
<input type=text name=table[columns][labels][] value="Column Label 3">
I know how to work with Zend/Form, but this is an advanced usaged I can`t master.
Your code looks like a dynamic form rendered into table format. One possibility besides Zend Fieldsets is to generate your basic form structure dynamically wihtin OOP-Constructor, like #Sam said.
In your case, you should have a concept of which the form is structured. E.g. you have an array with data from a database, which gives you for example the number and names of your "columns" and so the structure how to build your dynamic form.
Following example uses Zend Form constructor to generate a dynamic form. Number of columns are dynamic and specified by given array $tableColumns
<?php
class DynamicForm extends Form {
/**
* This constructor builds a Zend Form dynamically
*
* #param array $tableColumns Dynamic data e.g. 'Column Name 1'
*/
public function __construct($tableColumns) {
// Add table name input
$this->add(array(
'type' => 'text',
'name' => 'name'
));
// Add table title input
$this->add(array(
'type' => 'text',
'name' => 'title'
));
// iterate each dynamic data
foreach($tableColumns as $column) {
$this->add(array(
'type' => 'text',
'name' => $column['name']
));
}
}
}
This is how you generate the Zend form structure. Rendering works similar, but within yout view code.
<?php
echo "<table>";
echo "<tr>";
echo "<td>". $this->formElement($form->get('name')) . "</td>";
echo "<td>". $this->formElement($form->get('title')) . "</td>";
foreach($tableColumns as $column) {
echo "<td>". $this->formElement($column) ."</td>";
}
echo "</tr>";
echo "</table>";
?>
In my opinion it is much more comfortable to render a form dynamically into a table format, than it's possible a Fieldset.
But nevertheless Zend Form Collection (e.g. Fieldsets) is a good choice, if you have to add or remove Form-elements dynamically with Javascript and handle your form data also highly dynamically.
More information about Zend From Collections are described at the ZF2 Reference Manual
You make use of Zend\Form\Element\Fieldset for this kind of scenario. It helps a great deal with proper OOP-Construction of Forms.
I would like to know if it's possible to format the name of the checkbox when I try to send it to my other page via a GET method.
Actually I've got a multiple checkboxes that generate something like:
<div class="checkbox"><input type="checkbox" name="test[]" value="1" id="ResearchTest1" /><label for="ResearchTest1">First Test</label></div>
<div class="checkbox"><input type="checkbox" name="test[]" value="2" id="ResearchTest2" /><label for="ResearchTest2">Second Test</label></div>
<div class="checkbox"><input type="checkbox" name="test[]" value="3" id="ResearchTest3" /><label for="ResearchTest3">Third Test</label></div>
And when I send my form, my URL looks like:
research%3D%26test%3D%26test%5B%5D%3D1%26test%5B%5D%3D2%26test%5B%5D%3D3
Which is :
research=&test=&test[]=1&test[]=2&test[]=3
And what I would like, will be:
research=&test1=1&test2=2&test3=3
Or
research=&test=1&test=2&test=3
Or, much better:
research=&test=1,2,3
Any ideas ?
That is how checkboxes work, if you want test1=1&test2=2&test3=3 then you should name each checkbox individually.
echo $this->Form->checkbox('foo', array('name' => 'test1'));
echo $this->Form->checkbox('foo', array('name' => 'test2'));
echo $this->Form->checkbox('foo', array('name' => 'test3'));
This will make your processing much harder.
You can do research=&test=1,2,3 with JS join(). Why you want to make this difficult for yourself I dont know. You can easily get that same format in the controller doint the GET with implode(',', $theData)
I would recommend using standards for submitting your form and process the data later.
You can define the name of the input using the FormHelper:
echo $this->Form->checkbox('yourInputId', array('name' => 'yourInputName'));
The problem is that I use a multiple checkboxes:
$this->Form->input('tests', array('type' => 'select', 'multiple' => 'checkbox', 'options' => $options));
So I can't rename each checkbox individually.
Maybe I should not use cakephp for this, but try to write my own checkboxes directly in html?
Let's say I have field that looks like this in the view:
<li class="bigfield">
<?php echo $form->input('phone', array(
'placeholder' => 'Phone',
'label' => false,
'between' => '<br />'
)); ?>
</li>
If I have a validation rule on this field and validation fails, I see the following HTML:
<li class="bigfield">
<div class="input text required error">
<br>
<input name="data[Appointment][email]" type="text" placeholder="Email"
maxlength="45" value="" id="AppointmentEmail" class="form-error">
<div class="error-message">Please enter a valid email address</div>
</div>
</li>
I'm like to do something like move the error message div to an entire different part of the page rather then have it inside with the same <li> as the field itself. What would be the most straight forward way of doing this?
Just updating an old post.
The validations errors are automatically passed on to view (as pointed out by #Angel S. Moreno)
$this->validationErrors
In you controller:
$this->set('validationErrorsArray', $this->ModelName->invalidFields());
You will have $validationErrorsArray in your views.
UPDATE (Sept. 2014):
From the view
From CakePHP 2.3 you can access validation errors array from the view:
$this->validationErrors;
From the controller
If you tried to save data in the controller you can access validation errors this way:
$this->ModelName->validationErrors;
If you want to validate data before saving do it this way:
$this->ModelName->set($this->request->data);
if ($this->ModelName->validates()) {
$this->ModelName->save();
} else {
$errors = $this->ModelName->validationErrors;
// handle errors
}
Validating Data from the Controller
From controller you can use:
$this->Modelname->validationErrors['TheFieldYouWantToDisplay'] = 'This is not correct'
In your case it would be like this in your controller:
$this->Appointment->validationErrors['email'] = 'Error message'
This code is just to make a custom error message on the fly. But you can also define $validate in the model and do it like how brancer has described it.