Welcome Guest, Not a member yet? Register   Sign In
form_validation->set_message
#1

[eluser]Narkboy[/eluser]
Hi - I'm new to the forum, so Hi!, and to CI, which in 3 days is already making life a _lot_ easier - fantastic!

I have a small issue with form_validation.. I've set individual error messages for the rules I'm using which all works fine, but I what I want to do is to have a message at the top of the form saying 'Sorry- validation error, see below'. I thought of just adding it as $data before sending it to the view, but this will appear on the first showing of the form.

So - how do I change the validation_errors() to read a specific string?


Thanks in advance!!

/N
#2

[eluser]jedd[/eluser]
Welcome to CI, and welcome to the forums.

I am pretty sure (but my memory may be playing up) that this is described quite nicely in the third video tutorial. If you haven't gone through all three of the video tutorials, and especially as you're new to CI, I'd recommend you check them out. They are extremely well made.
#3

[eluser]Narkboy[/eluser]
Excellent tip on watching the tutorials - they are really helpful thanks, but sadly don't have anything on the validation messages. Anyone?
#4

[eluser]pistolPete[/eluser]
There are several possibilities:

Either use:
Code:
// in your view
if(!empty(validation_errors()))
{
   echo 'Sorry- validation error, see below';
}

Or use:
Code:
// in your controller
if ($this->form_validation->run() == FALSE)
{
   $data['message'] = 'Sorry- validation error, see below';
   $this->load->view('myform', $data);
}
else
{
   $this->load->view('formsuccess');
}
Code:
// in your view
echo $message;
#5

[eluser]Narkboy[/eluser]
Excellent - thanks!

In the second option, how do you prevent the error message from appearing the first time the form view is shown (i.e. before the user has actually submitted any data). I'd rather keep the view as free from PHP as possible..
#6

[eluser]pistolPete[/eluser]
Are you using more than one method?

Code:
function index()
{
   // load form view
   // form action = .../save
}
function save()
{
   // run form validation
   // display your sorry message here
}

Or check if submit button has been pressed:
Code:
// i assume your submit button has the name="submit"
if($this->inpud->post('submit') !== FALSE) ...
#7

[eluser]Narkboy[/eluser]
I'm using one method to both display the form and process the input, so I'll use your send method. Thank you so much!! Smile




Theme © iAndrew 2016 - Forum software by © MyBB