Skip to content

Instantly share code, notes, and snippets.

@swthate
Created August 18, 2014 21:09
Show Gist options
  • Save swthate/df324b3d9d65fb20a0c1 to your computer and use it in GitHub Desktop.
Save swthate/df324b3d9d65fb20a0c1 to your computer and use it in GitHub Desktop.
Cake AutoComplete
<!-- File: app/View/Jobs/add.ctp -->
<?php
// Load jQuery from Google
$this->Html->script('https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js', array('inline' => false));
$this->Html->script('https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js', array('inline' => false));
// Load file for this view to work on 'autocomplete' field
$this->Html->script('View/Jobs/add', array('inline' => false));
$this->Html->addCrumb('Jobs', '/jobs');
$this->Html->addCrumb('Add Job');
?>
<div class="large-12 column">
<h1>Add Job</h1>
</div>
<div class="large-6 column">
<?php echo $this->Form->create('Job'); ?>
<fieldset>
<?php
echo $this->Form->input('client_id', array(
'class' => 'ui-autocomplete',
'id' => 'autocomplete'));
echo $this->Form->input('title', array('default' => 'Enter a short, descriptive title for the job.'));
echo $this->Form->input('datein');
echo $this->Form->textarea('description', array('rows' => '5', 'default' => 'Enter your description here.'));
echo $this->Form->button(
'Save Job <i class="fa fa-save btn-icon-small"></i>', array(
'class' => 'button',
'escape' => false)
);
?>
</fieldset>
</div>
// File: app/webroot/js/View/Jobs/add.js
(function($) {
$('#autocomplete').autocomplete({
source: "/jobs/add.json"
});
})(jquery);
<!-- File: app/Model/Job.php -->
<?php
<?php
App::uses('AppModel', 'Model');
/**
* Job Model
*
* @property Client $Client
* @property Contact $Contact
* @property Comment $Comment
*/
class Job extends AppModel {
/**
* jQuery Auto Complete
*
*/
public function getClientNames ($term = null) {
if(!empty($term)) {
$clients = $this->find('list', array(
'conditions' => array(
'name LIKE' => trim($term) . '%'
)
));
return $clients;
}
return false;
}
/**
* Display field
*
* @var string
*/
public $displayField = 'title';
/**
* Validation rules
*
* @var array
*/
public $validate = array(
'datein' => array(
'date' => array(
'rule' => array('date'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'description' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'title' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
);
//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* belongsTo associations
*
* @var array
*/
public $belongsTo = array(
'Client' => array(
'className' => 'Client',
'foreignKey' => 'client_id',
'conditions' => '',
'fields' => '',
'order' => '',
'counterCache' => true
)
);
/**
* hasMany associations
*
* @var array
*/
public $hasMany = array(
'Comment' => array(
'className' => 'Comment',
'foreignKey' => 'job_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
'Task' => array(
'className' => 'Task',
'foreignKey' => 'job_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
}
<!-- File: app/Controller/JobsController.php -->
<?php
/**
* Components
*
* @var array
*/
public $components = array('Paginator', 'Session', 'RequestHandler');
public $paginate = array(
'order' => array(
'Job.id' => 'desc'
)
);
public $helpers = array('Js' => array('Jquery'));
/**
* add method
*
* @return void
*/
public function add() {
if ($this->request->is('ajax')) {
$term = $this->request->query('term');
$clientNames = $this->Client->getClientNames($term);
$this->set(compact('clientNames'));
$this->set('_serialize', 'clientNames');
}
if ($this->request->is('post')) {
$this->Job->create();
if ($this->Job->save($this->request->data)) {
$this->Session->setFlash(__('The job has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The job could not be saved. Please, try again.'));
}
}
$clients = $this->Job->Client->find('list');
$this->set(compact('clients'));
}
<?php
/**
* Here, we are connecting '/' (base path) to controller called 'Pages',
* its action called 'display', and we pass a param to select the view file
* to use (in this case, /app/View/Pages/home.ctp)...
*/
Router::connect('/', array('controller' => 'dashboards', 'action' => 'index'));
/**
* ...and connect the rest of 'Pages' controller's URLs.
*/
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
/**
* Load all plugin routes. See the CakePlugin documentation on
* how to customize the loading of plugin routes.
*/
CakePlugin::routes();
/**
* Load the CakePHP default routes. Only remove this if you do not want to use
* the built-in default routes.
*/
require CAKE . 'Config' . DS . 'routes.php';
Router::parseExtensions('json');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment