Welcome Guest, Not a member yet? Register   Sign In
Problems getting a simple form working....
#1

[eluser]Lee Mc[/eluser]
Hi,

I've put together a simple contact form using CI, but the problem is i'm not that familiar with CI or even PHP if i'm honest.

So I got this form mostly working, but can't figure out how to generate error messages on screen when my validation routine fails. Any ideas...?? I've found most other things in the documentation relatively straightforward, but the validation stuff isn't very clear...

Any help appreciated (I start my day job in 7 hours...)

Here's my controller code if that helps:

class Rsvp extends Controller {
function Rsvp()
{
parent::Controller();
$this->load->helper(array('form', 'url'));
$this->load->library('validation');
}

function index()
{
$rules['firstname'] = "callback_firstnamechk";
$rules['surname'] = "required";

$this->validation->set_rules($rules);

if ($this->validation->run() == FALSE)
{
$this->load->view('containerrsvp');
}
else
{
$this->load->library('email');
$this->email->from('Wedding Site', 'Guest');
$this->email->message($msg);
$this->email->send();
$this->load->view('containerrsvpthanks');
}
}

function firstnamechk($str)
{
if ($str == NULL)
{
$this->validation->set_message('firstnamechk', 'You forgot to give us your &#xst;r');
return FALSE;
}
else
{
return TRUE;
}
}
#2

[eluser]danoph[/eluser]
put the following line above your form in your view:

Code:
<?=$this->validation->error_string?>

The user guide shows you a more in-depth explanation on how to use error codes, but that is the basic error string.
#3

[eluser]Lee Mc[/eluser]
Hi Danoph,

I have that there already. Does it need to be in a specific place within my view? I've just got it placed directly before my form at the moment.

When it validates, it takes me to my success page, but when it fails, it fails silently and reloads my form without any error messages...
#4

[eluser]danoph[/eluser]
I'm not an expert with the validation class, but maybe try declaring the field names when you declare your rules? Like:


Code:
$rules[’firstname’] = “required”;
$rules[’surname’] = “required”;

$this->validation->set_rules($rules);

$fields['firstname'] = 'First Name';
$fields['surname'] = 'Last Name';

$this->validation->set_fields($fields);

Also, it seems like your first name callback function is just checking to make sure it's not null, that's what required does, so you don't need that




Theme © iAndrew 2016 - Forum software by © MyBB