how can i make two form input are related cakephp - cakephp

I would like to make this in my add.ctp.
when user choose the department, on the file form field, only shows the file with the same department that they choose
in my add.ctp
<div class="form-group">
<?php echo $this->Form->input('department', array('class' => 'form-control', 'placeholder' => 'Department', 'options' => array(
'Administrator' => 'Administrator',
'Multimedia' => 'Multimedia',
'Treasurer' => 'Treasurer',
'Marketing' => 'Marketing',
),
'empty' => '(Choose Department)',));?>
</div>
<div class="form-group">
<?php echo $this->Form->input('fail_id', array('class' => 'form-control', 'label' => 'File','placeholder' => 'File Id', 'empty' => '(Choose File)'));?>
</div>
in my controller
public function add() {
if ($this->request->is('post')) {
$this->Borrow->create();
$this->request->data['Borrow']['user_id']= $this->Auth->user('id');
if ($this->Borrow->save($this->request->data)) {
$this->Session->setFlash(__('The borrow has been saved.'), 'default', array('class' => 'alert alert-success'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The borrow could not be saved. Please, try again.'), 'default', array('class' => 'alert alert-danger'));
}
}
$users = $this->Borrow->User->find('list');
$fails = $this->Borrow->Fail->find('list');
$fails = $this->Borrow->Fail->find('list');
$this->set(compact('users', 'fails', 'fails'));
}
thanks for the kindness help.

You have to use ajax post. after user select a department you should send an ajax post with the value of department and get the related files and update second combobox.

Related

Milesj uploader plugin creates new record instead of editing existing record

I'm using the cakephp uploader plugin by Miles Johnson. I like the plugin and I've got it working to be able to upload photos for a photo model.
The problem I'm running into is that when I try to edit an existing record, instead of replacing the photo associated with the record I'm editing a new record is created. The new record has all the same information with the exception of the new photo that's been uploaded.
Here's how the photo model looks:
'Uploader.Attachment' => array(
'imgPath' => array(
// 'name' => '', // Name of the function to use to format filenames
'baseDir' => '', // See UploaderComponent::$baseDir
'uploadDir' => 'files/uploads/', // See UploaderComponent::$uploadDir
'dbColumn' => 'imgPath', // The database column name to save the path to
'maxNameLength' => 60, // Max file name length
'overwrite' => false, // Overwrite file with same name if it exists
'stopSave' => true, // Stop the model save() if upload fails
'allowEmpty' => true, // Allow an empty file upload to continue
'transforms' => array( // What transformations to do on images: scale, resize, etc
array(
'method' => 'resize',
'width' => 50,
'height' => 50,
'append' => '_thumb',
'dbColumn' => 'imgPathThumb',
)
)
)
)
This is the photo admin edit view form:
<?php echo $this->Html->script('ckeditor/ckeditor');?>
<?php echo $this->Form->create('Photo', array('type' => 'file'));?>
<fieldset>
<legend><?php echo __('Admin Edit Photo'); ?></legend>
<?php
echo $this->Form->input('title');
echo $this->Form->input('caption', array('class'=>'ckeditor'));
echo $this->Form->input('imgPath', array('type' => 'file'));
echo $this->Form->input('alt_tag');
echo $this->Form->input('type', array( 'label' => 'Choose a type of photo', 'options' => array(
'gallery'=>'gallery',
'news'=>'news',
'post'=>'post',
)));
echo $this->Form->input('active', array( 'label' => 'Make active', 'options' => array('yes'=>'yes','no'=>'no' )));
echo $this->Form->input('gallery_id');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
This is the controller admin edit code:
public function admin_edit($id = null) {
$this->layout = 'admin';
if (!$this->Photo->exists($id)) {
throw new NotFoundException(__('Invalid photo'));
}
if ($this->request->is(array('post', 'put'))) {
if ($this->Photo->save($this->request->data)) {
$this->Session->setFlash(__('The photo has been saved.'), 'success');
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The photo could not be saved. Please, try again.'), 'error');
}
} else {
$options = array('conditions' => array('Photo.' . $this->Photo->primaryKey => $id));
$this->request->data = $this->Photo->find('first', $options);
}
$galleries = $this->Photo->Gallery->find('list');
$this->set(compact('galleries'));
}
Have I overlooked something obvious?
Cheers, Paul
When You editing record, You must set ID of this record.
Add to form $this->Form->input('id') or set ID in controller before save() action

Issue deleting items with filter search

I am developing a plugin for CakePHP 2.
My problem is below:
I have a view with a filter search, where users can search by category:
<div>
<?php echo $this->Form->create('Permission'); ?>
<fieldset>
<?php echo $this->Form->input("permissionCategory", array('label' =>
__d('permission_manager','Categoría'), 'options' => $categorias, 'empty' =>
__d('permission_manager','-- Categorías --'), 'default' => '','class' => 'select-cat', 'onchange' => 'this.form.submit()')); ?>
</fieldset>
<?php //echo $this->Form->end(__d('permission_manager','Aceptar')); ?>
</div>
When I do click over delete button, it shows the confirm message, but when I push yes, the item is not deleted.
I realized it has relation with the action. Maybe with the request type.
The action code:
public function index() {
$this->paginate = array('order' => 'Permission.category asc, Permission.code asc');
$this->Permission->recursive = 2;
if($this->request->is('post')) {
if(empty($this->request->data['Permission']['permissionCategory'])) {
$conditions = null;
} else {
$cat = $this->Permission->findById($this->request->data['Permission']
['permissionCategory']);
$conditions = array('Permission.category ' => $cat['Permission']['category']);
}
$this->Paginator->settings = array(
'conditions' => $conditions );
}
$this->set('categorias', $this->Permission->find('list', array('fields' => 'category',
'group' => 'category')));
$this->set('permissions', $this->Paginator->paginate());
}
Edit:
My delete button:
(I have one like this for row in DB)
<?php echo $this->Form->postLink($this->Html->image('PermissionManager.cross.png',
array('title' =>
__d('permission_manager','Borrar'))),'delete/'.h($permission['Permission']['id']),
array( 'escape' => false), __d('permission_manager','Are you sure... ?'));
?>
My delete action:
public function delete($id = null) {
$this->Permission->id = $id;
if (!$this->Permission->exists()) {
//throw new NotFoundException(__d('permission_manager','Permiso inexistente'));
$this->Session->setFlash(__d('permission_manager','Permiso inexistente'), 'default', array('class'=>'error'));
return $this->redirect(array('action' => 'index'));
}
$this->request->onlyAllow('post', 'delete');
if ($this->Permission->delete()) {
$this->Session->setFlash(__d('permission_manager','El permiso ha sido borrado correctamente.'), 'default', array('class'=>'success'));
} else {
$this->Session->setFlash(__d('permission_manager','El permiso no ha podido borrarse. Por favor, inténtelo de nuevo.'), 'default', array('class'=>'error'));
}
return $this->redirect(array('action' => 'index'));
}

cakephp check password with profile update

I am newbie to cake php and I have a problem.
When a user updates his profile iam asking to enter his password.Now I dont know how to check it with the stored password.
<?php echo $form->create('UserProfile', array('url' => array('action' => 'edit_profile', 'controller' => 'users'))); ?>
<?php
echo $form->input('nickname', array('label' => __('Display Name', true), 'class' => 'INPUT required'));
?>
<b>To save these settings, please enter your password</b><br /><br />
<?php
echo $form->input('repeat_password', array('label' => __('Password', true), 'class' => 'INPUT required','type'=>'password'));
echo $form->input('private', array('label' => __('Set Profile Private', true), 'class' => 'INPUT required'));
?>
<!-- <div id="CityList">
<?php
// echo $form->input('city', array('id' => 'citySelect', 'name' => 'data[UserProfile][city]', 'empty' => __('Please select city', true), 'label' => __('City', true), 'class' => 'INPUT required'));
?>
</div> -->
<?php echo $form->submit(__('Submit', true), array('class' => 'save_btn')) ?>`
>
In My model i have applied the following validation on repeat password.
'repeat_password' => array(
array('rule' => 'check_repeatPassword'),
array('rule' => 'notempty', 'message' => __('Required', true), 'require' => true),
array('rule' => 'alphaNumeric', 'allowEmpty' => true, 'message' => __('Password must only contain letters and numbers.', true)),
array('rule' => array('minLength', 6), 'allowEmpty' => true,
'message' => __('Password must be at least 6 characters long.', true)),
array('rule' => array('maxLength', 16), 'allowEmpty' => true,
'message' => __('Password must be at most 16 characters long.', true))
),
function check_repeatPassword($data) {
$repeatPassword = $data['repeat_passowrd'];
$passwordExists = $this->find('count', array('conditions' => array('User.repeat_password' => $repeatPassword)));
if ($passwordExists == 0) {
return false;
}
return true;
}
In My Controller I have made the following edit profile method.
function edit_profile() {
$this->loadModel('UserProfile');
$user = $this->_authenticate_user();
$id = $user['account_num'];
if (!empty($this->data)) {
$this->data['UserProfile']['account_name'] = $id;
unset($this->data['UserProfile']['country']);
unset($this->data['UserProfile']['city']);
if ($this->UserProfile->save($this->data)) {
$userInfo = $this->User->getUserInfo($id);
$this->User->reassign_session($userInfo);
$this->flashMessage(__('Your Profile has been Updated successfully', true), 'Sucmessage');
//$this->redirect(array('action' => 'profile'));
} else {
$this->flashMessage(__('Your Profile has not been updated. Please, try again.', true));
}
}
if (empty($this->data)) {
$this->data = $this->UserProfile->read(null, $id);
}
// $this->loadModel('Country');
// $countries = $this->Country->find('list');
// $this->set('countries', $countries);
$this->loadModel('Nationality');
$nationalities = $this->Nationality->find('list', array('fields' => array('name', 'name')));
$this->set('nationalities', $nationalities);
$this->pageTitle = __('Edit My Profile', true);
}
Kindly help me how to do this.Thankssssss in advance
You are not storing passwords in plain text
I cannot avoid seeing this and being amazed:
$passwordExists = $this->find('count', array(
'conditions' => array(
'User.repeat_password' => $repeatPassword
)
));
Please, please confirm that you aren't storing user passwords in plain text, and if you are stop doing so immediately and execute the following sql:
ALTER TABLE users DROP repeat_password;
Compare a hash with a hash
In the db, you should have the user's existing password stored as a one-way-hash. Assuming that's the case, to verify that the user entered their existing account password - just hash what they provided in the same way the Auth component does, and compare to the db:
function check_repeatPassword($data) {
$userId = $this->data['UserProfile']['account_name']; // from the question
$userInput = current($data);
$hashedUserInput = Security::hash($userInput, null, true);
return $this->find('count', array(
'conditions' => array(
'password' => $hashedUserInput,
'id' => $userId
)
));
}
This will confirm that the password the user entered, is the same as the one already in the db for that user.

cakephp database not saving the correct information

my function puts the correct value into the form for my view but doesn't save the correct value in the database. The database is saving the accounts_users.id NOT the accounts_users.account_id to my the code looks like it should be entering the persons accounts_users.account_id. I see the correct information in the dropdown box in the form
add function
function add(){
$accounts=$this->User->AccountsUser->find('list', array(
'fields'=>array('id','account_id'),'conditions' => array(
'user_id' => $this->Auth->user('id'))));
if($this->request->is('post')){
$this->Template->create();
if ($this->Template->save($this->request->data)) {
$this->Session->setFlash('The template has been saved');
$this->redirect( array('controller' => 'Fields','action' => 'add'));
} else {
$this->Session->setFlash('The template could not be saved. Please, try again.');
}
}
here is the add view
<?php
echo $this->Form->create('Template', array('action'=>'add'));
echo $this->Form->input('name',array('label'=>'Template Name: '));
echo $this->Form->input('account_id',array('label'=>'Business: ', 'type' => 'select', 'options' => $accounts));
echo $this->Form->input('description',array('label'=>'Short Description Of Template: '));
echo $this->Form->end('Click Here To Submit Template');
?>
Try the following code:
function add(){
$accounts=$this->User->AccountsUser->find('list', array(
'fields'=>array('account_id','account_id'),'conditions' => array(
'user_id' => $this->Auth->user('id'))));
if($this->request->is('post')){
$this->Template->create();
if ($this->Template->save($this->request->data)) {
$this->Session->setFlash('The template has been saved');
$this->redirect( array('controller' => 'Fields','action' => 'add'));
} else {
$this->Session->setFlash('The template could not be saved. Please, try again.');
}
}

In my cakephp project the save function is not working?

My controller action is:
function add() {
if (!empty($this->data)) {
$this->Customer->create();
if ($this->Customer->save($this->data)) {
$this->Session->setFlash('A new Customer has been added');
$this->redirect(array('action'=>'index'));
}
else {
$this->Session->setFlash('The customer cannot be added this time. Try again later.');
$this->redirect(array('action'=>'index'));
}
}
}
My model is:
class Customer extends AppModel {
var $name = 'Customer';
var $validate = array(
'name' => array(
'length'=> array(
'rule' => array('between', 4,50),
'message' => 'Name must be minimum 4 and maximum 50 characters long.'
),
'checkUnique'=> array(
'rule' => 'isUnique',
'message' => 'This Name is already registered'
)
));
and this is my view:
<div class="customers form">
<?php echo $form->create('Customer',array('action'=>'add'));?>
<fieldset>
<legend><?php __('Add Customer');?></legend>
<?php
echo $form->input('Customer.name');
echo $form->input('Customer.balance',array('type'=>'hidden','default'=>0));
?>
</fieldset>
<?php echo $form->end('Submit');?>
</div>
every time I submit the form it splashes:
The customer cannot be added this time. Try again later.
For the hidden input field use 'value' instead of 'default' in your view:
$form->input('Customer.balance', array('type' => 'hidden', 'value' => 0));

Resources