Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
validation problem
  • public function action_chk_login()
    {
    $val = Validation::forge();
    $val->add('pass', 'password')->add_rule('required')->add_rule('min_length',3);
    $val->add('uname', 'User Name')->add_rule('required'); if($val->run())
    {
    echo "ok";exit;
    }
    else
    {
    $val->set_message('required', 'The field :label is required.');
    echo $val->errors('uname')->get_message('The field :label must be filled out before auth is attempted.');
    exit;
    }
    }
    I got error "Call to a member function get_message() on a non-object"
    and how to pass that error message in view
    any idea !!
    Thank you
  • The Validation class has a show_errors() method that displays all errors using the template variables found in the config. It does not have the option to request the errors of a single field. If you want this, your best bet is probably extend the Validation class in app/classes, and add a show_error($field, $options) to do the same as show_errors(), but for a single field.
  • Thank you for clarification. I will try to follow your lead on extending Validation class.
  • You can't use it like this. $val->error('fieldname') returns false if there was no error for 'fieldname', it only returns a Validation_Error object is the validation of the 'fieldname' failed. p.s. don't use errors(), it's deprecated. Use error() instead.
  • I use error() for password field but still I face problem "Call to a member function get_message() on a non-object" echo $val->error('uname')->get_message('The field :label must be filled out before auth is attempted.');
    echo $val->error('pass')->get_message('The field :label must be filled out before auth is attempted.');
    exit; If i pass both value(user name and password) then I do not get error
    And if any of them is miss then I got exception error "Call to a member function get_message() on a non-object" and if I do not pass (null value) then that errors are echo "The field UserName must be filled out before auth is attempted and The field password must be filled out before auth is attempted"
  • Did you run the validation? The last error message is from your authentication solution, not from the validation.
  • Harro Verton wrote on Thursday 12th of January 2012:
    Did you run the validation? The last error message is from your authentication solution, not from the validation.
    $val = Validation::forge();
    $val->add('pass', 'password')->add_rule('required')->add_rule('min_length',3);
    $val->add('uname', 'User Name')->add_rule('required'); if($val->run())
    {
    echo "ok";exit;
    }
    else
    {
    $val->set_message('required', 'The field :label is required.');
    echo $val->error('uname')->get_message('The field :label must be filled out before auth is attempted.');
    echo $val->error('pass')->get_message('The field :label must be filled out before auth is attempted.');
    } Still Problem please help me
  • Try this: $val = Validation::forge();
    $val->add('pass', 'password')->add_rule('required')->add_rule('min_length',3);
    $val->add('uname', 'User Name')->add_rule('required'); if($val->run())
    {
    echo "ok";exit;
    }
    else
    {
    $val->set_message('required', 'The field :label must be filled out before auth is attempted.');
    echo $val->error('uname');
    echo $val->error('pass');
    }
  • kevin sakhuja wrote on Saturday 14th of January 2012:
    Try this: $val = Validation::forge();
    $val->add('pass', 'password')->add_rule('required')->add_rule('min_length',3);
    $val->add('uname', 'User Name')->add_rule('required'); if($val->run())
    {
    echo "ok";exit;
    }
    else
    {
    $val->set_message('required', 'The field :label must be filled out before auth is attempted.');
    echo $val->error('uname');
    echo $val->error('pass');
    }

    @kevbook
    Thx brother...
    Problem solved
  • I am getting the same error, but I am trying to do slightly different thing. In my view I have this code for each individual input field:
    <?php echo Form::label('Name', 'name'); ?>
    <?php echo Form::input('name', Input::post('name')); ?>
    <?php echo $val->error('name'); ?>
    

    This echoes out the error for this field if there were any errors, otherwise it echoes nothing. Not I want to add some styling to this error messages and I want to set open tag and close tag. If I do it like this:
    <?php echo Form::label('Name', 'name'); ?>
    <?php echo Form::input('name', Input::post('name')); ?>
    <?php echo $val->error('name')->get_message(false, '<div class="alert">', '</div>'); ?>
    

    this only works if there is an error. If there is no error, I get this message: ErrorException [ Error ]: Call to a member function get_message() on a non-object. How can I set open and close tag for individual error messages?
  • $val->error() returns:
    - false if no errors were detected, or if the requested field is not in error
    - a Validation_Error object in case the field is in error
    - an array of Validation_Error objects in case you don't pass a fieldname and there were errors. So you can't just chain get_message() to it, as that is a method of the Validation_Error class.
  • Thank you for explaining why I am getting the error when chaining $val->error()->get_message(). But is it possible to set open and close tags for individual error messages? How? In Validation config there are only 'open_list', 'close_list' and 'open_error', 'close_error' settings, which are probably used if one would like to output all errors in a list (above the form for example). And the only method in Validation and Validation_Error that accepts open and close parameters is Validation_Error::get_message($msg = false, $open = '', $close = ''). Am I missing something?

Howdy, Stranger!

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

In this Discussion