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 7368125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:37:32+00:00 2026-05-29T03:37:32+00:00

Trying to get Validation with ORM working for Kohana 3.2. At the moment i

  • 0

Trying to get Validation with ORM working for Kohana 3.2.

At the moment i have my Model:

<?php defined('SYSPATH') or die('No direct access allowed.');

class Model_Brand extends ORM {

    protected $_has_many = array('models' => array());

    protected $_rules = array(
        'name' => array(
            'not_empty' => NULL,
            'min_length' => array(3),
            'max_length' => array(20),
        ),
        'sku' => array(
            'not_empty' => NULL,
            'min_length' => array(3),
            'max_length' => array(6),
        ),

    );

}

And heres my Controller:

<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Brand extends Controller_Layout {

    public function action_view()
    {
        $brands = ORM::factory('brand')->find_all();
        $this->template->title = __('Brands');
        $this->template->content = View::factory('brands/view' );
        $this->template->content->set('brands', $brands);
    }

    public function action_edit()
    {
        if($_POST)
        {
            try
            {
                $brand = ORM::factory('brand', $this->request->param('id'));
                $brand->values($_POST);

                if($brand->check())
                {
                    $brand->update();
                    $brand->save();

                    //go to brand/views
                }

            }
            catch (ORM_Validation_Exception $e)
            {
                //pass errors to brand/edit
            }
        }
        else
        {
            $brand = ORM::factory('brand', $this->request->param('id'));

            $this->template->title = __('Edit Brand');
            $this->template->content = View::factory('brands/edit' );
            $this->template->content->set('brand', $brand);
        }
    }
}

I haven’t even got to the errors part yet. The problem i’m having is its validating on any input and not using the rules from the model. Also if anyone can show me how an update action like this should be designed would be a big help. Thanks.

  • 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-29T03:37:32+00:00Added an answer on May 29, 2026 at 3:37 am

    This is how I do model validation, and I feel it’s most straightforward and elegant.

    First, I set my rules in the rules() method:

    <?php defined('SYSPATH') or die('No direct access allowed.');
    
    class Model_Brand extends ORM {
    
        public function rules()
        {
            return array(
                'name' => array(
                    array('not_empty'),
                    array('min_length', array(':value', 3)),
                    array('max_length', array(':value', 20)),
                )
                'sku' => array(
                    array('not_empty'),
                    array('min_length', array(':value', 3)),
                    array('max_length', array(':value', 6)),
                )
            );
        );
    }
    

    And this is how I manage my edit action:

    public function action_edit()
    {
        $brand = ORM::factory('brand', $this->request->param('id'));
    
        if (!$brand->loaded())
        {
            throw new Kohana_Exception('Brand not found.');
        }
    
        $this->template->title = __('Edit Brand');
        $this->template->content = View::factory('brands/edit')
            ->set('brand', $brand)
            ->bind('errors', $errors);
    
        if ($this->request->method() === Request::POST)
        {
            try
            {
                $brand->values($this->request->post());
                $brand->save();
    
                // Success! You probably want to set a session message here.
    
                $this->request->redirect($this->request->uri());
            }
            catch(ORM_Validation_Exception $e)
            {
                // Fail!
    
                $errors = $e->errors('brand');
            }
        }
    }
    

    And in my view:

    <?php if ($errors) {?>
        <!-- display errors here -->
    <?php } ?>
    
    <?php echo Form::open()?>
        <fieldset>
    
            <div class="field">
                <?php echo 
                    Form::label('name', __('Name')),
                    Form::input('name',  $brand->name)
                ?>
            </div>
    
            <?php echo Form::submit('save', 'Save')); ?>
        </fieldset>
    <?php echo Form::close()?>
    

    As you can see in the view, I’m not doing any conditional checking to see what to display in the form field, as that is managed by the data in the model, which is managed by the controller.

    Hope this helps, ask away if you need further clarification.

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

Sidebar

Related Questions

I'm trying to get client validation working on my asp.net mvc 2 web application
I have been working for 6 hours trying to get my standard core functions
I am trying to get client validation working in MVC3 using data annotations. I
I am trying to get the jQuery validation working on a webpage I am
Using MVC3 and Entity Framework. Am trying to get validation flowing from data model
Newbie question... I'm trying to get form validation working with Spring Web using this
I'm trying to get WPF validation to work within the MVVM pattern. In my
alt text http://img42.imageshack.us/img42/4161/blinkthru.png I'm trying to get the validation to not show through my
I am trying to get my UniqueEmail validator working but it seems that my
Trying to get this example working from http://www.munna.shatkotha.com/blog/post/2008/10/26/Light-box-effect-with-WPF.aspx However, I can't seem to get

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.