0
votes

Im back to professional programming after 5 year late, so i have learn from beginning ;) So i chose Kohana framework for my first framework i try to bulid first application now and i have a small problem, lets begin.

I use Kohana ORM and Auth module, as you know default Auth module user table have default fields (username, password, lastlogin) i try to extend User by:

  • Creating new table (user_additionals)
  • Creating new model (User_Additional)

Model look like this: http://pastie.org/private/412jflfoifyqs46uaxmga - Nothing special. Everything will be okay, i like easy reference like this: $user->additional->firstname etc.

At the moment i have bulid admin panel (admin can edit every user) and... every field. I have 10 fields like firstname, lastname, birthdate presented as form (filled form - placeholder loaded by template assign) and here is my small problem: I want to give admin possibility to edit one from much fields, if admin need to edit user signature or something else - he edit one field from a lot fields available and click "Submit" it's easy - one form have been updated.

But, if i try use something like this: $edit = ORM::Factory('User_Additional')->values($_POST); I get validation Exception (which be catched but, here are validation error - model required all fields to be !empty... (By validation rules)

I use temporary solution, but im a perfectionist and i want to create good code from begining, so here you can find my code: http://pastie.org/private/axtwxbt66gtvcwiv97hvlq My solution start at line 29 (link above).

So my question is: *How to make exceptions from Validation in cases like this?? * *How make exceptions from validation for example for action /admin/edituser/ is it possible? * *How do i can do my model code better? Thanks for any suggestions which can help me *

Thanks!

1

1 Answers

0
votes

Validation will only run on "non-empty" fields, unless you specify the "not_empty" rule. So basically, you could do the following: (when you need to enforce the "non emptiness" of a field)

class Model_User_Additional extends ORM
{
  protected $foreign_key_suffix = 'user_id';
  protected $_primary_key = 'user_id';

  public function enforce_rules(array $fields)
  {
    $validation = Validation::factory($this->_object);
    foreach ($fields as $field)
    {
      $validation->rule($field, 'not_empty');
    }
    return $validation;
  }

  public function rules()
  {
    /* Removed all not_empty */
    return array(
      /*  'user_id' => array(
            array('not_empty'),
        ), */
        'firstname' => array(
            array('text'),
        ),
        'lastname' => array(
            array('text'),
        ),
        'birthdate' => array(
            array('date'),
        ),
        'postprice' => array(
            array('decimal'),
        ),
        'articleprice' => array(
            array('decimal'),
        ),
        'phonenumber' => array(
            array('phone'),
        ),
  /*      'active' => array(
            array('not_empty'),
        ), */
  /*      'lastupdate' => array(
            array('not_empty'),
        ), */
        'info' => array(
            array('text'),
        ),

    );
  }

  public function edit_user($values, $expected, $extra_validation = NULL)
  {
    return $this->values($values, $expected)->update($extra_validation);
  }
}

the usage would be:

$user = ORM::factory('User_Additional');
$user->values(array('field' => 'value'))->create($user->enforce_rules(array('user_id')));
/* and the same for updating */
$user->set('info', $_POST['info'])->update($user->enforce_rules(array('info')));