Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How to use validation with multiple models
  • Dear sir,
    I am sorry for asking again.
    Here I have question regarding validation.
    Is there any ways to validate the multiple models at the same time?
    For example, lets say we have 2 models called Model_A and Model_B.
    Model_A has relation with Model_B and it needs to have 2 Model_B data all the time.
    In this case, I thought I can do like following.
    $val_A = Model_A::validate('create_A');
    $val_B = Model_A::validate('create_B');
    if($val_A->run() && $val_B->run() )
    {
     $model = new Model_A();
     $model->set_attributes(Input::post());
     $model_B1 = new Model_B();
     $model_B1->set_attribuets(Input::post());
     $model_B2 = new Model_B();
     $model_B2->set_attribuets(Input::post());
     $model->B[] = $model_B2;
    }
    else
    {
     Session::set_flash('error', $val_A->error());
     Session::set_flash('error', $val_B->error());
    }
    I created some class to test above code, but, validation message is not working properly.
    If you have an idea, please let me know.
    Best regards,
  • The problem with this is that if the first validation (on model A) returns an error, the second is never executed. Try this instead:

    $val_A = Model_A::validate('create_A');
    $val_B = Model_A::validate('create_B');

    $result_A = $val_A->run();
    $result_B  =
    $val_B->run();
     
    if ($result_A and $resultB
    )
    {
      $model = new Model_A()
     
    $model->set_attributes($val_A->validated());
      $model_B1 = new Model_B();
      $model_B1->set_attribuets($val_B->validated());
      $model_B2 = new Model_B();
      $model_B2->set_attribuets($val_B->validated());
      $model->B[] = $model_B2;
    }
    else
    {
      Session::set_flash('error', $val_A->error());
      Session::set_flash('error', $val_B->error());
    }

    Do not use Input::post(), as that will give you the unvalidated data.
  • Dear Harro,

    Thank you very much for your quick response.
    I really appreciate in helping me.
    Then I try your code and found that Session::set_flash() inside else is not working so I rewrite like this.

    ■In Controller inside else
    Session::set_flash('error', 
                                 array_merge(array_merge(array(),$val_A->error_message()), $val_B->error_message()));


    If this is wrong way, please let me know.
    Best regards,
  • HarroHarro
    Accepted Answer
    Your observation is absolutely correct, you do indeed need to merge both error message array's. So your solution is spot-on.

    Note that the first array() isn't needed, this works fine:

    Session::set_flash('error', array_merge($val_A->error_message(), $val_B->error_message()));

    Please also note that you will have an issue if both A and B contains the same fieldname, as the index of the error array is the fieldname, the merge will overwrite one.
  • Dear Harror,

    Thank you for your help and suggestions.

    I finally could solve the issue.

    Yes, I noticed that if both A and B contains the same fieldname, as the index of the error array is the fieldname, the merge will overwrite one.

    Anyways, thanks :)

    Best regards,

Howdy, Stranger!

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

In this Discussion