Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Validation, problem with the error messages
  • Hi,
    I can't view the error messages in a validation form. The validation rules are working fine, but i can't show the messages.

    I've the validation rules in the model:

    public static function validate_register_form() {
    $val = Validation::forge('registro');
    $val->add_field('email', 'E-mail', 'required|valid_email|max_length[100]');
    $val->add_field('name', 'Nombre', 'required|max_length[255]');
    $val->add_field('surname', 'Apellidos', 'required|max_length[255]');
    $val->add_field('company', 'Empresa', 'required|max_length[255]');
    $val->add_field('tel', 'Teléfono', 'required|max_length[50]');
    $val->add_field('password', 'Contraseña', 'required|max_length[100]');
    return $val;
    }


    I call the validation from the controller/action:

    public function action_register() {
    if (Input::method() == 'POST') {
    $val = Model_User::validate_register_form();

    // Error en el form
    if ($val->run()) {

    // Continue coding here...

    //Session::set_flash('success', 'FORM OK');
    //Response::redirect('login');

    } else {

    //Debug::dump($val);
    //die();

    Session::set_flash('error_registro', $val->error());
    Response::redirect('login');
    }
    }
    }


    The validations rules are working, but the error messages is no saving in the Session::set_flash... Are empty.

    I check this in teh view:


    <?php if (Session::get_flash('error_registro')): ?>

    <?php echo implode('<br>', e((array) Session::get_flash('error_registro'))); ?>

    <?php endif; ?>


    I'm using the same system in other project and is working... At this point, I'm lost.

    Thanks!
  • HarroHarro
    Accepted Answer
    What are you using as Session backend?

    If you're using cookie based sessions, note that you're storing Validation_Error objects, which can be quite large, too many of them will cause the cookie to be too large, and discarded.

  • Great! I was using with "cookie", I changed to "file" and works.

    Thanks!
  • It's a lot more economical to pull the errors from the object, and only pass the strings to the View.

    We use a central Messaging class in our applications, which act like a container for all kinds of messages, and is queried by the page template to display the errors.

    A simplified version can be found here: https://github.com/fuel/depot/tree/1.0/develop/fuel/app/classes. It will automatically extract the validation message in case you pass a Validation_Error object.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion