How can I adapt my form to implement bootstrap ?
My function for create form is:
private function createCreateForm(User $entity)
{
$form = $this->createFormBuilder($entity, array(
'action' => $this->generateUrl('user_create'),
'method' => 'POST',
))
->add('username')
->add('email')
->add('password', 'repeated', array(
'first_name' => 'password',
'second_name' => 'confirm',
'type' => 'password',
))
->add('submit', 'submit', array('label' => 'Create'))
->getForm();
return $form;
}
My views is
{{ form_start(form) }}
{{ form_row(form.username, { 'attr': {'class': 'form-control', 'placeholder': 'Imię oraz nazwisko'} }) }}
{{ form_row(form.password, { 'attr': {'class': 'form-control', 'placeholder': 'Password'} }) }}
It is working for username but not for the password, some sugestion ? How can i create our own form ?
My bootstrap:
<form action="#" class="form-horizontal">
<div class="form-body">
<div class="form-group">
<label class="col-md-3 control-label">NAME</label>
<div class="col-md-4">
<input type="text" class="form-control" placeholder="NAME
</div>
</div>
<div class="form-actions fluid">
<div class="col-md-offset-3 col-md-9">
<button type="submit" class="btn blue">Add</button>
<button type="button" class="btn default">Anuluj</button>
</div>
</div>
</form>
Use Mopa Bootstrapbundle https://github.com/phiamo/MopaBootstrapBundle
In your controller:
$this->createForm(
MyFormType(),
$entity,
array('attr' => array('class' => 'form-horizontal'))
);
Related
my problem is i cannot search and cannot disply the values in my texboxes.
what i want is to search the id of each user and display its data to my textbox
How can I do this on this video This Video in Laravel?
as of now I have this
here is my page
View
{!! Form::open(['action' => 'Admin\EmployeeFilemController#search', 'method' => 'POST', 'enctype' => 'multipart/form-data']) !!}
<input type="text" name="id" class="form-control" placeholder="Enter ID to Search"><br>
<input type="submit" class="btn btn-primary btn-md" name="search" value="Search Data">
{!! Form::close() !!}
Controller
public function search(Request $request){
$output = "";
$employees = DB::table('employeefms')->where('id')->get();
return redirect('/admin/employeemaintenance');
}
my View Inputs
<div class="form-group col-md-2">
{{Form::label('employee_no', 'Employee No.')}}
{{Form::text('employee_no', '',['class' => 'form-control', 'placeholder' => 'Employee No.'])}}
</div>
<div class="row">
<div class="form-group col-md-4">
{{Form::label('last_name', 'Last Name')}}
{{Form::text('last_name', '',['class' => 'form-control', 'placeholder' => 'Last Name'])}}
</div>
<div class="form-group col-md-4">
{{Form::label('first_name', 'First Name')}}
{{Form::text('first_name', '',['class' => 'form-control', 'placeholder' => 'First Name'])}}
</div>
</div>
<div class="row">
<div class="form-group col-md-4">
{{Form::label('middle_name', 'Middle Name')}}
{{Form::text('middle_name', '',['class' => 'form-control', 'placeholder' => 'Middle Name'])}}
</div>
<div class="form-group col-md-4">
{{Form::label('nick_name', 'Nick Name')}}
{{Form::text('nick_name', '',['class' => 'form-control', 'placeholder' => 'Nick Name'])}}
</div>
</div>
You don't seem to pass the id entered by the user in your controller function.
$employees = DB::table('employeefms')->where('id')->get();
You may have to do the following changes
$input = $request->all();
$id = $input['id']
// $employees = DB::table('employeefms')->where('id', $id)->get();
// actually, if 'id' is the primary key, you should be doing
$employee = DB::table('employeefms')->find($id);
// now pass the data to the view where you want to display the record
// like so
return view('name_of_view', compact('employee'));
Then, use Laravel's Form-Model binding
{!! Form::model($employee,
['action' => ['Admin\EmployeeFilemController#update', $employee->id],
'method' => 'patch' // or whatever method you have defined
]) !!}
// your form fields specified above will go here
{!! Form::close() !!}
I am using Symfony 3 to create a web-application. I am using FOSUserBundle. I want user to register theirself, so I wrote a custom template and RegistrationFormType. My problem is, that the fiel "email" is not filled correctly in the controller. Every submit I get following error: https://ibb.co/cYkp8y
I am using the default controller from FOSUserBundle.
My template:
{{ form_start(form, {'method': 'post', 'action': path('fos_user_registration_register'), 'attr': {'class': 'form-group'}}) }}
<fieldset>
<div class="form-group">
{{ form_widget(form.forename, {'attr': {'class': 'form-control col-md-7 col-xs-12', 'placeholder': 'Vorname'}}) }}
</div>
<div class="form-group">
{{ form_widget(form.surname, {'attr': {'class': 'form-control col-md-7 col-xs-12', 'placeholder': 'Nachname'}}) }}
</div>
<div class="form-group">
{{ form_widget(form.email, {'attr': {'class': 'form-control col-md-7 col-xs-12', 'placeholder': 'Email'}}) }}
</div>
<div class="form-group">
{{ form_widget(form.club, {'attr': {'class': 'form-control col-md-7 col-xs-12'}}) }}
</div>
<div class="form-group">
{{ form_widget(form.plainPassword.first, {'attr': {'class': 'form-control col-md-7 col-xs-12', 'placeholder': 'Passwort'}}) }}
</div>
<div class="form-group">
{{ form_widget(form.plainPassword.second, {'attr': {'class': 'form-control col-md-7 col-xs-12', 'placeholder': 'Passwort wiederholen'}}) }}
</div>
<input class="btn btn-lg btn-success btn-block" type="submit" value="Anmelden">
</fieldset>
{% do form.username.setRendered %}
{{ form_end(form) }}
My RegistrationType:
class RegistrationType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('email', EmailType::class)
->add('plainPassword', LegacyFormHelper::getType('Symfony\Component\Form\Extension\Core\Type\RepeatedType'), array(
'type' => LegacyFormHelper::getType('Symfony\Component\Form\Extension\Core\Type\PasswordType'),
'options' => array('translation_domain' => 'FOSUserBundle'),
'first_options' => array('label' => 'form.password'),
'second_options' => array('label' => 'form.password_confirmation'),
'invalid_message' => 'fos_user.password.mismatch',
))
->add('forename', TextType::class, array(
'label' => 'Vorname',
))
->add('surname', TextType::class, array(
'label' => 'Nachname'
))
->add('club', EntityType::class, array(
'class' => 'AppBundle\Entity\Club',
'multiple' => false,
'expanded' => false,
'label' => 'Vereinszugehörigkeit',
'required' => false,
))
;
}
public function getParent()
{
return 'FOS\UserBundle\Form\Type\RegistrationFormType';
}
public function getBlockPrefix()
{
return 'app_user_registration';
}
}
Does anybody know, why the controller can't handle the email?!
I have two form in one view when I leave the text field empty it redirects to wrong action. I am looking forwrad for the solution of this problem. Thanks in hope.
Form one works fine when I enter data in ll the input fields. /inquiry/store also contains http request validation.
View code which contains two forms.
<div class="col-lg-6">
{!! Form::open(['role' => 'form', 'url' => '/inquiry/store', 'class' => 'form-horizontal', 'method'=>'POST']) !!}
<div class='form-group'>
{!! Form::label('name', 'Student Name *', ['class' => 'control-label col-md-4']) !!}
<div class="col-md-8">
{!! Form::text('name', $student->name,['autocomplete'=>'off' , 'placeholder' => 'Student Name', 'class' => 'form-control']) !!}
</div>
</div>
<div class='form-group'>
{!! Form::label('father_name', 'Father Name *', ['class' => 'control-label col-md-4']) !!}
<div class="col-md-8">
{!! Form::text('father_name',$student->father_name, ['autocomplete'=>'off' , 'placeholder' => 'Father Name', 'class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('class', 'Class', ['class' => 'control-label col-md-4']) !!}
<div class="col-md-8">
{!! Form::text('class', $student->admission_class,['autocomplete'=>'off' , 'placeholder' => 'Class', 'class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('roll_no', 'Roll Number', ['class' => 'control-label col-md-4']) !!}
<div class="col-md-8">
{!! Form::text('roll_no', $student->roll_no,['autocomplete'=>'off' , 'placeholder' => 'Roll Number', 'class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('date', 'Date',['class' => 'control-label col-md-4']) !!}
<div class="col-md-8 date">
<div class="input-group input-append date" id="dateRangePicker">
{!! Form::input('date', 'date', null, ['autocomplete'=>'off' , 'placeholder' => 'Date of Birth', 'class'=>'form-control col-height datepicker']) !!}
<span class="input-group-addon add-on"><span class="glyphicon glyphicon-calendar"></span></span>
</div>
</div>
</div>
<div class="form-group">
{!! Form::label('teacher_name', 'Teacher Name', ['class' => 'control-label col-md-4']) !!}
<div class="col-md-8">
{!! Form::select('teacher_name', $teacher, ['autocomplete'=>'off' , 'placeholder' => 'Teacher Name', 'class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('remarks', 'Remarks', ['class' => 'control-label col-md-4']) !!}
<div class="col-md-8">
{!! Form::textarea('remarks', null, ['autocomplete'=>'off' ,
'placeholder' => 'Remarks',
'class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
<div class="col-xs-offset-3 col-md-12">
{!! Form::submit('Save', ['class' => 'btn btn-primary']) !!}
<input type="reset" class="btn btn-default" value="Reset">
</div>
</div>
{!! Form::close() !!}
second form for searching the student. When I submit the first form with out some empty field it redirect to /inquiry/search_stu. and displays MethodNotAllowedHttpException .
<form role="form" id="search_form" action="/inquiry/search_stu" method="post" class="form-inline">
<div class='form-group'>
<label class="control-label col-md-4">Search Student *</label>
<input class="form-control" type="text" name="search" placeholder="Admission No" required>
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class='form-group'>
<input type="submit" form="search_form" class="btn btn-primary" name="submit">
<input type="reset" class="btn btn-default">
</div>
</form>
Controller function for search student.
public function search_student(Request $request)
{
$teacher = \App\Teacher::lists('name', 'name');
$adminid = $request['search'];
$student = Admission::where('admission_no',$adminid)->first();
return View::make('/inquiry/create', ['student'=> $student,'teacher' => $teacher]);
}
When I submit the store form it redirects to search student.
store function .
public function store(Requests\StoreInquiryRequest $request) {
$input = Input::all();
$inquiry = new Inquiry();
$inquiry->name = $input['name'];
$inquiry->father_name = $input['father_name'];
$inquiry->date = $input['date'];
$inquiry->class = $input['class'];
$inquiry->teacher_name = $input['teacher_name'];
$inquiry->roll_no = $input['roll_no'];
$inquiry->remarks = $input['remarks'];
try {
$inquiry->save();
return redirect()->to('inquiry')->with('message', 'success| Student details have been saved.');
} catch (Exception $ex) {
\Log::error($ex);
return redirect()->to('inquiry')->with('message', 'error| There was an error adding new student, please try again later.');
}
}
Http request for validation when wants to store data
public function rules()
{
$cid = \Route::input('id');
$isEdit = isset($cid) ? true : false;
$rules = array(
'name' => 'required|string|Max:50',
'father_name' => 'required|string|Max:50',
'class' => 'required|string|Max:50',
'date' => 'required|date|Max:50',
'teacher_name' => 'required|string|Max:50',
'remarks' => 'required',
'roll_no' => 'required',
);
return $rules;
}
You can performe as many forms on one page as you wish. As far as I see you should have proper validtion in your controller - different validation for each form.
You need to use proper Laravel Validation. Reading this will make your coding process easier and faster: https://laravel.com/docs/5.0/validation
Example of validation in controller in Laravel 5.0:
public function store(Request $request)
{
$this->validate($request, [
'title' => 'required|unique|max:255',
'body' => 'required',
]);
}
Laravel Valiation also let you perform validations with query to database qithout writing whole SQL query: https://laravel.com/docs/5.0/validation#available-validation-rules
To display errors in view use:
#if($errors->has())
#foreach ($errors->all() as $error)
<div>{{ $error }}</div>
#endforeach
#endif
Good luck!
I have a problem, i don't know where i'm messing up but Laravel does not save data to the database and it does not display any errors. Here is my code:
User.php
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'users';
protected $fillable = array('first_name', 'last_name', 'username', 'email', 'password', 'password_temp', 'code', 'active');
/**
* The attributes excluded from the model's JSON form.
*
* #var array
*/
protected $hidden = array('password', 'remember_token');
}
Routes.php
Route::get('/', array(
'as' => 'home',
'uses' => 'PagesController#home'
));
//Unauthenticated group
Route::group(array('before'=>'guest' ), function(){
//CSRF authentication
Route::group(array('before' => 'csrf'), function(){
//create account (POST)
Route::post('/account/create', array(
'as' => 'account-create',
'uses' => 'AccountController#postCreate'
));
});
//create account GET
Route::get('/account/create', array(
'as' => 'account-create',
'uses' => 'AccountController#getCreate'
));
});
Account.php //Here is the Controller that i used to create accounts
class AccountController extends BaseController{
public function getCreate(){
return View::make('account.create');
}
public function postCreate(){
$validator = Validator::make(Input::all(),
array(
'first_name' => 'required',
'last_name' => 'required',
'username' => 'required|max:20|min:3|unique:users',
'email' => 'required|max:50|email|unique:users',
'password' => 'required|min:6',
'passsword-repeat' => 'required|same:password'
)
);
if($validator->passes()){
//activation code
$code = str_random(60);
$user = new User;
//create an account
$user->first_name = Input::get('first_name');
$user->last_name = Input::get('last_name');
$user->username = Input::get('username');
$user->email = Input::get('email');
$user->password = Hash::make(Input::get('password'));
$user->code = $code;
$user->active = 0;
$user->save();
//send email of activation
return Redirect::to('home')
->with('global', 'Your account has been created! We have sent an email to activate your account');
}else{
return Redirect::to('account/create')
->withErrors($validator)
->withInput();
}
}
}
Here is the view i have used:
Create.blade.php
#extends('master')
#section('content')
<div class="panel panel-default">
<div class="panel-heading">Please Register</div>
<div class="panel-body">
<div class="col-md-4 col-md-offset-4">
{{ Form::open(array('route' => 'account-create', 'class' => 'form-horizontal', 'method' => 'post'))}}
<fieldset>
<div class="control-group">
{{ Form::label('first_name', 'First name', array('class' => 'control-label') )}}
<div class="controls">
{{ Form::text('first_name', Input::old('first_name'), array('class' => 'form-control input-medium', 'placeholder' => 'Enter Your First Name') )}}
<p class="error">{{ $errors->first('first_name')}}</p>
</div>
</div>
<div class="control-group">
{{ Form::label('last_name', 'Last name', array('class' => 'control-label') )}}
<div class="controls">
{{ Form::text('last_name', Input::old('last_name'), array('class' => 'form-control input-medium', 'placeholder' => 'Enter Your Last Name') )}}
<p class="error">{{ $errors->first('last_name')}}</p>
</div>
</div>
<div class="control-group">
{{ Form::label('username', 'Username', array('class' => 'control-label') )}}
<div class="controls">
{{ Form::text('username',Input::old('username'), array('class' => 'form-control input-medium', 'placeholder' => 'Enter Your Username') )}}
<p class="error">{{ $errors->first('username')}}</p>
</div>
</div>
<div class="control-group">
{{ Form::label('email', 'Email', array('class' => 'control-label') )}}
<div class="controls">
{{ Form::text('email', Input::old('email') , array('class' => 'form-control input-medium', 'placeholder' => 'Enter Your Email') )}}
<p class="error">{{ $errors->first('email')}}</p>
</div>
</div>
<!-- Password input-->
<div class="control-group">
{{ Form::label('password', 'Password', array('class' => 'control-label') )}}
<div class="controls">
{{ Form::password('password', array('class' => 'form-control')) }}
<p class="error">{{ $errors->first('password')}}</p>
</div>
</div>
<div class="control-group">
{{ Form::label('password-repeat', 'Re-Enter Password', array('class' => 'control-label') )}}
<div class="controls">
{{ Form::password('password-repeat', array('class' => 'form-control')) }}
<p class="error">{{ $errors->first('password-repeat')}}</p>
</div>
</div>
<!-- Button -->
<div class="control-group">
<div class="controls">
{{Form::submit('Sign Up', ['class' => 'btn btn-large btn-primary', 'id'=> 'signup'])}}
</div>
</div>
</fieldset>
{{ Form::close() }}
</div>
</div>
</div>
#stop
It validates well but whenever i want to create a user and save data to the database nothing is happening and it returns back to the same login page with the data i tried to save without any error message . please help me if you can spot my mistake
I am try to upload a file with laravel but when submit the form it gives the below error
Exception
Serialization of 'Symfony\Component\HttpFoundation\File\UploadedFile' is not allowed
here is my blade:
{{ Form::open(array('route' => 'drivers.store', 'files' => true, 'class' => 'form-horizontal')) }}
<form role="form">
<div class="form-group first-field">
{{ Form::label('first_name', 'First Name:', array('class' => 'col-sm-3 control-label')) }}
<div class="col-xs-4 ">
{{ Form::text('first_name', $value = null, array('placeholder' => 'ex-Jon', 'required' => 'required', 'autofocus' => 'autofocus', 'class' => 'form-control' )) }}
</div>
<span class="required-symbol">* </span>
</div>
<span class='error-text'> {{ $errors->first('first_name') }} </span>
<div class="form-group">
{{ Form::label('last_name', 'Last Name:', array('class' => 'col-sm-3 control-label')) }}
<div class="col-xs-4">
{{ Form::text('last_name', $value = null, array('placeholder' => 'ex-Doe', 'required' => 'required', 'class' => 'form-control', 'class' => 'form-control')) }}
</div>
<span class="required-symbol">* </span>
</div>
<span class='error-text'> {{ $errors->first('last_name') }} </span>
<div class="form-group">
{{ Form::label('email', 'Email:', array('class' => 'col-sm-3 control-label')) }}
<div class="col-xs-4">
{{ Form::text('email', $value = null, array('placeholder' => 'ex-test#example.com', 'rows' => '3', 'required' => 'required', 'autofocus' => 'autofocus', 'class' => 'form-control' )) }}
</div>
<span class="required-symbol">* </span>
</div>
<span class='error-text'> {{ $errors->first('email') }} </span>
<div class="form-group">
{{ Form::label('contact_number', 'Phone:', array('class' => 'col-sm-3 control-label')) }}
<div class="col-xs-4">
{{ Form::text('contact_number', $value = null, array('rows' => '3', 'required' => 'required', 'autofocus' => 'autofocus', 'class' => 'form-control' )) }}
</div>
<span class="required-symbol">* </span>
</div>
<span class='error-text'> {{ $errors->first('contact_number') }} </span>
<div class="form-group">
{{ Form::label('sin', 'SIN:', array('class' => 'col-sm-3 control-label')) }}
<div class="col-xs-4">
{{ Form::text('sin', $value = null, array('rows' => '3', 'required' => 'required', 'autofocus' => 'autofocus', 'class' => 'form-control' )) }}
</div>
<span class="required-symbol">* </span>
</div>
<span class='error-text'> {{ $errors->first('sin') }} </span>
<div class="form-group">
{{ Form::label('license_number', 'License Number:', array('class' => 'col-sm-3 control-label')) }}
<div class="col-xs-4">
{{ Form::text('license_number', $value = null, array('rows' => '3', 'required' => 'required', 'autofocus' => 'autofocus', 'class' => 'form-control' )) }}
</div>
<span class="required-symbol">* </span>
</div>
<span class='error-text'> {{ $errors->first('license_number') }} </span>
<div class="form-group">
{{ Form::label('license_file', 'License File:', array('class' => 'col-sm-3 control-label')) }}
<div class="col-xs-4">
{{ Form::file('license_file') }}
</div>
<span class="required-symbol">* </span>
</div>
<span class='error-text'> {{ $errors->first('license_file') }} </span>
<div class="form-group">
{{ Form::label('street_address', 'Street Address:', array('class' => 'col-sm-3 control-label')) }}
<div class="col-xs-4">
{{ Form::text('street_address', $value = null, array('rows' => '3', 'required' => 'required', 'autofocus' => 'autofocus', 'class' => 'form-control' )) }}
</div>
<span class="required-symbol">* </span>
</div>
<span class='error-text'> {{ $errors->first('street_address') }} </span>
<div class="form-group">
{{ Form::label('password', 'Password:', array('class' => 'col-sm-3 control-label')) }}
<div class="col-xs-4">
{{ Form::password('password',array('rows' => '3', 'required' => 'required', 'autofocus' => 'autofocus', 'class' => 'form-control' )) }}
</div>
<span class="required-text">Between 6 and 12 Characters</span>
</div>
<span class='error-text'> {{ $errors->first('password') }} </span>
<div class="form-group">
{{ Form::label('password_confirmation', 'Confirm Password:', array('class' => 'col-sm-3 control-label')) }}
<div class="col-xs-4">
{{ Form::password('password_confirmation', array('rows' => '3', 'required' => 'required', 'autofocus' => 'autofocus', 'class' => 'form-control' )) }}
</div>
</div>
<div class="form-group">
{{ Form::submit('Add Driver', array('class' => 'btn btn-primary center-block sh-request-button sign-up')) }}
</div>
</form>
{{ Form::close() }}
//controller:
public function store()
{
$input = \Input::all();
////echo "</pre>";print_r($input); $file= \Input::file('license_file.name');
$validator = $this->_modelDriver->validator($input);
if ($validator->fails()) {
return \Redirect::route('drivers.create')->withInput($input)->withErrors($validator);
}
else {
echo "test success";exit;
}
}
//validator:
public function validator(array $input, $isUpdate=false)
{
if(!$isUpdate) {
$rules = array(
'first_name'=>'required|alpha|min:2',
'last_name'=>'required|alpha|min:2',
'email'=>'required|email|unique:drivers',
'password'=>'required|between:6,12|confirmed',
'password_confirmation'=>'required|between:6,12'
);
} else {
$rules = array(
'firstname'=>'required|alpha|min:2',
'lastname'=>'required|alpha|min:2',
'email'=>'required|email',
);
}
return Validator::make($input, $rules);
}
I am new to laravel. So if someone can tell what am I doing wrong and how to fix this.
Thanks
This is happening because you are trying to return with the file input.
You should write this
$input = \Input::except('license_file');
return \Redirect::route('drivers.create')->withInput($input)->withErrors($validator);
instead of only
return \Redirect::route('drivers.create')->withInput($input)->withErrors($validator);