Im a copy and paste programmer and try to put together some functionality for our non-profit project. I nearly succeeded in what the subject says. Because of using Bootstrap Im not using {{ form_open }} and there for Im not succeeding with retrieving the Input values when redirected to a error message. Means, someone fills in everything, one field is wrong, thats gets displayed beautifully but all data is lost. How do I do that in the view and in the controller file?
I found my way through googeling stack overflow and laravel cheat sheets and a dear friend.
Here is the code, including saving fields into pivot table (schoollevels and stakeholders), bootstrap UX (aria compatible), ajax validation error messages and flash success message at the end.
from a-z all is multilanguage! It was struggle over a full week for me, I hope this helps the next ones.
<?php namespace Ffte\Forms\Components;
use Cms\Classes\ComponentBase;
use Input;
use Mail;
use October\Rain\Exception\ValidationException;
use Validator;
use Redirect;
use Flash;
use Ffte\Crm\Models\Contact;
use Ffte\Crm\Models\ContactStakeholder;
use Ffte\Crm\Models\Schoollevel;
use Ffte\Crm\Models\ContactSchoollevel;
use Ffte\Forms\Models\LogTeacherEntry;
use RainLab\Translate\Models\Message;
class CreateTeacher extends ComponentBase
{
public function componentDetails()
{
return [
'name' => 'Teacher Entry',
'description' => 'Creates a complete form for an entry of a new teacher into our CRM'
];
}
public function onRun()
{
$this->schoollevels = $this->loadSchoollevels();
}
protected function loadSchoollevels()
{
return Schoollevel::all();
}
public $schoollevels;
public $success;
public function onSend()
{
// Validation
$validator = Validator::make(
[
'salutation' => Input::get('salutation'),
'firstname' => Input::get('firstname'),
etc
],
[
'salutation' => 'required',
'firstname' => 'required',
'lastname' => 'required',
'email_b' => 'required|email',
etc
],
[
]
);
if ($validator->fails()) {
throw new ValidationException($validator);
} else {
// Saving data in to databases
// save into Contact
$teacher = new Contact();
$teacher->anrede = Input::get('salutation');
$teacher->vorname = Input::get('firstname');
etc
$teacher->geschichte = ' * Automatischer Eintrag durch Schulverteiler-Formular am '.date('Y-m-d').' *';
$teacher->save();
// save into Contact.Stakeholder Pivot
$contact_id = $teacher->id; // retrieves the id of the last entry above
$stakeholder = new ContactStakeholder();
$stakeholder->stakeholder_id = '6'; // replaces hidden input. 6 = Teacher
$stakeholder->contact_id = $contact_id;
$stakeholder->save();
// save into Contact.Schoollevels Pivot
$contact_id = $teacher->id;
$schoollevel = new ContactSchoollevel();
$schoollevel->schoollevel_id = Input::get('schoollevel');
$schoollevel->contact_id = $contact_id;
$schoollevel->save();
//save into LogTeacherEntry
$log = new LogTeacherEntry();
$schoollevel_id = Input::get('schoollevel');
$nameofschoollevel = Schoollevel::find($schoollevel_id)->name;
//$log->fill(Input::get());
$log->salutation = Input::get('salutation');
$log->firstname = Input::get('firstname');
etc
$log->save();
//Send Mails
// values for mail
$vars = [
'salutation' => Input::get('salutation'),
'firstname' => Input::get('firstname'),
etc
];
// Mail for User
Mail::send('ffte.forms::createTeacher', $vars, function ($message) {
$fullname = Input::get('firstname');
$fullname .= Input::get('lastname');
$message->to(Input::get('email_b'), $fullname);
$message->from(Message::trans('noreply.mail'), Message::trans('site.name'));
});
// Mail for staff
Mail::send('ffte.forms::createTeacher.data', $vars, function ($message) {
$message->to('you@yourorganization.com', '');
//$message->bcc(xy@xx.org, '');
$message->from('noreply@fixx.org', Your Organization');
});
// Flash Success Message
return [
'#wrapper' => $this->renderPartial('@success.htm')
];
}
}
}
Note: The Pivot tables need with this code a Model each (has to set up manually or with the Builder Plugin). Bootstrap CSS, jQuery, Translate Plugin needed. LogTeacherEntry is just Forms Log for me, not needed.
Last updated
1-3 of 3
You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.