form – nevykresli se success hlaska
Upozornění: Tohle vlákno je hodně staré a informace nemusí být platné pro současné Nette.
- Mira8
- Člen | 36
Ahoj,
mam problem s vykreslenim success hlasky po odeslani formulare, error hlasky a
odesilani formulare je funkcni.Success hlaska se ale po odeslani nezobrazi.
Na stranku s formularem se dostavam pres handle signal
<a n:href="contact!" class="active ajax"><img src="{$basePath}/images/contact.png" id="contactIcon"></a>
zpracovani v presenteru:
public function handleContact()
{
$this->setView('contact');
$this->redrawControl('contentVideos');
}
zpracovani formulare:
/**
*
* @return UI\Form
*/
protected function createComponentContactForm()
{
$form = new UI\Form;
$form->getElementPrototype()->data['ajax-contactForm'] = '';
$form->addText('email', 'Email:')
->addCondition(UI\Form::FILLED)
->addRule(UI\Form::EMAIL, 'The email address is not valid.');
$form->addTextArea('message', 'Let us know your special requirements:')
->setRequired('Please type your message.');
$form->addSubmit('send', '');
$form->addProtection('Submission timed out. Please try again.');
$form->onSuccess[] = array($this, 'contactFormSucceeded');
$form->onError[] = array($this, 'contactFormError');
return $form;
}
public function contactFormError(UI\Form $form) {
foreach($form->getComponents() as $component) {
if(!empty($component->errors)) {
$this->payload->errors[$component->name] = $component->errors[0];
}
}
$this->sendPayload();
}
/**
*
* @param UI\Form $form
* @param $values
*/
public function contactFormSucceeded(UI\Form $form, $values)
{
$email = (isset($values->email)) ? $values->email : '';
$bodyMessage = $values->message;
$message = new Nette\Mail\Message();
if ($email) {
$bodyMessage .= "\nEmail: " . $email;
$message->setFrom($email);
}
foreach($this->config->getEmails() as $emailOur) {
if (filter_var($emailOur, FILTER_VALIDATE_EMAIL)) {
$message->addTo($emailOur);
}
}
$message->setSubject('contact')
->setBody($bodyMessage);
$mailer = new Nette\Mail\SendmailMailer();
try {
$mailer->send($message);
} catch (Nette\InvalidStateException $e) {
$form->addError('There was a problem sending message. Please repeat your action later.');
return;
}
$this->flashMessage(self::CONTACT_SENT_SUCCESS, 'success');
if (!$this->isAjax()) {
$this->redirect('this');
} else {
$form->setValues(array(), TRUE);
$this->payload->success = true;
$this->redrawControl('flashesContact');
}
}
V contact.latte:
{snippet flashesContact}
{foreach $flashes as $flash}
{if ($flash->type !== 'success')}
<br/>
<div style="font-size:110%" class="inner-success flash {$flash->type}">{$flash->message}</div>
{/if}
{if $flash->type === 'success'}
<div>
<p>{$flash->message}</p><br>
<span>
Your opinion is valuable to us!
</span>
</div>
{/if}
{/foreach}
{/snippet}
{snippet contactForm}
{var $formClass = 'form-std'}
{foreach $flashes as $flash}
{if $flash->message === App\Presenters\BasePresenter::CONTACT_SENT_SUCCESS}
{var $formClass .= ' is-sent'}
{/if}
{/foreach}
{form contactForm class => $formClass . ' contact-form ajax form-std form-contact'}
<fieldset>
<p align="center">
{input message, rows => 10,
placeholder =>
'We’re open to your feedback, cooperation proposal or any tips and ideas!', class=> 'inp-text'}
<span class="text-danger" n:ifcontent>{$form['message']->error|noescape}</span>
</p>
<p>
<br><span>Would you like to get an answer from us?</span>
<ul class='tg-list'>
<li class='tg-list-item'>
<input class='tgl tgl-skewed' id='cb3' type='checkbox'>
<label class='tgl-btn' data-tg-off='NO' data-tg-on='YES' for='cb3'></label>
</li>
</ul>
</p><br>
<p align="center">
{input email, placeholder => 'Email', class=> 'inp-text'}
</p>
<span class="text-danger" n:ifcontent>{$form['email']->error|noescape}</span>
<p class="reset">
<button n:name="send" type="submit" class="btn btn-m">
<span>Send message</span>
</button>
</p>
</fieldset>
{/form}
{/snippet}
v main.js mam jeste rozsireni:
$.nette.ext('contactForm', {
success: function(payload) {
if(payload.hasOwnProperty('errors')) {
$('.error').remove();
for(var key in payload.errors) {
if(key == 'email' && !$('#cb3').is(':checked')) {
} else {
$('[name="' + key + '"]').after('<p class="error">' + payload.errors[key] + '</p>')
}
}
} else if(payload.hasOwnProperty('success')) {
$('#frm-contactForm, #contactInfoText').css('display', 'none');
}
}
});
Kde by mohl byt problem? Diky moc za rady!