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?
Related
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.
I have a Tabs::widget that all settings are located in different tabs in a ActiveForm and admin can set config in each tab and once submit.(multiple forms in one widget )
in setting view :
<?php $form = ActiveForm::begin(); ?>
<?php
echo \yii\jui\Tabs::widget([
'headerOptions' => ['class' => 'tabs'],
'itemOptions' => ['tag' => 'div'],
'items' => [
[
'label' => 'serverSetting',
'content' => $this->render('serverSetting', ['model' => $model, 'form' => $form]),
'active' => true
],
[
'label' => 'emailSetting',
'content' => $this->render('emailSetting', ['model' => $model, 'form' => $form]),
],
[
'label' => 'smsSetting',
'content' => $this->render('smsSetting', ['model' => $model, 'form' => $form]),
],
],
]);
?>
<div class="btnForm">
<?= Html::submitButton(Yii::t('app', 'ثبت', ['class' => 'btn btn-primary', 'name' => ''])) ?>
</div>
<?php ActiveForm::end(); ?>
in view of one of the tabs (smsServer view):
<?php
use yii\helpers\Html;
use app\components\ActiveForm;
?>
<div class="user-form">
<?= $form->field($model, 'login')->textInput(['placeholder' => 'host']) ?>
<?= $form->field($model, 'password1')->textInput(['placeholder' => 'username']) ?>
<?= $form->field($model, 'wsdl')->textInput(['placeholder' => 'password']) ?>
<?= $form->field($model, 'from1')->textInput(['placeholder' => 'port']) ?>
</div>
in controller :
public function actionSetting()
{
$model = new Setting();
$model->setAttributes(Yii::$app->params, false);
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
file_put_contents(Yii::getAlias('#app/config') . DIRECTORY_SEPARATOR . 'extra_params.php', base64_encode(serialize($model->attributes)));
}
return $this->render('setting', [
'model' => $model,
]);
}
My form and submit button do not work. where is my problem?
Seems you don't have an ActiveForm::end()
.......
<div class="btnForm">
<?= Html::submitButton(Yii::t('app', 'ثبت', ['class' => 'btn btn-primary'])) ?>
</div>
<?php ActiveForm::end(); ?>
and for debugging don't suppress the name of button
So I found my answer. My validation was false.
in rule():
[['login', 'wsdl', 'password','from1'], 'required',],
But I post form without filled all fields.
I changed to
[['login',], 'required',],
[['login', 'wsdl', 'password',from1'], 'safe',],
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']
)
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)
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.