Sorry, last attempt said my message was expired for some odd reason.
Good thing I had my post saved in the ole clipboard.
I'm trying to figure out the "proper" way to create and submit an ajax
form, validate the data, and return a success or error messages to the
original div.
Simply stated:
1) User submits ajax form
2) Submitted data is validated (against the model)
3) Error Messages/Success Message are generated and original contact
form is overwritten with these messages
4) Data is saved/sent (if validated properly)
As an example, I have a short & simple contact form below that should
help clarify what I'm trying to do.
[Code Snippet From The View]
-------------------------------------------------------------------
<div id="shortContact">
<form onSubmit="return false;">
<p>
<label>Your Name: </label>
<?php echo $html->input('shortContact/name',
array('size' =>
'30')); ?>
<?php echo $html->tagErrorMsg('shortContact/name', 'A
name is
required.'); ?>
</p>
<p>
<label>Your Email: </label>
<?php echo $html->input('shortContact/email',
array('size' =>
'30')); ?>
<?php echo $html->tagErrorMsg('shortContact/email', 'An
email
address is required.'); ?>
</p>
<?php echo $html->hidden('shortContact/phone'); ?>
<p>
<?php echo $ajax->submit('Get In Touch', array('url' =>
'shortContact', 'update'=>'shortContact')); ?>
</p>
</form>
</div>
-------------------------------------------------------------------
[Code Snippet From The Controller]
-------------------------------------------------------------------
function shortContact ()
{
//make sure data was submitted
if (!empty($this->data))
{
//the phone field is a hidden field 'spam checker'
//it should always be empty unless being spammed???
if (empty($this->data['shortContact']['phone'])) {
if($this->ShortContact->validates($this->data)) {
$this->sendEmail($this->data['shortContact']);
$this->render('success_contact');
} else {
$this->render('error_contact');
}
}
}
}
-------------------------------------------------------------------
[ShortContact Model Snippet]
-------------------------------------------------------------------
class ShortContact extends AppModel
{
var $name = 'ShortContact';
var $useTable = false;
var $validate = array(
'name' => VALID_NOT_EMPTY,
'email' => VALID_EMAIL);
}
-------------------------------------------------------------------
So, here's my issues:
1) If I have properly implemented the ajax->submit() method, then
perhaps someone can enlighten me how to get validation working.
Currently, I the data gets to the controller perfectly, but the
validation doesn't work (eg: validation always passes - no errors ever
returned).
2) How do I get the 'errors' back to the original view to update the
tagErrorMsg with the error. The render statements seem to be the key
here. In simple terms: rendering a success message and overwriting
the 'shortContact' div is no problem at all. However, if there's an
error in the form and I render the "error_contact" to overwrite the
'shortContact' div, the errors pass through to the original view. The
issue seems to revolve around how the Model handles error message.
3) I can't seem to figure out how to properly use the $ajax->form()
methods. This was my original preferable method, but I ran into a lot
of snags. I see the usage in the API, but I'm a tad confused on how
to implement it with the form. Do I use a regular submit button? Do
I need a button with: onclick="this.form.submit();" or something else
to actually submit the form? Any help in that area would be vastly
appreciated. (Documentation is sparse on this topic)
4) Does anyone think my 'spam checking method' will work? I assumed
that any field named "phone" in a contact form would be filled out by
a spam-bot. A normal user would never see this hidden field, so it
should work ok. I guess I need a way to test spammers, heh. Any
comments on this method would be appreciated.
Thanks for your help in advance!
Dustin
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---