Search code examples
phpyii

How to validate multiple forms inline on a single IF?


For logical and faster purposes, whenever you make PHP try two statements using an AND condition, if the first one fails he doesn't attempt to try the second one... for logical and faster purposes that is beautiful indeed, but on my case I would need to know if the second statement also didn't work, so I don't need to make an user go through the same page a lot of times because the mistake was different than before...

So, assuming I have this:

    if ($firstModel->validate() && $secondModel->validate()) {$sayCheese;}

How do I make it try both?

I did consider separating the models but I just want to know if there's an alternative.

Edit: My form has multiple inserts for different models at the same time so the validation works beautifully if for a single model only, and since I need to try two or more at the same time, NOT stopping at the first statement is a MUST.


Solution

  • if ($firstTry & $thenTry) {$sayCheese;}
    

    This will do a bitwise and, and check $thenTry even if $firstTry is false.

    Edit : note I am just naively replying to your question, but I am not entirely sure why you would realistically want to do this.