CakePHP Form outside controller - php

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.

Related

Cannot remove project name from the form action in Cakephp 3.0

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

Multiple dropdowns populated by one related table CakePHP 3

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

yii CActiveForm client validation not working on some field

In my yii application i have a simple form that contains two fields like this:
<?php
$form = $this->beginWidget('CActiveForm', array(
'id' => 'message-form',
'enableClientValidation' => true,
//'enableAjaxValidation' => true,
'clientOptions' => array(
'validateOnSubmit' => true,
'validateOnChange' => false,
'validateOnType' => false,
'errorCssClass' => 'has-error',
'successCssClass' => 'has-success',
)));
?>
<div class="form-group">
<div class="row">
<div class="col-md-6">
<?php echo $form->labelEx($model, 'subject'); ?>
<?php echo $form->textField($model, 'subject', array('class' => 'form-control')); ?>
<?php echo $form->error($model, 'subject', array('class' => 'alert alert-danger')); ?>
</div>
<div class="col-md-5"></div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-md-6">
<?php echo $form->labelEx($model, 'message'); ?>
<?php echo $form->textField($model, 'message', array('class' => 'form-control', 'id'=>'message')); ?>
<?php echo $form->error($model, 'message', array('class' => 'alert alert-danger')); ?>
</div>
</div>
</div>
......
//remaining of code
"subject" and "message" are two fields of "Notification" model. in Notification model, i have defined this rule:
array('subject, message', 'required')
my problem is validation for "subject" works but validation for "message" not works!
after submitting the form, error message of "subject" shows up but "message" does not have any error. can anyone help me solve this problem?
I use Yii 1.1.15
in http://www.yiiframework.com/doc/api/1.1/CActiveForm u can see example
<?php $form = $this->beginWidget('CActiveForm', array(
'id'=>'user-form',
'enableAjaxValidation'=>true,
'enableClientValidation'=>true,
'focus'=>array($model,'firstName'),
)); ?>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'firstName'); ?>
<?php echo $form->textField($model,'firstName'); ?>
<?php echo $form->error($model,'firstName'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'lastName'); ?>
<?php echo $form->textField($model,'lastName'); ?>
<?php echo $form->error($model,'lastName'); ?>
</div>
<?php $this->endWidget(); ?>
i think u set enableAjaxValidation = true
Is there a reason for HTML attribute "id" => "message" on your message field? Yii CActiveForm generate a specific HTML id : modelName_fieldName for each field.
You don't need to set id manually (like your subject field; it don't have id). Yii needs those format of ids for validations, get/post data and more things (as client/js functions).
Remove "id" => "message" on your message field and test again.

laravel form post instead of put

This is my form:
{{ Form::model($data, array(
'route' => array('waitingtimes.update', $data->id),
'class' => 'mainInformationContrainer',
'method' => 'put'
)) }}
When I submit the form, I got
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
though I've already set the request as put.
Could you help me please?
Edit 1
I noticed that the form html is
<form method="POST" action="http://localhost:8082/test/public/waitingtimes/2" accept-charset="UTF-8" class="mainInformationContrainer">
</form>
It is post not put,
Edit 2
The problem was because I mistyped the route to rout, but not I am getting this exception
Trying to get property of non-object
this is the view:
{{Form::model($data, array(
'route' => array('waitingtimes.update', $data->id)
, 'class' => 'mainInformationContrainer',
'method' => 'put'
))}}
<ul>
<li>
<label>First Time:</label>
<div class="oneInfo">
{{ Form::text('startTime', $value=null, array('class' => 'time ui-timepicker-input', 'id' => 'startTime', 'autocomplete' => 'off'))}}
<span class="errorMessage">
<?php
echo $errors->first('startTime');
?>
</span>
</div>
</li>
<li>
<label>End Time:</label>
<div class="oneInfo">
{{Form::text('endTime', $value=null, array('class' => 'time ui-timepicker-input', 'id' => 'endTime'))}}
<span class="errorMessage">
<?php
echo $errors->first('endTime');
?>
</span>
</div>
</li>
<li>
<label>Value:</label>
<div class="oneInfo">
{{Form::text('value', $value=null, array())}}
<span class="errorMessage">
<?php
echo $errors->first('value');
?>
</span>
</div>
</li>
<li>
<input type="submit" value="Save Changes"/>
<input type="button" value="Cancle" class="cancelButton"/>
</li>
</ul>
{{ Form::close() }}
this is the controller update
$input = Input::all();
$validation = Validator::make($input, WaitingTimes::$rules);
if ($validation->passes()){}else{
return Redirect::route('waitingtimes.edit')->withInput()->withErrors($validation)->with(array(
'verticalMenu'=>'none',
'verticalMenuTab' => 'none',
'data' => $input
));
}
Please notice that this html blade code is used for editing the data and it is working correct when I call the edit function, and I am using it also to redirect when the user try to edit information but the validation falls
You'll need to specify the method in your form creation, add this to your Form::model array:
'method' => 'PUT'
You will need to tell your form that you will be using method PUT:
{{ Form::model($data, array(
'route' => array('waitingtimes.update', $data->id),
'class' => 'mainInformationContrainer',
'method' => 'put',
)) }}
Note that you will still see method = "POST" in your form but Laravel will add a hidden field called _method to your form. See http://laravel.com/docs/html#opening-a-form
I found the solution,
which is
return Redirect::back()->withInput()->withErrors($validation)->with(array(
'verticalMenu'=>'none',
'verticalMenuTab' => 'none',
'data' => $input
));
Thanks to this question
Laravel form model binding

how to pass a php variabe to yiibooster modal window via link

I want to pass a php variable to modal window , what i am doing is opening a modal window using this link , but i want to pass a variable to this link and get same variable in modal window , i try to to do this to append a text in some div but it return html that i am unable to get in query
echo CHtml::link(
'Set Recipe', '', array(
'class' => 'testclass',
'id' => $finalDate,
'data-toggle' => 'modal',
'data-target' => '#myModal',
'fahadVar' => $finalDate
));
and when i click this button i got modal window how to get variable set in button
Here is simple modal code of yiibooster
<div class="modal-body">
<p>One fine body...</p>
</div>
<div class="modal-footer">
<?php $this->widget(
'bootstrap.widgets.TbButton',
array(
'type' => 'primary',
'label' => 'Save changes',
'url' => '#',
'htmlOptions' => array('data-dismiss' => 'modal'),
)
); ?>
<?php $this->widget(
'bootstrap.widgets.TbButton',
array(
'label' => 'Close',
'url' => '#',
'htmlOptions' => array('data-dismiss' => 'modal'),
)
); ?>
</div>
<?php $this->endWidget(); ?>
thanks in advance
You should create a Widget.
Note: I copied below from another post. It works like charm.
First Create a new widget. Let say the name is CategoryWidget. Put this widget under components directory protected/components.
class CategoryWidget extends CWidget {
public function run() {
$models = Category::model()->findAll();
$this->render('category', array(
'models'=>$models
));
}
}
Then create a view for this widget. The file name is category.php. Put it under protected/components/views
category.php
<?php if($models != null): ?>
<ul>
<?php foreach($models as $model): ?>
<li><?php echo $model->name; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
Then call this widget from your main layout.
main.php
// your code ...
<?php $this->widget('CategoryWidget') ?>

Categories