Add onClick event on form input with multiple checkbox


To add a javascript onclick event on a form input with multiple checkbox, use this alternative method:

$options = array(); 
foreach($roles as $value => $label) { 
  $options[] = array( 
    'name' => $label, 
    'value' => $value, 
    'onClick' => 'showDiv(this)' 
  ); 
} 
echo $form->input('Role', array('options' => $options, ...)); 

But why use inline javascript? You could do this with one line of mootools:

$('input[id^=ModelRole][type=checkbox]').addEvent('click', showDiv);
(replace 'Model' with the name of your model)

And I’m sure it’s just as easy (or easier) in jQuery or other frameworks.

Advertisements

CakePHP 2.0 Naming Conventions


The PHP’s popular Framework has some Naming conventions that need to be followed at the time of development.

Controller Conventions

Controller class names are plural, CamelCased, and end in Controller and the filename would be ‘ApplesController.php’.

e.g. PeopleController and LatestArticlesController

File and Class Name Conventions

In general, filenames match the class names, which are CamelCased. So if you have a class MyNiftyClass, then in CakePHP, the file should be named MyNiftyClass.php. Below are examples of how to name the file for each of the different types of classes you would typically use in a CakePHP application:

  • The Controller class KissesAndHugsController would be found in a file namedKissesAndHugsController.php
  • The Component class MyHandyComponent would be found in a file named MyHandyComponent.php
  • The Model class OptionValue would be found in a file named OptionValue.php
  • The Behavior class EspeciallyFunkableBehavior would be found in a file namedEspeciallyFunkableBehavior.php
  • The View class SuperSimpleView would be found in a file named SuperSimpleView.php
  • The Helper class BestEverHelper would be found in a file named BestEverHelper.php

Each file would be located in the appropriate folder in your app folder. Continue reading

[RFC] PHP 5.4 Strict Error Reporting


Testing with PHP 5.4 at E_ALL produces strict warning messages such as:

Strict (2048): Declaration of Product::beforeSave() should be compatible with Model::beforeSave($options = Array) [APP/Model/Product.php, line 4]

It’s an easy fix, for example in APP/Model/Product.php change:

public function beforeSave() {

to:

public function beforeSave($options = array()) {

Confirm before Deleting post in CakePHP


In the controller, create a delete function:

<?php
class EmployeeController extends AppController
{
public function delete($id)
{
$this->Employee->delete($id);
$this->Session->setFlash(‘The Employee is deleted.’);
$this->redirect(array(‘action’=>’index’));
}
}

In the view page, write the following code:

<a href=”#” onclick=”return confirm(‘Are you sure you want to delete this Employee?’);”>Delete</a>

Pagination in CakePHP


In the controller file like I have Employeecontroller.php, write the following code:

<?php
class EmployeeController extends AppController
{
public $paginate = array(
‘limit’ => 2,
‘order’ => array(
‘Employee.id’ => ‘asc’
)
);

public function index()
{
$data = $this->paginate(‘Employee’);
$this->set(’employeeList’, $data);
}
}
?>

Now in the view file like I have index.ctp, write the following code:

<div align=”center”>
<!– Shows the page numbers –>
<?php echo $this->Paginator->numbers(); ?>
<!– Shows the next and previous links –>
<?php echo $this->Paginator->prev(‘« Previous ‘, null, null, array(‘class’ => ‘disabled’)); ?>
<?php echo $this->Paginator->next(‘ Next »’, null, null, array(‘class’ => ‘disabled’)); ?>
<!– prints X of Y, where X is current page and Y is number of pages –>
<?php echo $this->Paginator->counter(); ?>
</div>