Laravel 4 - 2 forms with the same input names - php

I have created 2 separate forms. One for signin and one for signup. They work fine on separate pages but if they are on the same page they print each others error messages. I'm guess its because they both contain the same input names.
They have separate controller methods though. Here is the example setup.
Signup form
{{ Form::open(['route' => 'signup']) }}
<div class="form-group">
{{ Form::label('email', 'Email') }}
{{ Form::text('email', null, ['class' => 'form-control']) }}
{{ $errors->first('email', '<p class="error">:message</p>')}}
</div>
<div class="form-group">
{{ Form::label('password','Paswword') }}
{{ Form::password('password', ['class' => 'form-control']) }}
<p class="help-block">Password needs to be between 6 - 8 characters</p>
{{ $errors->first('password', '<p class="error">:message</p>')}}
</div>
<div class="form-group">
{{ Form::submit('Sign up', ['class' => 'btn btn-primary']) }}
</div>
{{ Form::close() }}
Login form
{{ Form::open(['route' => 'login']) }}
<div class="form-group">
{{ Form::label('email', 'Email') }}
{{ Form::text('email', null, ['class' => 'form-control']) }}
{{ $errors->first('email', '<p class="error">:message</p>')}}
</div>
<div class="form-group">
{{ Form::label('password','Paswword') }}
{{ Form::password('password', ['class' => 'form-control']) }}
{{ $errors->first('password', '<p class="error">:message</p>')}}
</div>
<div class="form-group">
{{ Form::submit('Login', ['class' => 'btn btn-primary']) }}
</div>
{{ Form::close() }}
routes.php
Route::get('/signup', [
'as' => 'signup',
'uses' => 'UsersController#getSignup'
]);
Route::post('/signup', [
'as' => 'signup',
'uses' => 'UsersController#postSignup'
]);
Just wondering if anyone else has come across this issue and how to solve it.
Thanks

http://laravel.com/docs/4.2/validation#error-messages-and-views
Named Error Bags
If you have multiple forms on a single page, you may wish to name the MessageBag of errors. This will allow you to retrieve the error messages for a specific form. Simply pass a name as the second argument to withErrors:
return Redirect::to('register')->withErrors($validator, 'login');
You may then access the named MessageBag instance from the $errors variable:
<?php echo $errors->login->first('email'); ?>

Related

Neither it show an error nor save the Data into my database PHP Laravel?

I am trying to insert data into the database neither it shows an error nor saves the data into my database below are the codes. here is the create blade php page .
#extends('layouts.app')
#section('content')
<h1>Create a Camp</h1>
<!-- if there are creation errors, they will show here -->
{{ HTML::ul($errors->all()) }}
{{ Form::open(array('url' => 'camps')) }}
<div class="form-group">
{{ Form::label('name', 'Name') }}
{{ Form::text('name', Request::old('name'), array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('district', 'District') }}
{{ Form::text('district', Request::old('district'), array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('address', 'Address') }}
{{ Form::text('address', Request::old('address'), array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('block', 'Block') }}
{{ Form::text('block', Request::old('block'), array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('population', 'population') }}
{{ Form::text('population', Request::old('population'), array('class' => 'form-control')) }}
</div>
{{ Form::submit('Create the Camp!', array('class' => 'btn btn-primary')) }}
{{ Form::close() }}
#endsection
here is the controller the index controller works fine when I manually enter the data it fetches from the database
here is the store controller and also it not validate the forms data , i am new i dont know how to do it
public function store(Request $request)
{
$rules = array(
'name' => 'required',
'district'=> 'required',
'address' => 'required',
'block' => 'required|numeric',
'Population' => 'required|numeric',
);
$validator = Validator::make(Request::all(), $rules);
if ($validator->fails()) {
return Redirect::to('camp/create')
->withErrors($validator)
->withRequest(Request::except('password'));
} else {
// store
$camp = new Camp;
$camp->name = Request::get('name');
$camp->district = Request::get('district');
$camp->address = Request::get('address');
$camp->block = Request::get('block');
$camp->population = Request::get('population');
$camp->save();
// redirect
Session::flash('message', 'Successfully created Camp!');
return view('camp\camps',['camps'=>$camps]);
}
}
You forgot to add parenthesis after Camp it should be like this:
$camp = new Camp();
$camp->name = $request->name;
$camp->district = $request->district;
$camp->save();

Laravel update how to update data in index

The problem is i want to skip that edit page. it works as fine. but i wanna edit my data in index view.
i tried this but i took this error
{!! Form::model($choice, ['method' => 'PATCH','route' => ['choices.update', $choice->id]]) !!}
<input class="form-control" value="#foreach ($choices as $choice){{ $choice->question_number }}#endforeach" type="number" name="number"></input>
{!! Form::submit('Update Task', ['class' => 'btn btn-primary']) !!}
{!! Form::close() !!}
Trying to get property of non-object (View: C:\wamp64\www\zainsurgalt\resources\views\choices\index.blade.php)
index view
<td>Edit</td>
edit view
{!! Form::model($choice, ['method' => 'PATCH','route' => ['choices.update', $choice->id]]) !!}
<input class="form-control" type="number" name="number"></input>
{!! Form::submit('Update Task', ['class' => 'btn btn-primary']) !!}
{!! Form::close() !!}
controller update
public function update(Request $request,Choice $choice){
Choice::where('id', $choice->id)->update([
'question_number' => $request->input('number')
]);
return redirect()->route('choices.index');
}
All time when you get object property you must be check object exist or not
#if (!empty($duplicate->topic))
<td>Edit</td>
#endIf
also
#if (!empty($choice))
{!! Form::model($choice, ['method' => 'PATCH','route' => ['choices.update', $choice->id]]) !!}
<input class="form-control" type="number" name="number"></input>
{!! Form::submit('Update Task', ['class' => 'btn btn-primary']) !!}
{!! Form::close() !!}
#endIf
and finally you use
#foreach ($choices as $choice){{ $choice->question_number }}#endforeach
change $choice to another name for example $_choice , for not confusing above used $choice

Laravel edit children in parent view

I try to get children's in their parents view with ability of editing them,
Codes
controller
public function edit($id)
{
$option = Option::findOrFail($id);
return view('admin.options.edit', compact('option'));
}
blade
// getting parent info in edit blade
{{ Form::model($option, array('route' => array('options.update', $option->id), 'method' => 'PUT')) }}
{{ Form::label('title', 'Name') }}
{{ Form::text('title', null, array('class' => 'form-control')) }}
{{ Form::submit('Update', array('class' => 'btn btn-success')) }}
{{ Form::close() }}
// Geting this option subs (children's) in my second tab
#foreach($option->suboptions as $sub)
{{ Form::model($sub, array('route' => array('suboptions.update', $sub->id), 'method' => 'PUT')) }}
{{ Form::label('title', 'Name') }}
{{ Form::text('title', null, array('class' => 'form-control')) }}
{{ Form::label('price', 'Price') }}
{{ Form::text('price', null, array('class' => 'form-control')) }}
{{ Form::submit('Update', array('class' => 'btn btn-success')) }}
{{ Form::close() }}
#endforeach
Issue
As my sub's form is inside the loop I get update button for each row
(children) how can I only have 1 button?
UPDATE
check this video to see what I mean.
UPDATE 2
More explain!
my page is include 2 different forms (one of them edits my option
eg. size that's fine we no have problem with that)
my second form which is my issue will return subs of my option, eg.
option = size, subs = 12 inch, 15 inch etc.) in order to edit this subs currently i have 1 button for each of them (as you see in video i shared), what i want is to have only 1 button for all of my second form.
You can just open and close the form outside of the loop:
// Geting this option subs (children's) in my second tab
{{ Form::open(array('route' => ['suboptions.updateMany'], 'method' => 'PUT')) }}
#foreach($option->suboptions as $sub)
{{ Form::label('title', 'Name') }}
{{ Form::text('title[]', null, ['class' => 'form-control']) }}
{{ Form::label('price', 'Price') }}
{{ Form::text('price[]', null, ['class' => 'form-control']) }}
#endforeach
{{ Form::submit('Update', ['class' => 'btn btn-success']) }}
{{ Form::close() }}
You would then implement the updateMany controller method to loop over the submitted data (validate it) and submit it.
SOLVED
I've made controller below and I can update my data as I wanted:
public function subsupdate(Request $req)
{
for ($i=0; $i<count($req->id); $i++) {
DB::table('suboptions')
->where('id',$req->id[$i])
->update([
'title' => $req->title[$i],
'price' => $req->price[$i],
]);
}
return redirect()->back()->with('success', 'Suboptions updated successfully.');
}

Laravel Form Model Binding One to One Relationships Not Populated

Terms table:
term_id
name
slug
Term_taxonomy table:
term_taxonomy_id
term_id
description
My Term model:
public function TermTaxonomy(){
return $this->hasOne('TermTaxonomy');
}
My TermTaxonomy model:
public function Term(){
return $this->belongsTo('Term');
}
my Controller
public function edit($id)
{
$categories = Term::with(['TermTaxonomy' => function($q){
$q->select('term_id', 'description');
}])->get(['term_id', 'name', 'slug']);
$category = Term::with(['TermTaxonomy' => function($q){
$q->select('term_id', 'description');
}])->find($id, ['term_id', 'name', 'slug']);
return View::make('qhymchildz.backend.posts.categories', compact('category', 'categories'));
}
My View
#if (isset($category))
{{ Form::model($category, ['route' => ['admin_posts_categories_update', $category->term_id], 'method' => 'PATCH']) }}
#else
{{ Form::open(['route' => 'admin_posts_categories_store'])}}
#endif
{{ Form::label('name', 'Name') }}
<span data-tooltip aria-haspopup="true" class="has-tip radius" title="Category name for your posts.">
{{ Form::text('name', '', ['placeholder' => 'Category name here']) }}
#if ($errors->has('name')) <small class="error"> {{ $errors->first('name') }} </small> #endif
</span>
{{ Form::label('slug', 'Slug') }}
<span data-tooltip aria-haspopup="true" class="has-tip radius" title="Slug or URL for your category.">
{{ Form::text('slug', '', ['placeholder' => 'Slug here']) }}
#if ($errors->has('slug')) <small class="error"> {{ $errors->first('slug') }} </small> #endif
</span>
{{ Form::label('description', 'Description') }}
{{ Form::textarea('description', '', ['placeholder' => 'Category description here', 'size' => '50x5']) }}
#if (!isset($category))
{{ Form::submit('Add New Category', ['class' => 'radius button']) }}
#else
{{ Form::submit('Update', ['class' => 'radius button']) }}
#endif
{{ Form::close() }}
and all input text is not populated, already try many way from googling but nothing work, then how to populate data in my input text and text area ? i use it for edit function.
Thanks, i am new in laravel. any help will be appreaciated.
First, the second parameter has to be null so it will actually use the value from your model:
{{ Form::text('name', null, ['placeholder' => 'Category name here']) }}
To use a property from a related model you can use this:
{{ Form::textarea('TermTaxonomy[description]', null, ['placeholder' => 'Category description here', 'size' => '50x5']) }}
Note that there's no need to do an #if(isset($category)) at the beginning. The Form::model method will handle non-existent models on it's own. This is sufficient:
{{ Form::model($category, ['route' => ['admin_posts_categories_update', $category->term_id], 'method' => 'PATCH']) }}
In my case was not rendering the model value on the Textarea.
So instead of null i give the value like this:
{{ Form::textarea('TermTaxonomy[description]', $category->TermTaxonomy['description'], ['placeholder' => 'Category description here', 'size' => '50x5']) }}
Hope this also helps someone.
I also had the same problem, but I did it like this:
{!! Form::text('firstname', $user->profile->firstname, ['class' => 'form-control']) !!}
In my case:
User belongsTo('App\Profile'),
Profile hasOne('App\profile')
Just get an idea. Hope it helps!

Laravel Fill form with database saved fields

this below code is my profile managment form for users and i want to fill input tags with $profile variable .
{{ $profile }} can echo all fields of user table. but i can not fill input tags with this variable.
Result: {"id":"1","username":"mahdi","name":"Mahdi","family":"Pishguy","email":"name#gmail.com"}
My Form:
{{ Form::open(array('route' => 'admin.profile.update', $profile->id , 'method' => 'PUT','class'=>'navbar-form navbar-right', 'role' =>'search')) }}
<div class="form-group rtl">
<div>Your Username: <b> {{ $profile->username }} </b></div><br />
{{ Form::label('name' , 'name: ') }}
{{ Form::text('name', Input::old('name'), array('id'=>'email', 'class' => 'form-control')) }}
{{ Form::label('family' , 'family: ') }}
{{ Form::text('family', Input::old('family'), array('placeholder'=>'sss', 'id'=>'email', 'class' => 'form-control')) }}
<p>
{{ Form::submit('Submit', array('id'=>'submit','class'=>'btn btn-default' , 'style'=>'float:left')) }}
</p>
</div>
{{ Form::close() }}
Thats easy, see the Form Model Binding in the docs
You code should be something like this:
{{ Form::model($profile, array('route' => 'admin.profile.update', $profile->id , 'method' => 'PUT','class'=>'navbar-form navbar-right', 'role' =>'search')) }}
<div class="form-group rtl">
<div>Your Username: <b> {{ $profile->username }} </b></div><br />
{{ Form::label('name' , 'name: ') }}
{{ Form::text('name', Input::old('name'), array('id'=>'email', 'class' => 'form-control')) }}
{{ Form::label('family' , 'family: ') }}
{{ Form::text('family', Input::old('family'), array('placeholder'=>'sss', 'id'=>'email', 'class' => 'form-control')) }}
<p>
{{ Form::submit('Submit', array('id'=>'submit','class'=>'btn btn-default' , 'style'=>'float:left')) }}
</p>
</div>
{{ Form::close() }}
NOTE: You can remove the Input::old() stuf because this is handled by the Form::model()

Categories