Very basic error handling question

[Edit] Topic locked - solution found :)

Trying to cipher through to find something simple and elegant to just do the following.

have block of code

if condition

—> do this bla bla

else

—> set $message to my custom string error

—> call function x($message) to display error.

Pretty basic, I've looked around and read up on errorSummary() and getErrors() but they don't conform to what I'm looking for.

Kind of like the same idea as what userIdentity has defining it's own errors but I don't want to add continuous amounts of errors to be stored and then used only in one place, I'd rather use what I need when I need it.

Any help would be greatly appreciated :)

not quite sure what you want to achieve… Are you trying to display an error message after each input field that has error? If so, you can use CHtml::error().

It's like searching through a database query and then pulling up an error when the query fails (or in my case comes true).

For example, trying to make sure an entry to the database is unique, if the query returns a result I want to display an error saying that it has to be unique.

still not sure the problem here… if you mainly want to display this error when it occurs, you could declare a controller property named $error and assign to it when the error occurs. Then in your view, you can do: if($this->error) … else …

Ok that sounds like a standard approach - just means I'd have to code it in for all the functions within that controller.

Here's a curious question that may avoid some extra work. What I've done is made my own registration page in which case I want to make sure the username and email are unique.

Now I went on to make my own validation function to check if they are unique but is something like this already implemented through the save() aspect of an AR model.

like (this is not what I'm currently doing although it looks a lot easier):

$user=new User;

$user->username=$_POST['username'];

$user->email=$_POST['email'];

$user->save();  <- is there a way to check if $user->username and $user->email are unique before saving?

Or should I just overwrite the default beforeValidate() of save()?

For this unique problem, in your user model, you can declare a rule

array('username, email', 'unique'),

Ahh cool ok that’ll work haha Thanks :)