Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1055375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:33:10+00:00 2026-05-16T17:33:10+00:00

I find examples and tutorials about models and about validation. And I places that

  • 0

I find examples and tutorials about models and about validation. And I places that say the validation (or most of it at least) should be in the model, which I agree with. But I can’t any examples or tutorials that show how that should be done.

Could anyone help me with a simple example on how that could be done? Where would you have the rules in the model? Where would the validation happen? How would the controller know if the validation passed or fail? How would the controller get error messages and things like that?

Hope someone can help, cause feel a bit lost here :p

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T17:33:11+00:00Added an answer on May 16, 2026 at 5:33 pm

    I too had difficulty finding examples for Kohana3, bestattendance’s example is for Kohana2.

    Here’s an example I threw together in my own testing:

    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;
        }
    }
    

    application / messages / errors.php

    <?php
    return array(
        'title' => array(
            'not_empty' => 'Title can\'t be blank.',
        ),
        'post' => array(
            'not_empty' => 'Post can\'t be blank.',
        ),
    );
    

    application / classes / controller / news.php

    <?php defined('SYSPATH') OR die('No Direct Script Access');
    
    Class Controller_News extends Controller
    {
        public function action_index() {
            //setup the model and view
            $news = Model::factory('news');
            $view = View::factory('news')
                ->bind('validator', $validator)
                ->bind('errors', $errors)
                ->bind('recent_posts', $recent_posts);
    
            if (Request::$method == "POST") {
                //added the arr::extract() method here to pull the keys that we want
                //to stop the user from adding their own post data
                $validator = $news->validate_news(arr::extract($_POST,array('title','post')));
                if ($validator->check()) {
                    //validation passed, add to the db
                    $news->add_news($validator);
                    //clearing so it won't populate the form
                    $validator = null;
                } else {
                    //validation failed, get errors
                    $errors = $validator->errors('errors');
                }
            }
            $recent_posts = $news->get_latest_news();
            $this->request->response = $view;
        }
    }
    

    application / views / news.php

    <?php if ($errors): ?>
    <p>Errors:</p>
    <ul>
    <?php foreach ($errors as $error): ?>
        <li><?php echo $error ?></li>
    <?php endforeach ?>
    </ul>
    <?php endif ?>
    
    <?php echo Form::open() ?>
    <dl>
        <dt><?php echo Form::label('title', 'title') ?></dt>
        <dd><?php echo Form::input('title', $validator['title']) ?></dd>
        <dt><?php echo Form::label('post', 'post') ?></dt>
        <dd><?php echo Form::input('post', $validator['post']) ?></dd>
    </dl>
    <?php echo Form::submit(NULL, 'Post') ?>
    <?php echo Form::close() ?>
    <?php if ($recent_posts): ?>
    <ul>
    <?php foreach ($recent_posts as $post): ?>
        <li><?php echo $post['title'] . ' - ' . $post['post'];?></li>
    <?php endforeach ?>
    </ul>
    <?php endif ?>
    

    To get this code working in a default install, you would have to enable the database module and configure it for authentication. Then you can access it from index.php/news using default configuration.

    It is tested in Kohana 3.0.7 and should give you a good starting point of how you might lay out code. Unlike other frameworks, Kohana seems to be very open ended as to where you put your logic so this is just what made sense to me. If you want to use the ORM instead of rolling your own database interaction, it has its own syntax for validation which you can find here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been looking at examples online, and tutorials, and I cannot find anything
I'm struggling to find examples, blogs etc that explain how to use the !?
I find it hard to find clear examples that would explain how to read
Any suggestions on where to find examples, tutorials, and more thorough documentation on how
Ive been googling mutex tutorials / examples but cant seem to find one for
Where can I find examples of code, written in Unified Parallel C? I also
Trying to find examples of when decorators might be really beneficial, and when not
I've been trying to find examples everywhere but it's been in vain. I am
I've been looking into multi-module projects in Maven, and I can only find examples
I find such examples in Boost code. namespace boost { namespace { extern C

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.