Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Gathering errors from models
  • Hi Guys,

    I was wondering how to get all errors from model and its relationships, in example I have:




    function action_index() {

    $messages = null;
    $socialNetworks = Model_Ntk_Social_Network::getAll();


    if (\Input::method() == "POST") {

    if ( \Security::check_token()) {


    try
    {
    $user = Model_Ntk_User::forge($_POST);

    if(!empty($_POST['socialnetworks'])) {
    foreach ($_POST['socialnetworks'] as $socialId => $social) {
    $socialNetwork = new Model_Ntk_Users_Social_Network();
    $socialNetwork->users_id = $user->id;
    $socialNetwork->social_networks_id = $socialId;
    $socialNetwork->description = $social;

    $user->socials[] = $socialNetwork;
    }
    }

    if ($user->save(null,true)) {
    \Response::redirect('/');
    }
    }
    catch (Orm\ValidationFailed $e)
    {
    $errors = array();


    //$messages = $e->getMessage();
    foreach ($e->fieldset()... as $error)
    {
    $errors[] = array(
    //'field' => $error->field, // the field that caused the error
    'value' => $error->value, // the value that is in error
    'message' => trim($error->get_message(false, '\t', '\t')), // the error message
    'rule' => $error->rule, // the rule that failed
    'params' => $error->params // any parameters passed to the rule
    );
    }

    // return $errors;

    print_r($errors);
    die;

    $messages = implode(",", $errors);
    }

    } else {
    $messages = "Token failed, try again please";
    }
    }

    $data = array(
    'socialnetworks' => $socialNetworks,
    'gitrepos' => $gitRepos,
    'skillcategories' => $skillcategories,
    'languages' => $languages,
    'messages' => $messages
    );

    return Response::forge(View::forge('site/index', $data, false));
    }


    But if user posts form I only get returned main model errors but not from its relationships, how can I get all the errors?

    Thanks In advance
  • At the moment you can't, the models are not aware of each other, and validation happens as part of the save operation. The first save that triggers a validation error throws the exception.
  • Hi Harro,

    Thanks for answering. How can do in fuelPHP something like: $model->validate() and it evaluates the rules from properties in the model?
  • You can't, that was the point. The validation is done by the observer, on the model. Observers are event driven, the model is not aware the observer exists.

    But you can probably "mis-use" the observer. Create a base-model class that all your models extend:

    <?php
    namespace Model;

    class Base extends \Orm\Model
    {
        public function validate()
        {
            try
            {
                $observer = new \Orm\Observer_Validation;
                $observer->validate($this);
            }
            catch (\Orm\ValidationFailed $e)
            {
                // validation failed, get the fieldset
                $fieldset = $e->get_fieldset();
                // get the errors
                $errors = $fieldset->error();
                // or display them
                $fieldset->show_errors();
            }
        }
    }

    Put this in app/classes/model, and have your models extend \Model\Base instead of \Orm\Model and you have a validate() method in all your model instances.

    nb: you probably want to do something with the errors, like return it, but I leave that to you ;-)

Howdy, Stranger!

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

In this Discussion