Multiple file upload in custom form with managed_file - drupal-7

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.

Related

CakePHP Validation Issue for dynamic fields

I have created below fields in my form-
<input type="text" id="ProductPrice1" class="small" name="data[Product][price][1]">
<input type="text" id="ProductPrice1" class="small" name="data[Product][price][2]">
<input type="text" id="ProductPrice1" class="small" name="data[Product][price][3]">
<input type="text" id="ProductPrice1" class="small" name="data[Product][price][4]">
Now I am trying to add validation for above fields by using below model function-
public function productValidates() {
$validate= array();
$validate = array(
'name'=> array(
'mustNotEmpty'=>array(
'rule' => 'notEmpty',
'message'=> __('PRODUCTS.TITLE_BLANK_ERROR',true)
)
),
'description'=> array(
'mustNotEmpty'=>array(
'rule' => 'notEmpty',
'message'=> __('PRODUCTS.DESCRIPTION_BLANK_ERROR',true)
)
),
'category_id'=> array(
'mustNotEmpty'=>array(
'rule' => 'notEmpty',
'message'=> __('PRODUCTS.SELECT_CATEGORY_ERROR',true)
)
),
);
$count = $this->data[$this->name]['total_prices'];
for($i=1; $i<= $count;$i++){
$validate['price'][$i] = array(
'1' => array(
'rule' => array('notEmpty',true),
'message' => __('PRODUCTS.PRICE_EMPTY_ERROR',true),
'last' => true
)
);
}
$this->validate = $validate;
return $this->validates();
}
But its not working.
Can you please suggest what’s the issue here?
I suppose that you are using cakephp 2.X
I think that the problem is the loop, instead of that add a custom validation method to check that the prices are not empty.
Here is the link to the book with an example.
http://book.cakephp.org/2.0/en/models/data-validation.html#adding-your-own-validation-methods
Another solution is to make a table Prices with columns id|product_id|value, put the association in the Models, put the validation criteria of the prices into the Price Model and in the ProductController put "saveAll" instead of save.
In this case your form must be like:
<input type="text" id="ProductName" class="small" name="data[Product][name]">
<input type="text" id="ProductDescription" class="small" name="data[Product][description]">
<input type="text" id="PriceValue1" class="small" name="data[Price][0][value]">
<input type="text" id="PriceValue1" class="small" name="data[Price][1][value]">
<input type="text" id="PriceValue1" class="small" name="data[Price][2][value]">
<input type="text" id="PriceValue1" class="small" name="data[Price][3][value]">
if this does not answer your question I will need more data such as the controller and the structure of your db

How to add attribute to an input in Cakephp

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'=>""
));

How to format the name of the checkbox while send by a GET cakephp

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?

Translating html input into cake php input convention

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'
)
);

How to format an array using PHP?

I am trying to get the data from the form I created where I used an array to name them like:
<input type="text" name="fname[]" />
<input type="text" name="mname[]" />
<input type="text" name="lname[]" />
where the said form field are dynamically inserted or removed in the page.
On my script to get the values of the forms, I wrote something like:
$student = array(
'fname' => '$_POST[fname]',
'mname' => '$_POST[mname]',
'lname' => '$_POST[lname]',
);
But when I used var_dump to see the values, each field will be indexed from zero and if it has repeating fields, it would be again be declared as another array inside an array.
What I wanted to do is to have an array with this stucture:
$student = array(
array(
'fname' => 'fname1',
'mname' => 'mname1',
'lname' => 'lname1'
),
array(
'fname' => 'fname2',
'mname' => 'mname3',
'lname' => 'lname2'
)
);
I tried using a loop but I just fail again and again. Can anyone help me solve this problem?
Thank you in advance for your help.
<input type="text" name="student[]['fname']" />
<input type="text" name="student[]['mname']" />
<input type="text" name="student[]['lname']" />
no loops necessary. Your $_POST['student'] variable will automatically be the array you wanted to achieve.
EDIT: this isn't achieving the desired result. It's incrementing student for each field. adding an n value to the first set of brackets like student[n][fname] does achieve the desired result. I don't know how the script is written to dynamically generate these three fields on the fly, but if you can figure out how to add an n value you're golden.
<?php
for($i=0; $i<count($_POST['fname']); $i++) {
$student[] = array(
'fname' => $_POST['fname'][$i],
'mname' => $_POST['mname'][$i],
'lname' => $_POST['lname'][$i],
);
}
?>
Oops, I thought Stephan's answer would suffice, but rather, it should be the following I believe (can't test right now). Try it:
<input type="text" name="student[0]['fname']" />
<input type="text" name="student[0]['mname']" />
<input type="text" name="student[0]['lname']" />
<input type="text" name="student[1]['fname']" />
<input type="text" name="student[1]['mname']" />
<input type="text" name="student[1]['lname']" />
etc...
(note the added numeric indexes)
Then when you do a echo '<pre>' . print_r( $_POST[ 'student' ], true ); you should see the structure you are looking for.

Resources