Sunday 23 December 2012

Simple Cakephp form creation

Form creation in cakephp

first app->controller->Admincontroller



public function category_add ( ) {
        $this->layout = 'admin';  //layout call
        $this->loadModel('Category'); //model call
        if(!empty ($this->request->data)) {
            $category_data = $this->request->data['Category'];
            $image = array();
            $formimage = array('category_image');
            $image = array($category_data['category_image']);
            $file = $this->uploadFiles('img/category', $image,$formimage);
            /* check file upload */
            if(!isset($file['urls'])) { 
                $this->Session->setFlash(implode($file['errors']));
            } elseif(isset($file['urls']) && count($file['urls']) < 1 ) { 
                $this->Session->setFlash(implode($file['errors']));
            } else {
                for($i=0; $i < count($file['urls']); $i++) {
                    $category_data[$formimage[$i]] = $file['urls'][$formimage[$i]];
                }
                if($this->Category->save($category_data)) {
                    $last_id= $this->Category->getLastInsertID();
                    if($category_data['url']) {
                        $this->loadModel('Indexer');
                        $indexer =array();
                        $indexer['Indexer']['item_id']="$last_id";
                        $indexer['Indexer']['type']="Category";
                        $indexer['Indexer']['url']=$category_data['url'];
                    if($this->Indexer->save($indexer)){
                    }
            }
            $this->Session->setFlash('The Category has been addedd successfully');
            $this->redirect(array('action'=>'category_list'));
            } else {
                print_r($this->Category->validationErrors);
                $this->Session->setFlash('The Category was not saved. Please re enter the details');
            }
            }
            /* end Db saved */
        } else {
            $this->render();
        }
    }


second app->Model creation ->Category.php

class Category extends AppModel {
    var $name = "Category";
    public $useTable = 'peeka_category';
    public $primaryKey = 'category_id';
    public $validate = array();

//relation ships(if not need it is not necessary)
    public $hasMany = array(
        'CategoryDeal'=>array(
            'class'=>'CategoryDeal',
            'foreignKey'=>'category_id'
        ),
        'Categorybanner'=>array(
            'class'=>'Categorybanner',
            'foreignKey'=>'category_id'
        ),'Productcatagory'=>array(
            'class'=>'Productcatagory',
            'foreignKey'=>'category_id'
        )
    );
    public $hasOne = array(
        'Indexer'=>array(
            'class'=>'Indexer',
            'foreignKey'=>'item_id',
            'coditions'=>array('Indexer.type'=>'Category')
        )
    );
}



View files app->View->Admin


<?php

    echo $this->Html->script('/js/jQuery.Validate.min.js');
   
    echo $this->Form->create('Category', array('type' => 'file','name'=>'addcategoryform'));

    echo $this->Form->input('category_name', array('label' => 'Category Name','class' => 'required ','id' =>'copy_from','onkeyup'=>'copy_data(this)'));
?>  
<div style="display:none">
<?php
      
   
     echo $this->Form->input('url', array('readonly' => 'true','label' => 'URL','class' => 'required','id' =>'copy_to'));

?>
</div>
<?php
    echo $this->Form->input ( 'category_title' ,array ('label' => 'Category Title','class' => 'required' ) ) ;
   
    echo $this->Form->input('category_image', array( 'type' => 'file','id'=>'img','class'=>'required'));
   
    echo $this->Form->input ( 'category_desc' ,array ('label' => 'Category Descrption','type'=>'textarea','class' => 'required' ) ) ;
   
    echo $this->Form->input('category_url', array('label' => 'Category Link URL','class' => 'required','id' =>'copy_to'));
   
    echo $this->Form->input('meta_keyword', array('label' => 'Meta Keyword'));

    echo $this->Form->input('meta_desc', array('type' =>'textarea','label' => 'Meta Desc'));
   

    echo $this->Form->end('Save Category');


?>