Welcome Guest, Not a member yet? Register   Sign In
Generic message for form validation
#1

[eluser]Stompfrog[/eluser]
Hi all,

I am half way through my first CI project and I am loving it. Just got to the validation class and think it is brilliant. The documentation for CI is very useful.

I do have one question tho.

As well as the specific error messages appearing next to each invalid input field I would like to put a generic error message at the top of my form.

Something along the lines of... "There appears to be a problem with your submission please see errors below".

I tried adding the following code to my view file...
Code:
if($this->validation->run() == FALSE)
{
//message here
}

...but this function always seems to return false even the first time the form is loaded, before a submission attempt is made. This means the generic error message always displays which is not good.

Is there any other existing logic I can use to achieve this or do i need to do something more complicated?

Thanks
#2

[eluser]wiredesignz[/eluser]
Try adding if ($_POST): before if($this->validation->run() == FALSE):
#3

[eluser]garymardell[/eluser]
Well you could just use a standard view variable.

So say if your passing $data array to your view have

Code:
$data['error'] = "There appears to be a problem with your submission please see errors below"

Then in your view have

Code:
if(!empty($error))
{
echo $error;
}

Or something similar.
#4

[eluser]Stompfrog[/eluser]
@wiredesignz - thanks, works brilliantly, seems so obvious now :0)

@gazza - I thought about trying that but then surely i would need additional logic in my controller to decide whether or not to set up $data['error']. Then we are back where we started hehe.
#5

[eluser]Chillahan[/eluser]
I just set a hidden input, and then inside the code that runs after:

Code:
if($this->validation->run() == FALSE)
{

I just add an if statement checking the POST class to see if my hidden input is set, and if not, clear the error you wish to display at the top (else, set it, since you know page has been viewed at least once AND validation has failed).




Theme © iAndrew 2016 - Forum software by © MyBB