spadefoot / kohana-orm-leap

An ORM module for the Kohana PHP framework that is designed to work with all major databases.
http://spadefoot.github.io/kohana-orm-leap/
100 stars 25 forks source link

remove current Field validation and use Model validation instead #61

Open taai opened 12 years ago

taai commented 12 years ago

With current validation I can't set all user posted data to the model without causing exception:

<?php
$car->set_values($_POST);

It possibly throws me something like this: _[Kohana_BadDataException] Message: Unable to set the specified property. Reason: Value x failed to pass validation constraints.

I would like to validate the model only when I save/update it. And I would like to use the model as an object where get the user posted and other data from.

<?php
$view = View::factory('car/form');

$car = new Model_Leap_Car();

if ($_POST) {
    $car->set_values($_POST);

    try {
        $car->save();
    }
    catch (Exception $e) {
        $view->errors = $e;
    }
}

$view->car = $car_model;

Possible solutions

Get rid of current validation. Make a validation for model. Maybe the validation rules could get set for each Field, but anyway it should not get validated right when value gets set. Also the validation should be as an separate function, because not always you need it. Oh, and there should be a "error" variable, where to store the error message, so that I can get that error in my template:

<?php
echo Form::input('name', $car->name);
echo '<span class="error">' . $car->name->error . '</span>';