Domanda

Ho lo script di validazione completamente funzionante Il mio problema è che non riesco a ricevere messaggi di errore personalizzati

Ecco la mia funzione per la registrazione: http://pastebin.com/zf3uvxur

Ed ecco il mio array di messaggi: http://pastebin.com/d9guvm3n

Il mio script di messaggi è in: \application\messages\registration.php Eventuali suggerimenti?

Scusa per il codice lungo, salta HTML e altre cose

È stato utile?

Soluzione

Se stai catturando l'eccezione di convalida lanciata dal modello utente, probabilmente la posizione del file dei messaggi non è corretta. Deve essere: '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'
    )
);

Inoltre, contrariamente alla risposta di Michael P, tu dovrebbe Memorizza tutta la logica di convalida nel modello. Il codice del controller, per registrare un nuovo utente, dovrebbe essere semplice come:

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');
}

Altri suggerimenti

Dovresti convalidare i dati post prima di tentare di colpire qualsiasi modello. Le tue regole di convalida non vengono eseguite perché non hai eseguito un Controllo di convalida ().

Farei qualcosa come:

// ./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 rimane principalmente uguale, con l'eccezione di sistemare l'errore di ortografia "Lenght" che hai avuto:

// ./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'
    )
);

Quindi, l'invio di un campo "Nome" vuoto tornerà:

Array
(
    [name] => Please enter your username.
)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top