This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
Hi to all!
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?
The default.htm;
<form data-request="onSend" data-request-flash>
<div class="form-group {% if errors.first('firstname') %} has-error has-feedback {% endif %}">
<label for="firstname" class="form-control-label {% if errors.first('firstname') %} text-danger {% endif %}">{{ 'forms.label.firstname'|_ }}</label>
<input type="text" name="firstname" value="????????" class="form-control" aria-describedby="inputError2Status">
<span class="glyphicon glyphicon-remove form-control-feedback" aria-hidden="true"></span>
<span id="inputError2Status" class="sr-only">(error)</span>
<span class="help-block bg-danger">{{ errors.first('firstname') }}</span>
</div>
...
</form>
My question: Can in the onSend Class a array be made which can be read with twig variables as:
in the php:
$vars = [
'firstname' => Input::get('firstname'),
'lastname' => Input::get('lastname'),
'organization' => Input::get('organization'),
'email_b' => Input::get('email_b')
];
in default.htm:
<input type="text" name="firstname" value="{{ vars('firstname') }}" class="form-control" aria-describedby="inputError2Status">
??
It seems as no one out there tries the same, Im very thankfull for complete answers I often have some gaps in my Ocotber knowledge. Thank you!
Last updated
and here my full controller code
<?php namespace Ffte\Forms\Components;
use Cms\Classes\ComponentBase;
use Input;
use Mail;
use Validator;
use Redirect;
use Flash;
use Ffte\Crm\Models\Contact;
use Ffte\Crm\Models\Schoollevel;
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 function onSend(){
// Saving data in to databases
$teacher = new Contact();
$teacher->vorname = Input::get('firstname');
$teacher->nachname = Input::get('lastname');
$teacher->save();
$log = new LogTeacherEntry();
$log->firstname = Input::get('firstname');
$log->lastname = Input::get('lastname');
$log->save();
// Validation
$validator = Validator::make(
[
'salutation' => Input::get('salutation'),
'firstname' => Input::get('firstname'),
'lastname' => Input::get('lastname'),
'email_b' => Input::get('email_b'),
'organization' => Input::get('organization'),
'position' => Input::get('position'),
//'schoollevel' => Input::get('schoollevel'),
'com_lang' => Input::get('com_lang')
],
[
'salutation' => 'required',
'firstname' => 'required',
'lastname' => 'required',
'email_b' => 'required|email',
'organization' => 'required',
'position' => 'required',
//'schoollevel' => 'required',
'com_lang' => 'required'
],
[
'salutation.required' => Message::trans('forms.messages.salutation.required'),
'firstname.required' => Message::trans('forms.messages.firstname.required'),
'lastname.required' => Message::trans('forms.messages.lastname.required'),
'email_b.required' => Message::trans('forms.messages.email_b.required'),
'email_b.email' => Message::trans('forms.messages.email_b.email'),
'organization.required' => Message::trans('forms.messages.organization.required'),
'position.required' => Message::trans('forms.messages.position.required'),
//'schoollevel' => Message::trans('forms.messages.schoollevel.required'),
'com_lang.required' => Message::trans('forms.messages.com_lang.required')
]
);
if($validator->fails()){
return Redirect::back()->withErrors($validator);
/*HOW IS THIS DONE??????
$formvalues = [
'firstname' => Input::get('firstname'),
'lastname' => Input::get('lastname'),
'organization' => Input::get('organization'),
'email_b' => Input::get('email_b')
];
HOW IS THIS DONE??????*/
} else {
//Send Mails
// values for mail
$vars = [
'firstname' => Input::get('firstname'),
'lastname' => Input::get('lastname'),
'organization' => Input::get('organization'),
'email_b' => Input::get('email_b')
];
// Mail for User
Mail::send('ffte.forms::createTeacher', $vars, function($message) {
$message->to(Input::get('email_b'), Input::get('firstname')); // wie kriege ich hier firstname und lastname rein?
$message->from(Message::trans('noreply.mail'), Message::trans('site.name'));
});
// Mail for staff
Mail::send('ffte.forms::createTeacher.data', $vars, function($message) {
$message->to('MAIL@GMAIL.COM', '');
//$message->bcc(xy@XX.org, '');
$message->from('noreply@xx.org', 'someone');
});
// Flashing messages
Flash::success(Message::trans('forms.createTeacher.flash.success'));
}
}
}
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.
default.htm
{% set schoollevels = __SELF__.schoollevels %}
<div id="wrapper">
<form data-request="onSend" data-request-validate data-request-flash>
<div class="form-group">
<label class="control-label" for="salutation">{{ 'forms.label.salutation'|_ }}</label>
<select class="form-control" name="salutation">
<option value="" selected="selected">{{ 'forms.pleasechoose'|_ }}</option>
<option value="{{ 'forms.option.value.ms'|_ }}">{{ 'forms.option.label.ms'|_ }}</option>
<option value="{{ 'forms.option.value.mr'|_ }}">{{ 'forms.option.label.mr'|_ }}</option>
</select>
<span class="help-block bg-danger" data-validate-for="salutation">{{ 'forms.messages.salutation.required'|_ }}</span>
</div>
<div class="form-group has-feedback">
<label class="control-label" for="firstname">{{ 'forms.label.firstname'|_ }}</label>
<input type="text" name="firstname" class="form-control" aria-describedby="firstnameStatus">
<span class="glyphicon form-control-feedback" aria-hidden="true"></span>
<span id="firstnameStatus" class="sr-only">{{ 'forms.messages.firstname.required'|_ }}</span>
<span class="help-block bg-danger" data-validate-for="firstname">{{ 'forms.messages.firstname.required'|_ }}</span>
</div>
<div class="form-group">
<label for="schoollevel" class="control-label">{{ 'forms.label.schoollevel'|_ }}</label>
<select class="form-control" name="schoollevel">
<option value="" selected="selected">{{ 'forms.label.pleasechoose'|_ }}</option>
{% for schoollevel in schoollevels %}
<option value="{{ schoollevel.id }}">{{ schoollevel.name }}</option>
{% endfor %}
</select>
<span class="help-block bg-danger" data-validate-for="schoollevel">{{ 'forms.messages.schoollevel.required'|_ }}</span>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="newsletter" class="form-control" value="1" checked />
{{ 'forms.createTeacher.label.newsletter'|_ }}
</label>
</div>
<input type="hidden" name="com_lang" value="{{ localePicker.activeLocale }}">
<button class="btn btn-primary">
{{ 'forms.label.submit'|_ }}
</button>
</form>
</div>
{% put scripts %}
<script>
$(window).on('ajaxInvalidField', function(event, fieldElement, fieldName, errorMsg, isFirst) {
$(fieldElement).closest('.form-group').addClass('has-error');
$(fieldElement).siblings('.glyphicon').addClass('glyphicon-remove');
});
$(document).on('ajaxPromise', '[data-request]', function() {
$(this).closest('form').find('.form-group.has-error').removeClass('has-error');
});
$(window).on('ajaxErrorMessage', function(event, message) {
$('#errors').html(message);
event.preventDefault();
})
</script>
The Component Class:
<?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')
];
}
}
}
success.htm
<h1>{{ 'forms.createacher.flash.success.title'|_ }}</h1>
<div class="alert alert-success">
{{ 'forms.createTeacher.flash.success.text'|_ }}
</div>
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