PHP attachment in write form - php

I need to edit the following form so it will have attach file function:
function _getWriteForm()
{
$aForm = array(
'form_attrs' => array(
'name' => 'WallPostText',
'action' => BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'post/',
'method' => 'post',
'enctype' => 'multipart/form-data',
'target' => 'WallPostIframe',
'onsubmit' => 'javascript:return ' . $this->_sJsPostObject . '.postSubmit(this);'
),
'inputs' => array(
'content' => array(
'type' => 'textarea',
'name' => 'content',
'caption' => '',
'colspan' => true
),
'submit' => array(
'type' => 'submit',
'name' => 'submit',
'value' => _t('_wall_post'),
'colspan' => true
)
),
);
I wanted to edit HTML file first, but the only HTML code for this is:
<!-- Post Text -->
<div class="wall-ptype-cnt wall_text">__post_wall_text__</div>
Can someone please help me with editing array to allow file attachment?
Thank you.

You should try something like this:
function _getWriteForm()
{
$aForm = array(
'form_attrs' => array(
'name' => 'WallPostText',
'action' => BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'post/',
'method' => 'post',
'enctype' => 'multipart/form-data',
'target' => 'WallPostIframe',
'onsubmit' => 'javascript:return ' . $this->_sJsPostObject . '.postSubmit(this);'
),
'inputs' => array(
'content' => array(
'type' => 'textarea',
'name' => 'content',
'caption' => '',
'colspan' => true
),
'file' => array(
'type' => 'file',
'name' => 'upload_file',
'caption' => 'Upload',
'colspan' => true
),
'file_two' => array( //I added a second input file
'type' => 'file',
'name' => 'upload_file_two',
'caption' => 'Upload',
'colspan' => true
),
'submit' => array(
'type' => 'submit',
'name' => 'submit',
'value' => _t('_wall_post'),
'colspan' => true
)
),
);
I hope it helps.
Saludos ;)

Related

How to create fieldset collection using form factory in zf2

I'm trying to create a form that contains a collection of fieldsets using only array specs and Zend\Form\Factory.
Here is how I create the form using the factory:
$factory = new Zend\Form\Factory();
$fieldset = $factory->createFieldset(array(
'elements' => array(
array(
'spec' => array(
'name' => 'name',
'type' => 'Text',
'attributes' => array(
'class' => 'form-control input-sm',
),
'options' => array(
'label' => 'Name',
),
),
),
array(
'spec' => array(
'name' => 'driverClass',
'type' => 'Text',
'attributes' => array(
'class' => 'form-control input-sm',
),
'options' => array(
'label' => 'Driver',
),
),
),
),
'input_filter' => array(
'name' => array(
'required' => true,
),
),
));
$form = $factory->createForm(array(
'name' => 'application-form',
'attributes' => array(
'role' => 'form',
),
'elements' => array(
array(
'spec' => array(
'type' => 'Collection',
'name' => 'connection',
'options' => array(
'label' => 'Connections',
'allow_add' => true,
'allow_remove' => true,
'should_create_template' => true,
'count' => 2,
'target_element' => $fieldset,
),
),
),
array(
'spec' => array(
'name' => 'security',
'type' => 'Csrf',
'attributes' => array(
'required' => 'required',
),
),
),
array(
'spec' => array(
'name' => 'submit',
'type' => 'Submit',
'attributes' => array(
'class' => 'btn btn-sm btn-primary',
),
'options' => array(
'label' => 'Apply',
),
),
),
),
));
The resulting form works fine when I try to set data and render form elements. But when I validate it and retrieve data, like so (in a controller):
$form->setData($this->getRequest()->getPost());
if ($form->isValid() === true) {
$data = $form->getData();
var_dump($this->getRequest()->getPost());
var_dump($data);
}
With this set of data as POST:
object(Zend\Stdlib\Parameters)[141]
private 'storage' (ArrayObject) =>
array (size=3)
'connection' =>
array (size=2)
0 =>
array (size=2)
'name' => string 'orm_default' (length=11)
'driverClass' => string 'Doctrine\DBAL\Driver\PDOMySql\Driver' (length=36)
1 =>
array (size=2)
'name' => string 'blog' (length=4)
'driverClass' => string 'Doctrine\DBAL\Driver\PDOMySql\Driver' (length=36)
'submit' => string '' (length=0)
'security' => string '20d5c146d8874dc804948e962d5de91b-87c9e4097f9140d259efb5c589a05d6b' (length=65)
The array returned by the call to $form->getData() shows an empty collection:
array (size=3)
'security' => string '20d5c146d8874dc804948e962d5de91b-87c9e4097f9140d259efb5c589a05d6b' (length=65)
'submit' => string '' (length=0)
'connection' =>
array (size=0)
empty
What am I missing?
The expected result is a collection, named 'connection' in this example, containing two arrays representing the two fieldsets as specified by the POST data. I have a feeling this has to do with a missing InputFilter (or at least its specs) because I have managed to obtain the expected result when I implement a fieldset class that extends Zend\Form\Fieldset and implements Zend\InputFilter\InputFilterProviderInterface.
Just discovered this class Zend\Form\InputFilterProviderFieldset which does exactly what I missed.
I added a type in the fieldset specs and changed the input filter specs (which is mandatory) like so:
$fieldset = $factory->createFieldset(array(
'type' => 'Zend\Form\InputFilterProviderFieldset',
'elements' => array(
array(
'spec' => array(
'name' => 'name',
'type' => 'Text',
'attributes' => array(
'class' => 'form-control input-sm',
),
'options' => array(
'label' => 'Name',
),
),
),
array(
'spec' => array(
'name' => 'driverClass',
'type' => 'Text',
'attributes' => array(
'class' => 'form-control input-sm',
),
'options' => array(
'label' => 'Driver',
),
),
),
),
'options' => array(
'input_filter_spec' => array(
'name' => array(
'required' => true,
),
),
),
));
And it works fine now. Hope this helped someone.

Codeigniter: Parsing array data in view in a loop

i want to multidimensional array to my view and then use this array to build form
This is what i've in my controller
function signin(){
$attributes = array(
'name' =>
array(
'name' => 'name',
'type' => 'text',
'placeholder' => '' ,
'value' => 'value'
),
'password' =>
array(
'name' => 'name',
'type' => 'password',
'placeholder' => '',
),
'gender' =>
array(
'name' => 'name',
'type' => 'select',
'value'=>
array(
'male','female'
),
),
'usertpye'=>array(
'type' => 'radio',
'seller' => 'seller',
'buyer' => 'buyer'
),
'upload'=>array(
'type' => 'file',
'name' => 'file'
),
'submit'=>array(
'type' => 'submit',
'name' => 'submit',
'value' => 'submit'
)
);
$this->load->view('login',$attributes);
}
in my view login i can access these items like $name or $password but i want to fetch in a loop.really have no idea how can i do it please help.
The load function receives an array, which keys then parses as variables in the view. Thus, you get variables like $name, $password etc.
Just add another layer before calling the load function like:
$data['attributes'] = $attributes;
And then, when loading the view do
$this->load->view('login',$data);
Here is the array a bit adjusted:
$attributes = array(
'name' =>
array(
'name' => 'name',
'type' => 'text',
'placeholder' => '' ,
'value' => 'value'
),
'password' =>
array(
'name' => 'name',
'type' => 'password',
'placeholder' => '',
),
'gender' =>
array(
'name' => 'name',
'type' => 'select',
'options' => array(
'male' => 'Male',
'female' => 'Female'
),
),
'usertpye'=>array(
'type' => 'radio',
'values' => array(
'seller' => 'seller',
'buyer' => 'buyer'
)
),
'upload'=>array(
'type' => 'file',
'name' => 'file'
),
'submit'=>array(
'type' => 'submit',
'name' => 'submit',
'value' => 'submit'
)
);
Here is how it would look like with the form helper of CI (this would go in the view, remember to first load the helper in the controller):
echo form_open('email/send');
foreach($attributes as $key=>$attribute) {
echo form_label($key).'<br/>';
if($attribute['type'] == 'select') {
echo form_dropdown($attribute['name'],$attribute['options']).'<br/>';
} elseif($attribute['type'] == 'radio') {
foreach ($attribute['values'] as $value) {
echo form_label($value);
echo form_radio(array('name' => $key, 'value' => $value)).'<br/>';
}
} else {
echo form_input($attribute).'<br/>';
}
}
Note I did some adjustments to your initial attributes array to make it work, but you'd still need to improve its structure, add unique names for all items etc.

Ajax Request is Not Working After CGridView Update

I Want to open a CJuiModel by Clicking on CGridView CButtonColumn view Button as my CGridView and CJuiModel is as:-
<?php
$dataProvider = new CArrayDataProvider($model->exhibitorLocation, array(
'keys' => array('productId'),
));
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $dataProvider,
'id' => 'exhibitor-location-grid',
'summaryText' => false,
'pager' => array(
'class' => 'CLinkPager',
'header' => false,
),
'columns' => array(
array(
'header' => 'S No.',
'value' => '++$row',
),
array(
'header' => 'Hall No',
'value' => '$data->location->hallNo',
),
array(
'header' => 'Stand No',
'value' => '$data->standNo',
),
array(
'class' => 'CButtonColumn',
'header' => 'Manage',
'template' => '{view}{delete}',
'buttons' => array(
'view' => array(
'imageUrl' => $this->module->assetsUrl . "/images/info_icon.png",
'options' => array('class' => 'exb-location'),
'url' => 'Yii::app()->createUrl("admin/venuemap/viewExbLocation", array("exbLocId"=>$data->primaryKey))',
),
'delete' => array(
'imageUrl' => $this->module->assetsUrl . "/images/delete_icon.png",
'url' => 'Yii::app()->createUrl("admin/venuemap/deleteExbLocation", array("exbLocId"=>$data->primaryKey))',
),
),
'htmlOptions' => array(
'style' => 'width:100px;float:center'
)
),
),
'rowCssClassExpression' => '($row%2 ? "visit2" : "visit1")',
'itemsCssClass' => 'visitor_list',
'htmlOptions' => array(
)
));
?>
<div class="name_field">Hall No<span>*</span></div>
<?php echo CHtml::dropDownList('hall-no', '', CHtml::listData(Venuemap::model()->findAll(), 'locationId', 'hallNo'), array('class' => 'name_input2', 'id' => 'hall-no')); ?>
<div class="name_field">Stand No</div>
<?php echo CHtml::textField('Stand No', '', array('class' => 'name_input', 'id' => 'stand-no')); ?>
<?php
echo CHtml::ajaxLink("Add Location", Yii::app()->createUrl('admin/venuemap/addExbLocation'), array(
'type' => 'post',
'data' => array(
'exhibitorId' => $model->exhibitorId,
'standNo' => 'js: $("#stand-no").val()',
'locationId' => 'js: $("#hall-no").val()'
),
'success' => 'function(html){ $.fn.yiiGridView.update("exhibitor-location-grid"); }'
), array('class' => 'search_btn'));
?>
<?php
$this->beginWidget('zii.widgets.jui.CJuiDialog', array(
'id' => 'location-dialog',
'options' => array(
'title' => 'View Location',
'autoOpen' => false,
'modal' => true,
'width' => 'auto',
'height' => 'auto',
'resizable' => false
),
));
$this->endWidget();
?>
<?php
Yii::app()->clientScript->registerScript('view-location-ajax', "
$('.exb-location').click(function(){
url=$(this).attr('href');
$.ajax({
type: 'post',
url: url,
success: function(result){
$('#location-dialog').html(result).dialog('open');
$('#draggable').draggable();
return false;
}
});
return false;
});
");
?>
View Button is working fine and open a CJuiModel on click but when I add a new Location in CGridView .
On Clicking on view button my CJuiModel is not opening and page is redirecting to Url.Why my jquery is getting failed on updating CGridView. Help..
Change ajax call using JQuery 'on' instead direct event binding:
This:
...
$('.exb-location').click(function(){
...
Change for:
...
$('.exb-location').on('click', '.exb-location', function(){
...
Html Elements are destroyed and regenerated after CGridview refresh. Direct JQuery events only work with existing elements before DOM load. When they are removed, or new elements are added later, direct events don't work.

How to configure ObjectMultiCheckBox to persist an empty element?

I have the following element in my form, and I tried all possible options found in the web to allow empty value for element:
$this->add(array(
'type' => 'DoctrineModule\Form\Element\ObjectMultiCheckbox',
'name' => 'directPractice',
'options' => array(
'label' => 'A. Check all direct practice field education assignments',
'label_attributes' => array(
'class' => 'label-multicheckbox-group'
),
'required' => false,
'allow_empty' => true,
'continue_if_empty' => false,
'object_manager' => $this->getObjectManager(),
'target_class' => 'OnlineFieldEvaluation\Entity\FieldEducationAssignments', //'YOUR ENTITY NAMESPACE'
'property' => 'directPractice', //'your db collumn name'
'disable_inarray_validator' => true,
'value_options' => array(
'1' => 'Adults',
'2' => 'Individuals',
'3' => 'Information and Referral',
'4' => 'Families',
array(
'value' => 'Other',
'label' => 'Other (specify)',
'label_attributes' => array(
'class' => 'bindto',
'data-bindit_id' => 'otherDirectPracticeTxt_ID'
),
'attributes' => array(
'id' => 'otherDirectPractice_ID',
),
)
),
),
'attributes' => array(
'value' => '1', //set checked to '1'
'multiple' => true,
)
));
And I am always getting the same error message when it is empty:
Validation failure 'directPractice':Array
(
[isEmpty] => Value is required and can't be empty
)
Ok, this is a best what I could come up after researching on it. Solution is inspired by these question.
Form: hidden field and ObjectMultiCheckBox
public function init()
{
$this->setAttribute('method', 'post');
$this->setAttribute('novalidate', 'novalidate');
//hidden field to return empty value. If checkbox selected, checkbox values will be stored with empty value in Json notation
$this->add(array(
'type' => 'Hidden',
'name' => 'otherLearningExperiences[]', // imitates checkbox name
'attributes' => array(
'value' => null
)
));
$this->add(array(
// 'type' => 'Zend\Form\Element\MultiCheckbox',
'type' => 'DoctrineModule\Form\Element\ObjectMultiCheckbox',
'name' => 'otherLearningExperiences',
'options' => array(
'label' => 'C. Check other learning experiences',
'object_manager' => $this->getObjectManager(),
'target_class' => 'OnlineFieldEvaluation\Entity\FieldEducationAssignments',
'property' => 'otherLearningExperiences',
'label_attributes' => array(
'id' => 'macro_practice_label',
'class' => 'control-label label-multicheckbox-group'
),
'value_options' => array(
array(
'value' => 'seminars',
'label' => 'Seminars, In-Service Training/Conferences',
'label_attributes' => array(
'class' => 'bindto',
'data-bindit_id' => 'seminarsTxt_ID'
),
'attributes' => array(
'id' => 'seminars_ID',
),
),
array(
'value' => 'other',
'label' => 'Other (specify)',
'label_attributes' => array(
'class' => 'bindto',
'data-bindit_id' => 'otherLeaningExperiencesTxt_ID'
),
'attributes' => array(
'id' => 'otherLeaningExperiences_ID',
),
)
),
),
'attributes' => array(
//'value' => '1', //set checked to '1'
'multiple' => true,
'empty_option' => '',
'required' =>false,
'allow_empty' => true,
'continue_if_empty' => false,
)
));
Validator
$inputFilter->add($factory->createInput(array(
'name' => 'otherLearningExperiences',
'required' => false,
'allow_empty' => true,
'continue_if_empty' => true,
)));
}
View
//hidden field to be posted for empty value in multicheckbox
echo $this->formHidden($form->get('otherLearningExperiences[]'));
$element = $form->get('otherLearningExperiences');
echo $this->formLabel($element);
echo $this->formMultiCheckbox($element, 'prepend');

Yiibooster editable widget inside popover widget

I'm building website using yii 1.1 framework and Clevertech YiiBooster extension.
Is there an easy way to render few "editable field" widgets inside the popover widget ?
Please see the code below. Any suggestions are welcome.
Main view:
....
$this->widget(
'booster.widgets.TbButton', array(
'label' => 'Right popover',
'context' => 'warning',
'buttonType' => 'button',
'size' => 'extra_small',
'htmlOptions' => array(
'id' => uniqid(),
'data-html' => true,
'data-title' => 'A Title',
'data-placement' => 'right',
'data-content' => $this->renderPartial('VIEW', array('data'=>$data), true, true),
'data-toggle' => 'popover'
),
)
);
....
partial view:
$this->widget('booster.widgets.TbEditableField', array(
'type' => 'text',
'model' => $survey,
'attribute' => 'text',
'pk' => 'pk',
'name' => 'name',
'text' => '',
'url' => $this->createUrl('controller/method/'), //url for submit data
'title' => 'text',
'placement' => 'right',
'id' => uniqid()),true);
$this->widget('booster.widgets.TbEditableField', array(
'type' => 'text',
'model' => $survey,
'attribute' => 'text',
'pk' => 'pk',
'name' => 'name',
'text' => '',
'url' => $this->createUrl('controller/method/'), //url for submit data
'title' => 'text',
'placement' => 'right',
'id' => uniqid()),true);
$this->widget('booster.widgets.TbEditableField', array(
'type' => 'text',
'model' => $survey,
'attribute' => 'text',
'pk' => 'pk',
'name' => 'name',
'text' => '',
'url' => $this->createUrl('controller/method/'), //url for submit data
'title' => 'text',
'placement' => 'right',
'id' => uniqid()),true);

Categories