I'm trying to use the same dropdown twice and have it work when storing the record in Cake 3.
The referenced table in this case is 'responsible_people' and the referencing table is 'organisation_details'. The FK in the latter is 'responsible_people_id'. Nothing special going on. If I bake it as it is, it's fine. I change the displayField in ResponsiblePeopleTable.php to 'full_name' and away we go, a dropdown as expected.
Does anyone know how I can extend this and have, say, two fields on the Org Details add page populated by the responsible people table and have it save correctly?
This is the controller's add section:
public function add()
{
$organisationDetail = $this->OrganisationDetails->newEntity();
if ($this->request->is('post')) {
$organisationDetail = $this->OrganisationDetails->patchEntity($organisationDetail, $this->request->data);
if ($this->OrganisationDetails->save($organisationDetail)) {
$this->Flash->success(__('The organisation detail has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The organisation detail could not be saved. Please, try again.'));
}
}
$responsiblePeople = $this->OrganisationDetails->ResponsiblePeople->find('list', ['limit' => 200]);
$this->set(compact('organisationDetail', 'responsiblePeople'));
$this->set('_serialize', ['organisationDetail']);
}
This is the add.ctp:
<nav class="large-3 medium-4 columns" id="actions-sidebar">
<ul class="side-nav">
<li class="heading"><?= __('Actions') ?></li>
<li><?= $this->Html->link(__('List Organisation Details'), ['action' => 'index']) ?></li>
<li><?= $this->Html->link(__('List Responsible People'), ['controller' => 'ResponsiblePeople', 'action' => 'index']) ?></li>
<li><?= $this->Html->link(__('New Responsible Person'), ['controller' => 'ResponsiblePeople', 'action' => 'add']) ?></li>
</ul>
</nav>
<div class="organisationDetails form large-9 medium-8 columns content">
<?= $this->Form->create($organisationDetail) ?>
<fieldset>
<legend><?= __('Add Organisation Detail') ?></legend>
<?php
echo $this->Form->input('organisation_name');
echo $this->Form->input('organisation_address');
echo $this->Form->input('organisation_secondary_addresses');
echo $this->Form->input('organisation_email');
echo $this->Form->input('organisation_telephone');
echo $this->Form->input('organisation_employees');
echo $this->Form->input('organisation_contractors');
echo $this->Form->input('organisation_review', ['empty' => true, 'default' => '']);
echo $this->Form->input('organisation_external_assessment', ['empty' => true, 'default' => '']);
echo $this->Form->input('responsible_people_id', ['options' => $responsiblePeople]);
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
The last form input produces a nice dropdown with the names of the responsible people ready for selection. I'd like to work out how to have two of those.
At the moment you have only one dropdown on your form.
If you set name of your dropdowns then when you submit your form you will get values of each drop down.
echo $this->Form->input('responsible_people_id1', ['name'=>'dropdown1', 'options' => $responsiblePeople]);
echo $this->Form->input('responsible_people_id2', ['name'=>'dropdown2', 'options' => $responsiblePeople]);
The other option to create a dropdown would be using Form->select(). e.g.
echo $this->Form->select(
'field',
[1, 2, 3, 4, 5],
['empty' => '(choose one)']
);
Please check here for more information about Cakephp3.x Form Helper
Related
at the moment I have a list of products and each product has an array of categories (every array has a least 1 category object), I also made a filter for the categories so when I chose them as checkboxes then clicked submit, I'll get an array of the categories' ID. But now I don't really know how I can retrieve all the resources that have categories that match with the filtered ones. I'm very sorry if my explanation is not very clear.
Here is my code:
filter.php
<section class="section service border-top">
<?= $this->Form->create(null, ['novalidate' => true, 'type' => 'get']) ?>
<fieldset>
<?php echo $this->Form->control('categories._ids', array('multiple' => 'checkbox', 'options' => $categories));?>
</fieldset>
</br>
<?= $this->Form->button(__('Filter'), ['class' =>'logbtn']) ?>
<?= $this->Form->end() ?>
</section>
productsController.php
public function customerAllProducts()
{
$products = $this->paginate($this->Products);
$this->paginate = [
'contain' => ["Categories"],
];
$categories_filter = $this->request->getQuery('categories._ids');
if(!empty($categories_filter)){
foreach ($categories_filter as $category_filter){
echo $category_filter; // Print out all filtered categories' IDs
}
}
$categries = $this->Productss->Categoriess->find('list', ['limit' => 200]);
$this->set(compact('products','categories'));
}
Thank you
I mimicked the bookmarker tutorial to setup the basic files/logic needed to have tags associated to my products, but I can't get the 'sortWhiteList' functionality to work with the belongsToMany association. Any thoughts?
The model(entity and table) are working as the tags get saved.
The following is just one way I have tried it. I have tried many different scenarios, looking at the documentation, searching online, but I can't figure it out. Any help is much appreciated.
Controller:
public function index()
{
$this->paginate = [
'sortWhitelist' => [
'Products.title',
'Products.affiliate_link',
'Products.msrp',
'Products.sale_price',
'Products.Tags.name',
],
'contain' => ['Tags'],
'limit' => 48,
'order' => ['Products.title' => 'asc']
];
$products = $this->Products->find();
$this->set('products', $this->paginate($products));
$this->set('_serialize', ['products']);
}
View:
<ul class="side-nav no-bullet">
<li><h3><?= __('Sort') ?></h3></li>
<li><?= $this->Paginator->sort('Products.title', 'Title') ?></li>
<li><?= $this->Paginator->sort('Products.msrp', 'Retail Price') ?></li>
<li><?= $this->Paginator->sort('Products.sale_price', 'Sale Price') ?></li>
<li><?= $this->Paginator->sort('Products.Tags.name', 'Tag Name') ?></li>
</ul>
I am new to Cakephp 3.0 i have created a form
<?php echo $this->Form->create('Login', array('url' => array('controller' => 'Login', 'action' => 'dashboard'))); ?>
<label for="login-username">username</label>
<?= $this->Form->input('username'); ?>
<label for="login-password">password</label>
<?= $this->Form->input('password', array('type'=>'password')); ?>
<?= $this->Form->submit('Login',array('class' => 'button round blue image-right ic-right-arrow')); ?>
<?= $this->Form->end() ?>
I have to redirect it to Login controller and dashboard action, but in inspect element i can see /stock_mgmt_system/login/dashboard where stock_mgmt_system is the project name.
Please try to solve my issue.
Did you named your controller LoginsController respecting cake convention ?
If yes just replace "login" with "logins" in "controller" just like that
echo $this->Form->create(null, [
'url' => ['controller' => 'Logins', 'action' => 'dashboard']
]);
https://book.cakephp.org/3.0/fr/views/helpers/form.html
Here's my current code within an index.phtml viewscript:
<?= $this->paginationControl($posts,'Sliding','application/partial/paginator', ['route' => 'home','lang'=>'it']); ?>
I'd like to pass the :lang parameter within this paginationControl call so that way the router is notified and the html results show the it lang inside the pagination html ahref code for clickable links.
I'm not quite sure how to correctly do this.
Here's my route:
'home' => [
'type' => Segment::class,
'options' => [
'route' => '/:lang',
'defaults' => [
'controller' => ApplicationIndexController::class,
'action' => 'index',
'lang' => 'en'
],
],
],
The resulting html from this paginator will show:
/pp/public/it?page=2
But it currently shows
/pp/public/en?page=2
even when im on the italian version of the page
Well it depends on how you setup your paginationControl partial or viewpage script.
The paginationControl parameters:
PaginationControl::__invoke(Paginator $myPaginator, $scrollingStyle, $partial, $params);
So within your partial or viewpage script for the pagination you are able to access all the stuff you passed to the $params parameter, like you would with your parameters you pass from your controller to your view pages or the partial viewhelper.
You could pass a parameter to the partial like the route to use and its route and query parameters.
$this->paginationControl(
$posts,
'sliding',
'application/partial/pagination',
[
'route' => 'home',
'routeParams' => ['lang' => 'it'],
'queryParams' => []
]
);
So now within your pagination partial you could use the route, routeParams and queryParams - Template used - Item pagination.
<?php
if (!isset($queryParams)) {
$queryParams = [];
}
if (!isset($routeParams)) {
$routeParams = [];
}
?>
<?php if ($this->pageCount): ?>
<div class="paginationControl">
<?= $this->firstItemNumber; ?> - <?= $this->lastItemNumber; ?>
<?= $this->translate('of'); ?> <?= $this->totalItemCount; ?>
<!-- First page link -->
<?php if (isset($this->previous)): ?>
<a href="<?= $this->url(
$this->route,
$routeParams,
ArrayUtils::merge($queryParams, ['query' => ['page' => $this->first]])
); ?>">
<?= $this->translate('First'); ?>
</a> |
<?php else: ?>
<span class="disabled"><?= $this->translate('First') ?></span> |
<?php endif; ?>
<!-- Previous page link -->
<?php if (isset($this->previous)): ?>
<a href="<?= $this->url(
$this->route,
$queryParams,
ArrayUtils::merge($queryParams, ['query' => ['page' => $this->previous]])
); ?>">
< <?= $this->translate('Previous') ?>
</a> |
<?php else: ?>
<span class="disabled">< <?= $this->translate('Previous') ?></span> |
<?php endif; ?>
<!-- Next page link -->
<?php if (isset($this->next)): ?>
<a href="<?= $this->url(
$this->route,
$routeParams,
ArrayUtils::merge($queryParams, ['query' => ['page' => $this->next]])
); ?>">
<?= $this->translate('Next') ?> >
</a> |
<?php else: ?>
<span class="disabled"><?= $this->translate('Next') ?> ></span> |
<?php endif; ?>
<!-- Last page link -->
<?php if (isset($this->next)): ?>
<a href="<?= $this->url(
$this->route,
$routeParams,
ArrayUtils::merge($queryParams, ['query' => ['page' => $this->last]])
); ?>">
<?= $this->translate('Last') ?>
</a>
<?php else: ?>
<span class="disabled"><?= $this->translate('Last') ?></span>
<?php endif; ?>
I have a login form in my header that talks to my users_controller but the form itself isn't in a view being generated by the users controller so I get two problems
1.) The password field doesn't get treat like a password field and is just a normal text field
2.) When submitting the form it just redirects to the login action
Here is the code and it's used in BOTH the login view and in my header (so I know it works):
<?php echo $this->Form->create(null, array('id' => 'loginform', 'type' => 'post',
'url' => array('controller' => 'users', 'action' => 'login'))); ?>
<fieldset id="login">
<ul class="clearfix">
<li id="li-username">
<?php echo $this->Form->input('username', array('label'=>false,'placeholder'=>'Username or email address')); ?>
</li>
<li id="li-password">
<?php echo $this->Form->input('password', array('label'=>false,'placeholder'=>'Password')); ?>
<span id="iforgot"><?php echo $this->Html->link('?',
array('controller' => 'users', 'action' => 'forgotpassword'), array('title' => 'Forgot your password?')); ?></span>
</li>
<li id="li-submit">
<button type="submit" title="Log in">Log in ►</button>
</li>
</ul>
</fieldset>
<?php echo $this->Form->end(); ?>
Why not changing
Form->create(null,...
to
Form->create('User',...
otherwise change
Form->input('username',...
to
Form->input('User.username',...
You can use type = 'password'
echo $this->Form->input('password', array('label'=>false, 'type' => 'password', 'placeholder'=>'Password'));
You have specified array('controller' => 'users', 'action' => 'login') in your $this->Form->create() statement. So the form is submitted to "/users/login"
If you don't want to submit the form to '/users/login', you can use AJAX to perform login.
Hope these will answer your queries.