// app/classes/myvalidation.php class Myvalidation { public function _validation_unique($val, $options) { list($table, $field) = explode('.', $options); $result = DB::select("LOWER (\"$field\")") ->where("$field", '=', MBSTRING ? mb_strtolower($val) : strtolower($val)) ->from($table)->execute(); if($result->count() > 0) return false; else return true; } }
$val = Validation::factory('signup_user'); $val->add_callable('myvalidation'); $val->add('username', 'Username')->add_rule('required')->add_rule('min_length', 3)->add_rule('max_length', 20)->add_rule('unique[users.username]'); $val->add('password', 'Password')->add_rule('required')->add_rule('min_length', 3)->add_rule('max_length', 20); $val->add('email', 'Email Address')->add_rule('required')->add_rule('valid_email')->add_rule('unique[users.email]'); $val->add('api_id', 'Api ID')->add_rule('required')->add_rule('min_length', 10)->add_rule('max_length', 20); $val->add('api_key', 'Api Key')->add_rule('required')->add_rule('min_length', 10)->add_rule('max_length', 20);But for some reason I get following error:
Notice: Invalid rule "unique[users.username]" passed to Validation, not used. in APPPATH/classes/controller/users.php [25]: add_ruleNotice: Invalid rule "unique[users.email]" passed to Validation, not used. in APPPATH/classes/controller/users.php [27]: add_rule
peter vercauteren wrote on Tuesday 19th of April 2011:Isn't it already added?
Extending Validation class There are few approaches to extend the validation class: 1. To extend the core class like described in the Extending Core ClassesThere are few aproaches on extending the core clases, you'll need the first approach: And how is the core extended is described here: http://fuelphp.com/docs/general/extending_core.html If you won't be able to make it, let me know
<?php class Myvalidation extends Validation{ public function _validation_unique($val, $options) {
peter vercauteren wrote on Tuesday 19th of April 2011:I don't quite get it... I mean even though I remove the add_callable it doesnt change the error. Also I tried extending it... Does it mean I only need to add extend Validation? Like so:<?php class Myvalidation extends Validation{ public function _validation_unique($val, $options) {
Just to be sure, the myvalidation.php still needs to be set in the classes folder.
And should the add_callable still be set before the rules after extending the Validation class?
$val->add_field('username', 'Username', 'required|min_length[3]|max_length[20]|unique[users.username]');
$val->add('username', 'Username')->add_rule('required')->add_rule('min_length', 3)->add_rule('max_length', 20)->add_rule('unique', 'users.username');
Jaroslav Petrusevic wrote on Wednesday 20th of April 2011:Hello, to refill you from, you need smth like this, ebcause you don't tell the Form class to fill out the form for you,
for example you can do: <?php echo Form::input('name', Input::post('name')); ?> This way it will load the form, but if the submit happens, it will refill the form input 'name' with the post value 'name' does it looks more clear for you now?
$this->template->errors = $val->show_errors();
<?php echo Form::input('username', NULL, array('size' => 30)); ?>would become:
<?php echo Form::input('username', Input::post('username'), array('size' => 30)); ?>
<?php echo Validation::instance()->show_errors(); ?>
It looks like you're new here. If you want to get involved, click one of these buttons!