laravel post request validation dont view error( validation Fail) - php

i try to set rule to validate post request for laravel form to store data but if i set any validation rule ( either pass or fail ) it will show just blank empty page .
i using laravel 5
the form ( view ):
#if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
{!! Form::open(['action' => 'RentalAgreementController#store', 'method' => 'post']) !!}
{!! Form::label('dateOfAgreement', 'Date of Agreement', ['class' => 'control-label']) !!}
{!! Form::input('date', 'dateOfAgreement' , date('Y-m-d') , ['class' => 'form-control']) !!}
{!! Form::submit('Submit', ['class' => 'button']) !!}
{!! Form::close() !!}
Controller
public function store(StoreAgreementPostRequest $request)
{
$agreement= new RentalAgreement(array(
'dateOfAgreement' => $request->dateOfAgreement,
));
$agreement->save();
Session::flash('flash_message', 'Agreement successfully added! ');
return view('home');
}
StoreAgreementPostRequest
class StoreAgreementPostRequest extends Request {
public function authorize()
{
return true;
}
public function rules()
{
return [
'premiseUse'=>'required|max:5'
];
}
}
just shows the black page but if i remove the rules its works . but for all other forms in this application validation way works ,( all other forms and controllers works perfectly).

Related

Toggle database value from View on button click Laravel

I am using Laravel framework as a backend API and a few blade PHP files for the front end, specifically for the authentication and the admin panel from the /admin route.
In /admin, I display a list of all registered users and buttons next to them. (This page is only visible for users that have their value in Admin column set as true). I want to toggle the Admin status of a user, either promoting or demoting them by clicking the button next to the user name.
For this, I tried to use a form submit with get method.
I have a method defined inside UserController like this:
public function setAdmin($id) {
$user = User::find($id);
$user->admin = !$user->admin;
if($user->save()) {
echo "Changed";
}
else {
echo "Could not be changed";
}
}
I want to call this method from the view on the click of a button.
I tried using a Form to send a request by specifying the action, but it gave an error saying the values passed are less than the expected number of parameters.
{!! Form::open(['action' => ['UserController#setAdmin', $user->id], 'method' => 'POST']) !!}
{{ Form::submit('Submit', ['class' => 'btn btn-primary']) }}
{!! Form::close() !!}
I have a route set up explicitly to call this action
Route::post('/admin/users/setAdmin', 'UserController#setAdmin')
Although I am not sure if I have to set an explicit route for this action or if it's possible to call a controller function directly from a view without defining the route.
I have iterated through User Model to display all users:
#if(count($users) > 0)
#foreach($users as $user)
<div class="card">
{{ $user }}
</div>
{!! Form::open(['action' => ['UserController#setAdmin', $user->id], 'method' => 'POST']) !!}
{{ Form::submit('Submit', ['class' => 'btn btn-primary']) }}
{!! Form::close() !!}
#endforeach
#else
<h2>No users found!</h2>
#endif
EDIT: Added the foreach section of the blade file. Also I modified the 'action' part of the Form::open() parameters, it was a mistype, the parameters error is still there.
Can someone explain how this can be done?
You are trying to pass a parameter to your route but there is any within its declaration. You need to add it in your route path:
Route::post('/admin/users/setAdmin/{id}', 'UserController#setAdmin')
If you don't want to have an URL like this, you should add a hidden input to your form containing your ID:
{!! Form::open(['action' => ['UserController#setAdmin'], 'method' => 'POST']) !!}
{{ Form::hidden('id', $user->id) }}
{{ Form::submit('Submit', ['class' => 'btn btn-primary']) }}
{!! Form::close() !!}
And in your controller's method:
use Request;
/* ... */
public function setAdmin(Request $request) {
$user = User::find($request->id);
/* ... */
}

Contact Form Laravel 5.4

I'm trying to build contact form in Laravel 5.4. I'm almost succeeded but besides actual message, on my mail i'm getting the structure of the page (look screenshot). Can you help me with that?
enter image description here
my View Form:
<div class="row">
{{ Form:: open(array('action' => 'ContactController#getContactUsForm')) }}
<ul class="errors">
#foreach($errors->all('<li>:message</li>') as $message)
{{ $message }}
#endforeach
</ul>
<div class="form-group">
{{ Form:: textarea ('message', '', array('placeholder' => 'Message', 'class' => 'form-control', 'id' => 'message', 'rows' => '7' )) }}
</div>
<div class="modal-footer">
{{ Form::submit('Submit', array('class' => 'btn btn-primary')) }}
{{ Form:: close() }}
</div>
</div>
And my Controller:
namespace App\Http\Controllers;
use Input;
use Illuminate\Http\Request;
use Validator;
use Mail;
use Redirect;
class ContactController extends Controller
{
public function getContactUsForm(Request $request){
//Get all the data and store it inside Store Varible
$data = \Input::all();
//$data = $request->message;
//$data = $request->input('message');
//Validation rules
$rules = array (
//'first_name' => 'required', uncomment if you want to grab this field
//'email' => 'required|email', uncomment if you want to grab this field
'message' => 'required|min:5'
);
//Validate data
$validator = Validator::make ($data, $rules);
//If everything is correct than run passes.
if ($validator -> passes()){
Mail::send('support/contact', $data, function($message) use ($data)
{
//$message->from($data['email'] , $data['first_name']); uncomment if using first name and email fields
$message->from('masha#mail.com', 'contact form');
//email 'To' field: cahnge this to emails that you want to be notified.
$message->to('masha#mail.com', 'Masha')->subject('Contact Form');
});
// Redirect to page
return Redirect::route('contact')
->with('message', 'Your message has been sent. Thank You!');
//return View::make('contact');
}else{
//return contact form with errors
return Redirect::route('contact')
->with('error', 'Feedback must contain more than 5 characters. Try Again.');
}
}
}
change this:
<div class="modal-footer">
{{ Form::submit('Submit', array('class' => 'btn btn-primary')) }}
{{ Form:: close() }}
</div>
To this:
<div class="modal-footer">
{{ Form::submit('Submit', array('class' => 'btn btn-primary')) }}
</div>
{{ Form:: close() }}
I think ordering break the structure.

two or more forms in one view using one post request

I have only two forms for the moment in my view, they are both post method I tried to solve it like this
route
Route::post('view', function(){
if(Input::has('form1')){
'NameController#method1';
} elseif (Input::has('form2')){
'NameController#method2';
}
});
view
{!! Form::open(array('url' => '/view')) !!}
{!! Form::text('text', $trans->text) !!}
{!! Form::submit('Submit', array('name' => 'form1' )) !!}
{!! Form::close() !!}
{!! Form::open(array('url' => '/view')) !!}
{!! Form::text('text', 'text') !!}
{!! Form::submit('Submit', array('name' => 'form2')) !!}
{!! Form::close() !!}
And it throw's out this error
syntax error, unexpected ''ConfigurationController#title' (T_CONSTANT_ENCAPSED_STRING)
it was error in coding I corrected it but it won't do what I wish it simply returns blank screen it doesn't cycle trough controllers
I modified the code(removed return and closed the route)
What you want to do is create a method someMethodName in the NameController and in there
public function someMethodName()
{
if(Input::has('form1')){
$this->method1();
} elseif (Input::has('form2')){
$this->method2();
}
}
then replace all the route stuff with
Route::post('view', 'NameController#someMethodName')

Laravel form error message

I created a form for myself and it was working perfect. I did some rules like: if the user haven't wrote anything in the form or haven't wrote enough, give me some error message. This was working while used the 'former' package instead of the normally used 'form' package. Now I changed my form from "Former" to "Form" and now my error message are gone.. Well my form works and the rules too. If I don't write anything in my form or not at least 3 characters in the title/content section, it should redirect me with the errors I need. This works, but without the error message.
Here is my form:
#extends('master')
#section('content')
{!! Form::open(array('action' => 'Test\\TestController#store')) !!}
<div class="form-group">
{!! Form::label('thread', 'Title:') !!}
{!! Form::text('thread', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('content', 'Body:') !!}
{!! Form::textarea('content', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::submit('Add Thread', ['class' => 'btn btn-primary form-control']) !!}
</div>
{!! Form::close() !!}
#stop
My Controller#store function:
public function store(StoreRequest $request){
Thread::create($request->all());
return redirect(action('Test\\TestController#index'));
}
my Model:
<?php
namespace App\Models\Thread;
use Illuminate\Database\Eloquent\Model;
class Thread extends Model {
public $table = 'thread';
public $fillable = [
'thread',
'content',
];
}
and now the rules ( StoreRequest.php ) :
<?php
namespace App\Http\Requests\Store;
use App\Http\Requests\Request;
class StoreRequest extends Request {
public function rules() {
return [
'thread' => 'required|min:3',
'content' => 'required|min:3'
];
}
public function authorize() {
return true;
}
}
The error message was getting automaticly generated. Laravel did that for me. But not anymore.
Does anybody see why?
You can check if the form returned any error with something along these line:
#if ($errors->has('name')) <p class="help-block">{{ $errors->first('name') }}</p> #endif
Or you could use BootForm and let it handle everything for you

Laravel change password right after email confirmation view not working

When a user is created a random password (and a auth code) will be created and send with an email to the user.
When clicked on the link in the email the user status will be 1 (so active) and the user will be able to change his password right away.
Now it doesn't work as I want to.
UserController:
public function store(CreateUserRequest $request, User $user, Attribute $attribute)
// some unnecessary code
if ((Input::get('usertype_id')) > 1) {
$randomPassword = str_random(8);
$user->password = Hash::make($randomPassword);
$authentication_code = str_random(12);
$user->authentication_code = $authentication_code;
$user->active = 0;
};
$user->save();
if ((Input::get('usertype_id')) > 1) {
// Email sturen met verficatie code
$email = Input::get('email');
Mail::send('emails.user', ['user' => $user, 'password' => $randomPassword, 'authentication_code' => $authentication_code], function ($message) use ($email) {
$message->to($email, 'Lilopel')->subject('Lilopel: Verify your account!');
});
};
public function confirmUser($authentication_code)
{
if (!$authentication_code)
{
return 'auth code not found!';
}
$user = User::where('authentication_code', '=', $authentication_code)->first();
if (!$user)
{
return 'user not found!';
}
$user->active = 1;
$user->save();
Session::put('user_id', $user->id);
return view('user.setpassword', ['user' => $user]);
//return redirect()->route('user.setPassword', [$user_id]);
}
public function setPassword(SetPasswordRequest $request)
{
$user_id = Session::get('user_id');
$user = $this->user->find($user_id);
$user->fill($request->only('password'));
$user->save();
}
Route:
Route::get('user/verify/{authenticationCode}', 'UserController#confirmUser');
Route::get('user/password', 'UserController#setPassword');
View:
{!! Form::model($user, ["route"=>['user.setPassword', $user->id] , "method" => 'PATCH']) !!}
<div class="form-group {{ $errors->has('password') ? 'has-error' : '' }}">
{!! Form::label('password', trans('common.password'), ['class' => 'form-label col-sm-3 control-label
text-capitalize']) !!}
<div class="col-sm-6">
{!! Form::password('password', ['name' => 'password', "class"=>"form-control","placeholder" =>
trans('common.password') ]) !!}
{!! $errors->first('password', '<span class="help-block">:message</span>') !!}
</div>
</div>
<div class="form-group {{ $errors->has('confirm_password') ? 'has-error' : '' }}">
{!! Form::label('password_confirmation', trans('common.confirmpassword'), ['class' => 'form-label
col-sm-3 control-label text-capitalize']) !!}
<div class="col-sm-6">
{!! Form::password('password_confirmation', ['name' => 'password_confirmation',
"class"=>"form-control","placeholder" => trans('common.confirmpassword') ]) !!}
{!! $errors->first('password_confirmation', '<span class="help-block">:message</span>') !!}
</div>
</div>
{!! Form::submit( trans('common.edit'), ["class"=>"btn btn-primary text-capitalize center-block "]) !!}
{!! Form::close() !!}
Email links works, the status of the user gets active, but then the .blade will give a Route [user.setPassword] not defined. (View: public_html/server2/resources/views/user/setpassword.blade.php) error.
work togetherTo use the route as you do, you need a named route.
Change this
Route::get('user/password', 'UserController#setPassword');
to this
Route::get('user/password', [
'as' => 'user.setPassword',
'uses' => 'UserController#showProfile'
]);
Also, make sure the HTTP verbs of the route and your form's method work together.

Categories