0

In a controller, I am validating the input of the fields of a registration form. I am doing simple validation such as required fields, email validation etc.

When I get a successful submit, I then go on to attempt to create the user (I am using the Sentry package). I want to be able to catch the errors from this process and then append them to the validation errors and attach them to specific fields. For example: if the email already exists, I want to insert an error to the 'email field' and give it a custom error message.

I was hoping there was some easy way to do this, such as (pseudo-code):

$validation->error()->insert_error($field_name, $message)

Any easy way of doing this?

4

1 回答 1

0

The native Validation class does not allow that.

But I can see 2 solutions for your problem.

First solution (did not test, this is just to get the idea):

<?php
class CustomFieldError {
    function rule_which_always_fail($val) {
        return false;
    }
}
function insert_error($validation, $field_name, $error) {
    $validation->field($field_name)->->add_rule(array('CustomFieldError', 'rule_which_always_fail'))->set_message($error);
}

// You need to do that before $validation->run();
if ($duplicate_email) {
    insert_error($validation, 'email', 'Duplicate email');
}

Second solution: Replace and extends the \Fuel\Core\Validation class with your own. This way you can add the insert_error() method directly inside the Validation class and add errors with $this->errors['field_name'] = 'Error message';

[edit] If you're wanting to use the 2nd solution, also be sure to submit them a feature request in their Github tracker :)

于 2012-04-24T08:16:31.610 回答