Showing posts with label kohana 3 Example. Show all posts
Showing posts with label kohana 3 Example. Show all posts

Kohana 3 :: blog article unique slug

Post_Model
public function save()
{
 $this->slug = $this->_unique_slug(url::title(empty($this->slug) ? $this->title : $this->slug));
 return parent::save();
}

private function _unique_slug($str)
{
 static $i;
 
 $original = $str;
 
 while ($post = ORM::factory('post', $str) AND $post->loaded AND $post->id !== $this->id)
 {
  $str = $original.$i;
  $i++;
 }
 
 return $str;
}

http://forum.kohanaframework.org/discussion/comment/17756/#Comment_17756

Kohana 3 :: Example


How to use Kohana 3 validation (with forms) 3


Displaying validation errors in context on the form

Here is how we want to show the forms:
To accomplish this, I have created a new Appform helper which uses the Form class, but wraps it’s input with application-specific markup for errors.

Kohana 3: Example of model with validation

practice :: add -- validate -- get list post -- error message -- controller -- model -- views


application / classes / model / news.php

<?php defined('SYSPATH') OR die('No Direct Script Access');
Class Model_News extends Model
{
    /*
       CREATE TABLE `news_example` (
       `id` INT PRIMARY KEY AUTO_INCREMENT,
       `title` VARCHAR(30) NOT NULL,
       `post` TEXT NOT NULL);
     */

    public function get_latest_news() {
        $sql = 'SELECT * FROM `news_example` ORDER BY `id` DESC LIMIT  0, 10';
        return $this->_db->query(Database::SELECT, $sql, FALSE)
                         ->as_array();
    }

    public function validate_news($arr) {
        return Validate::factory($arr)
            ->filter(TRUE, 'trim')
            ->rule('title', 'not_empty')
            ->rule('post', 'not_empty');
    }
    public function add_news($d) {
        // Create a new user record in the database
        $insert_id = DB::insert('news_example', array('title','post'))
            ->values(array($d['title'],$d['post']))
            ->execute();

        return $insert_id;
    }
}