How to create icon inside form postlink with cakephp and twitter bootstrap - forms

This gives me what I want:
<?php echo $this->Html->link(
$this->Html->tag('i', '', array('class' => 'glyphicon glyphicon-edit')) . " Edit",
array('action' => 'edit', $comment['Comment']['comment_id']),
array('class' => 'btn btn-mini', 'escape' => false)
); ?>
But when I create a Form postLink I don't know how to get the remove icon in front of it..
<?php echo $this->Form->postLink(
$this->Html->tag('i', '', array('class' => 'glyphicon glyphicon-remove')) . " Delete",
array('action' => 'delete', $comment['Comment']['comment_id']), null, __('Are you sure you want to delete # %s?', $comment['Comment']['comment_id']),
array('class' => 'btn btn-mini', 'escape' => false)
); ?>
It gives me <i class="glyphicon glyphicon-remove"></i> Delete

You forgot add option escape to false
echo $this->Form->postLink(
$this->Html->tag('i', '', array('class' => 'glyphicon glyphicon-remove')). " Delete",
array('action' => 'delete', $comment['Comment']['comment_id']),
array('escape'=>false),
__('Are you sure you want to delete # %s?', $comment['Comment']['comment_id']),
array('class' => 'btn btn-mini')
);

using a button
<?php echo $this->Form->postLink(
'<button class="btn btn-danger">
<i class="icon-trash icon-white"></i>
</button>',
array(
'action' => 'delete', $post['Post']['id']
),
array(
'class' => 'tip',
'escape' => false,
'confirm' => 'Are you sure ?'
));
?>

Try this:
<?php
echo $this->Form->postLink(
'Delete',
array('controller'=>'Comments',
'class'=>'glyphicon glyphicon-remove','action' => 'delete',$comment['id']),
array('confirm' => 'Are you sure?')
);
?>

The below code will create Link Button for deleting Items with a Confirmation box.
$this->Form->postLink( 'Delete Item',
['action' => 'delete', 'paramId' => $item->id ],
['confirm' => __('Are you sure you want to delete this Item?'), 'class'=> 'btn btn-outline-danger']
)

Related

In Cakephp 3.6 How do I get the size and type of an image to send it by form?

How do I get the size and type of an image to send it by form?
Well that, I want to save in the table of images the size and type of an image that is uploaded through a form. With Ajax, I can recover those data, but to pass them to PHP I can only do when sending the form, also directly with PHP:
if ($this->request->is('post')) {
$isData = $this->request->getdata();
$imagene->imagen = $isData['image_path'];
$imagene->tipo = $isData['type']
$imagene->tamano = $isData['size'];
...
But I want to do it before sending the form, which is when the insertion is done in the database.
Form:
<?= $this->Form->create($imagene, ['novalidate', 'id' => 'addimageform', 'class' => 'form addimageform']); ?>
<?= $this->Form->control('imagen', ['type' => 'file', 'class' => 'imagen-addimage']); ?>
<?= $this->Form->hidden('tipo', ['value' => $tipo, 'class' => 'tipo-addimage']); ?>
<?= $this->Form->hidden('$tipo', ['value' => $size, 'class' => 'tamano-addimage']); ?>
<?= $this->Form->button('Subir imagen', ['id' => 'submit', 'class' => 'submit-addimage']); ?>
<?= $this->Form->button('Omitir', ['id' => 'omitir', 'class' => 'omitir-addimage', 'redirect' => ['controller' => 'administracion', 'action' => 'index']]); ?>
<?= $this->Form->end(); ?>
Now I see that if I do a debug of $isData, the field: "imagen" does not appear:
'tabla' => 'users',
'id_tabla' => '22',
'tipo' => '',
'tamano' => ''
UPDATING
I've changed things in the form and the controller:
form:
<?= $this->Form->create($imagene, ['enctype' => 'multipart/form-data', 'novalidate', 'id' => 'addimageform', 'class' => 'form addimageform']); ?>
<?= $this->Form->control('imagen', ['type' => 'file', 'class' => 'imagen-addimage']); ?>
<div class="centrar-submit">
<?= $this->Form->button('Subir imagen', ['id' => 'submit', 'class' => 'submit-addimage']); ?>
<?= $this->Form->button('Omitir', ['id' => 'omitir', 'class' => 'omitir-addimage', 'redirect' => ['controller' => 'administracion', 'action' => 'index']]); ?>
</div>
<?= $this->Form->end(); ?>
Controller:
public function add($table, $idTable) {
$imagene = $this->Imagenes->newEntity();
if ($this->request->is('post')) {
$isData = $this->request->getdata();
debug($this->request->getData('imagen')); // <---- Is null
debug($isData); // <---- Is empty
...
Why? I don't know.
You should mentioned what type of form you used. That is file type you should use
echo $this->Form->create($article, ['type' => 'file']);
Hope this help you.

CakePHP saveMany doesnt save

I have the following Form Code.
<?php echo $this
->Form
->create(
'PagePhoto',
array(
'type' => 'file',
'url' => array(
'controller' => 'page_photos',
'action' => 'add'
)
)
); ?>
<div class="modal-body has-padding">
<div class="form-group">
<?php echo $this->Form->label('PagePhoto.0.filename', 'Photos:'); ?>
<br/><br/>
<?php echo $this->Form->file('PagePhoto.0.filename', array('required' => false)); ?>
<?php echo $this->Form->error('PagePhoto.0.filename', null, array('class' => 'label label-block label-danger text-left', 'wrap' => 'label')); ?>
<br/>
<?php echo $this->Form->file('PagePhoto.1.filename', array('required' => false)); ?>
<?php echo $this->Form->error('PagePhoto.1.filename', null, array('class' => 'label label-block label-danger text-left', 'wrap' => 'label')); ?>
<br/>
<?php echo $this->Form->file('PagePhoto.2.filename', array('required' => false)); ?>
<?php echo $this->Form->error('PagePhoto.2.filename', null, array('class' => 'label label-block label-danger text-left', 'wrap' => 'label')); ?>
<br/>
<?php echo $this->Form->file('PagePhoto.3.filename', array('required' => false)); ?>
<?php echo $this->Form->error('PagePhoto.3.filename', null, array('class' => 'label label-block label-danger text-left', 'wrap' => 'label')); ?>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-warning" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Upload Photos</button>
</div>
Which returns the following array to the add action
array(
'PagePhoto' => array(
(int) 0 => array(
'filename' => array(
'name' => 'IMG_1683.jpg',
'type' => 'image/jpeg',
'tmp_name' => '/Applications/MAMP/tmp/php/phpoIrbZ6',
'error' => (int) 0,
'size' => (int) 94131
)
),
(int) 1 => array(
'filename' => array(
'name' => 'IMG_1683.jpg',
'type' => 'image/jpeg',
'tmp_name' => '/Applications/MAMP/tmp/php/phpAcbXbC',
'error' => (int) 0,
'size' => (int) 94131
)
),
(int) 2 => array(
'filename' => array(
'name' => 'IMG_1683.jpg',
'type' => 'image/jpeg',
'tmp_name' => '/Applications/MAMP/tmp/php/phppCeN8G',
'error' => (int) 0,
'size' => (int) 94131
)
),
(int) 3 => array(
'filename' => array(
'name' => 'IMG_1683.jpg',
'type' => 'image/jpeg',
'tmp_name' => '/Applications/MAMP/tmp/php/php8Ib3bO',
'error' => (int) 0,
'size' => (int) 94131
)
)
)
)
Add Action
public function admin_add() {
if ($this->request->is('post')) {
debug($this->request->data);
$this->PagePhoto->create();
if ($this->PagePhoto->saveMany($this->request->data)) {
$this->Session->setFlash(__('The gallery has been saved.'), 'admin/flash_success');
// return $this->redirect(array('action' => 'view', $this->request->data['Gallery']['album_id']));
} else {
$this->Session->setFlash(__('The gallery could not be saved. Please, try again.'), 'admin/flash_error');
}
} else {
// $this->request->data['Gallery']['album_id'] = $album_id;
}
}
Error:
FAILURE: The gallery could not be saved. Please, try again.
$this->PagePhoto->saveMany($this->request->data['PagePhoto']);
Note: When saving multiple records of same model the records arrays should be just numerically indexed without the model key.

CakePHP send form depending on data

Hello I'm fairly new to cakephp, I've just done two tutorials hehehe but now I'm working on it and I have a register form in which I register a user, But depending on the type of the user, more models will be shown and sent to the controller to register it. I was wondering which is the best practice to do this? Do it in separate forms, or sending the information depending on the user type?
Here is my View:
<?php echo $this->Form->create('User', array('type' => 'file')); ?>
<fieldset>
<legend><?php echo __('Add User'); ?></legend>
<?php
echo $this->Form->file('User.user_image');
echo $this->Form->input('User.name', array( 'label' => 'Nombre' ));
echo $this->Form->input('User.last_name', array( 'label' => 'Apellidos' ));
echo $this->Form->input('User.email', array( 'label' => 'E-Mail' ));
echo $this->Form->input('User.password', array( 'label' => 'Contraseña' ));
echo $this->Form->input('User.phone', array( 'label' => 'Telefono' ));
//echo $this->Form->input('created_ip_connection');
//echo $this->Form->input('last_ip_connection');
echo $this->Form->input('User.group_id', array('empty' => 'Elige un rol', 'label' => 'Rol de Usuario'));
?>
<div style="display: none;" id="companyAdd">
<legend><?php echo __('Datos de Compañia'); ?></legend>
<?php
echo $this->Form->input('Address.exterior_number', array( 'label' => 'Numero Exterior' ));
echo $this->Form->input('Address.internal_number', array( 'label' => 'Numero Interior' ));
echo $this->Form->input('Address.street', array( 'label' => 'Calle' ));
echo $this->Form->input('Address.suburby', array( 'label' => 'Colonia' ));
echo $this->Form->input('Address.country_id', array('empty' => 'Selecciona País', 'label' => 'Pais'));
echo $this->Form->input('Address.state_id', array('empty' => 'Selecciona País', 'label' => 'Estado'));
echo $this->Form->input('Address.city_id', array('empty' => 'Selecciona Estado', 'label' => 'Municipio'));
echo $this->Form->input('Company.name', array( 'label' => 'Nombre de Compañia' ));
echo $this->Form->input('Company.description', array( 'label' => 'Descripción de Compañia' ));
echo $this->Form->input('Company.bank_acc', array( 'label' => 'Cuenta de Banco' ));
echo $this->Form->input('Company.rfc', array( 'label' => 'RFC' ));
?>
</div>
<div style="display: none;" id="userAdd">
<legend><?php echo __('Datos de Comprador/Proveedor'); ?></legend>
<?php
echo $this->Form->input('Buyer.company_id', array('empty' => 'Elige Comapñia', 'label' => 'Compañia'));
?>
</div>
</fieldset>
<?php echo $this->Form->end(__('Registrar')); ?>
In this if the user has a certain group_id the companyAdd div will be visible for editing, if its another the userAdd div will be shown and if hes admin only the info of the user will be sent. When I try to submit this, the form wont let me cause the inputs for company and user are necessary. How can I control which data is sent depending on the group_id of the user?
The models goes as follow
User hasOne Buyer
User hasOne Provider(NormalUsers)
User hasOne Comapny
By the way Comapny has an address too thats why Address is another model
In the controller I'm thinking over use saveAll or a save per model sent.
Any suggestions or tutorials I can see how this is make ? or Best practices?

Zend Framework 2 formInput or formElement ID tag not rendering

In Zend framework 2, when I use the view's formRow method like so
$this->formRow($form->get('product_name'));
it will generate HTML like this
<label for="product_name">
<span>Name</span>
<input type="text" id="product_name" name="product_name">
</label>
but if I use formInput
<div class="control-group">
<?php echo $this->formLabel($form->get('product_name')->setLabelAttributes(array('class'=>'control-label'))); ?>
<div class="controls">
<?php echo $this->formInput($form->get('product_name')); ?>
</div>
</div>
$this->formInput($form->get('product_name'));
i don't get the id tag
<input type="" name="product_name">
I've tried with formElement with same results.
How can I get it to render just the input with all attributes and values?
This is how my Zend Framework 2 View looks like (simplified)
<?php echo $this->form()->openTag($form); ?>
<div class="control-group">
<?php echo $this->formLabel($form->get('product_name')->setLabelAttributes(array('class'=>'control-label'))); ?>
<div class="controls"><?php echo $this->formInput($form->get('product_name')); ?></div>
</div>
<div class="control-group">
<div class="controls"><?php echo $this->formSubmit($form->get('submit')); ?></div>
</div>
<?php echo $this->form()->closeTag(); ?>
and the Zend Framework 2 Form
<?php
namespace Product\Form;
use Zend\Form\Form;
class ProductForm extends Form
{
public function __construct($name = null)
{
// we want to ignore the name passed
parent::__construct('product');
$this->setAttribute('method', 'post');
$this->setAttribute('class','form-horizontal');
$this->add(array(
'name' => 'product_name',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'Name',
),
));
$this->add(array(
'name' => 'submit',
'attributes' => array(
'type' => 'submit',
'value' => 'Save',
'id' => 'submitbutton',
'class'=>'btn btn-success btn-large'
),
));
}
}
?>
Change:
$this->add(array(
'name' => 'product_name',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'Name',
),
));
to:
$this->add(array(
'name' => 'product_name',
'attributes' => array(
'type' => 'text',
'id' => 'product_name',
),
'options' => array(
'label' => 'Name',
),
));
Actually this code:
$this->add(array(
'type' => 'Zend\Form\Element\Text',
'name' => 'product_name',
'attributes' => array(
'id' => 'product_name',
'class' => 'span3',
),
'options' => array(
'label' => 'Your label',
),
));
could be used correctly by a css renderer like Bootstrap due to fact that the $this->formRow(...) form helper will produce:
<label for="product_name">Your label</label><input type="text" name="product_name" class="span3" id="product_name" value="">
which is more readable than the original formRow output (without id neither class attributes)

How to handle multiple forms in the same view?

I have a default form for my specific view.
Through an element I (dinamically) include another view (using view extension) in order to provide an upload form.
My problem is that the second form seems to submit the first one.
Default form
<div class="content-box-content">
<?php
echo $this->Form->create("WebSubject", array(
'inputDefaults' => array(
'error' => array(
'attributes' => array(
'wrap' => 'span',
'class' => 'input-notification error png_bg'
)
)
)
));
?>
<?=$this->Form->input('id', array('type' => 'hidden'))?>
<?=$this->Form->input('title', array('class' => "text-input small-input", 'label' => 'Denumire'))?>
<?=$this->Form->input('description', array('type' => 'textarea', 'label' => 'Descriere', 'id' => 'description'))?>
<?=$this->Form->input('description_long', array('type' => 'textarea', 'label' => 'Continut', 'id' => 'description_long'))?>
<?=$this->Form->submit('Salveaza', array('class' => "button"))?>
</div>
This way I include the element
<div class="tab-content default-tab" id="fotoUploadTab">
<?php
echo $this->element('file_upload_form', array(
'view' => 'upload_admin',
'webFileType' => 'image',
'redirect' => $SHT['here']
)
);
?>
<div class="tab-content default-tab">
Lista imagini
</div>
</div>
Element code
<?php
$view = (isset($view)) ? $view : "upload_admin";
$webFileType = (isset($webFileType)) ? $webFileType : "image";
$redirect = (isset($redirect)) ? $redirect : "/";
?>
<?php
$this->extend("/WebFiles/".$view);
?>
Extended View code
<div class="tab-content default-tab">
<?php echo $this->Form->create("WebFile", array('action' => '/', 'type' => 'file')); ?>
<input type="hidden" name="redirect" value="" />
<?php echo $this->Form->input('entity_id', array('type' => 'hidden')); ?>
<?php echo $this->Form->input('entity_table_name', array('type' => 'hidden')); ?>
<?php echo $this->Form->input('type', array('type' => 'hidden')); ?>
<?php echo $this->Form->input('title', array('class' => "text-input small-input", 'label' => 'Denumire')); ?>
<?php echo $this->Form->input('description', array('class' => "text-input small-input", 'label' => 'Descriere')); ?>
<?php echo $this->Form->submit('Upload', array('class' => 'button')); ?>
</div>
As seen in the last snippet, I tried to force the last form by providing an action url, but on submiting it, it sends data as the first one does.
How should I handle this ?
Thank you!
If you just want to have both forms, the one from parent and the other from the child view/element make sure you call $this->Form->end() in both templates and that you are not nesting a form inside the other. Probably, in your case, just by adding end() to both forms will solve your issue.
As a side note, you cannot have a parent view opening a Form with $this->Form->create() and inject fields into it using the child view, basically because you need create() to be called before any input is rendered and parent views are rendered after the child is executed.