Question

I have fully working validation script my problem is that i can't get custom error messages

Here is my function for registration: http://pastebin.com/ZF3UVxUr

And here is my message array: http://pastebin.com/d9GUvM3N

my messages script is in: \application\messages\registration.php Any suggestions?

Sorry about long code just skip html and other stuff

Était-ce utile?

La solution

If you're catching the validation exception that is thrown by the User model, then likely your messages file location is incorrect. It needs to be: 'registration/user.php'.

// ./application/messages/registration/user.php
return array(
    'name' => array(
        'not_empty' => 'Please enter your username.',
    ),
    'password' => array(
        'matches' => 'Passwords doesn\'t match',
        'not_empty' => 'Please enter your password'
    ),
    'email' => array(
        'email' => 'Your email isn\'t valid',
        'not_empty' => 'Please enter your email'
    ),
    'about-me' => array(
        'max_length' => 'You cann\'ot exceed 300 characters limit'
    ),
    '_external' => array(
        'username' => 'This username already exist'
    )
);

Also, contrary to Michael P's response, you should store all validation logic in the model. Your controller code, to register a new user, should be as simple as:

try
{
  $user->register($this->request->post());

  Auth::instance()->login($this->request->post('username'), $this->request->post('password'));
}
catch(ORM_Validation_Exception $e) 
{
  $errors = $e->errors('registration');
}

Autres conseils

You should be validating the post data before attempting to hit any models. Your validation rules are not being executed because you haven't performed a validation check().

I would do something like:

// ./application/classes/controller/user
class Controller_User extends Controller
{

    public function action_register()
    {

        if (isset($_POST) AND Valid::not_empty($_POST)) {
            $post = Validation::factory($_POST)
                ->rule('name', 'not_empty');

            if ($post->check()) {
                try {
                    echo 'Success';
                    /**
                    * Post is successfully validated, do ORM
                    * stuff here
                    */
                } catch (ORM_Validation_Exception $e) {
                    /**
                    * Do ORM validation exception stuff here
                    */
                }
            } else {
                /**
                * $post->check() failed, show the errors
                */
                $errors = $post->errors('registration');

                print '<pre>';
                print_r($errors);
                print '</pre>';
            }
        }
    }
}

Registration.php stays mostly the same, with the exception of fixing up the 'lenght' spelling mistake you had:

// ./application/messages/registration.php
return array(
    'name' => array(
        'not_empty' => 'Please enter your username.',
    ),
    'password' => array(
        'matches' => 'Passwords doesn\'t match',
        'not_empty' => 'Please enter your password'
    ),
    'email' => array(
        'email' => 'Your email isn\'t valid',
        'not_empty' => 'Please enter your email'
    ),
    'about-me' => array(
        'max_length' => 'You cann\'ot exceed 300 characters limit'
    ),
    '_external' => array(
        'username' => 'This username already exist'
    )
);

Then, sending an empty 'name' field will return:

Array
(
    [name] => Please enter your username.
)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top