I am developing a registration form. Everything is working fine but I am having problem with alpha and alpha_num validation.
Here is my Model(employee.php)
<?php
class Employee extends Eloquent
{
protected $table='employee';
public static $rules = array(
'first_name' => 'required|min:2',
'last_name' => 'required|min:2',
'username' => 'required|min:3|unique:employee',
'password' => 'required|alpha_num|between:6,12|confirmed',
'confirm_password' => 'required|aplha_num|between:6,12',
'department' => 'required',
'post' => 'required'
);
}
?>
Here is my controller (EmployeeController.php)
public function store()
{
//validate the user input
$validator = Validator::make(Input::all(),Employee::$rules);
//process the login
if ($validator->fails())
{
return Redirect::to('employee/create')
->withErrors($validator)
->withInput(Input::except('password'));
}
else
{
//store
$employee = new Employee;
$employee->first_name = Input::get('first_name');
$employee->last_name = Input::get('last_name');
$employee->username = Input::get('username');
$employee->password = Hash::make(Input::get('password'));
$employee->department = Input::get('department');
$employee->post = Input::get('post');
$employee->save();
//redirect to the main page
Session::flash('message','Successfully added employee');
return Redirect::to('employee');
}
}
My create.blade.php file
#extends('layouts.master')
#section('navigation')
#parent
#stop
#section('content')
<h1>Add New Employee</h1>
<!--if there are any errors,display here-->
{{ HTML::ul($errors->all()) }}
{{ Form::open(array('url'=>'employee','class'=>'form-horizontal')) }}
<div class="control-group">
{{ Form::label('first_name','Enter First Name',array('class'=>'control-label')) }}
<div class="controls">
{{ Form::text('first_name',Input::old('first_name'),array('placeholder'=>'First Name')) }}
</div>
</div>
<div class="control-group">
{{ Form::label('last_name','Enter Last Name',array('class'=>'control-label')) }}
<div class="controls">
{{ Form::text('last_name',Input::old('last_name'),array('placeholder'=>'Last Name')) }}
</div>
</div>
<div class="control-group">
{{ Form::label('username','Enter Username',array('class'=>'control-label')) }}
<div class="controls">
{{ Form::text('username',Input::old('username'),array('placeholder'=>'Username')) }}
</div>
</div>
<div class="control-group">
{{ Form::label('password','Enter Password',array('class'=>'control-label')) }}
<div class="controls">
{{ Form::password('password',array('placeholder'=>'Password')) }}
</div>
</div>
<div class="control-group">
{{ Form::label('confirm_password','Confirm Password',array('class'=>'control-label')) }}
<div class="controls">
{{ Form::password('confirm_password',array('placeholder'=>'Password')) }}
</div>
</div>
<div class="control-group">
{{ Form::label('department','Select Department',array('class'=>'control-label')) }}
<div class="controls">
{{ Form::select('department',array('it'=>'IT','finance'=>'finance','management'=>'Management','marketing'=>'Marketing')) }}
</div>
</div>
<div class="control-group">
{{ Form::label('post','Enter Post',array('class'=>'control-label')) }}
<div class="controls">
{{ Form::select('post',array('E'=>'Employee','PM'=>'Project Manager','CEO'=>'Executive CEO','MD'=>'Managing Director')) }}
</div>
</div>
<div class="control-group">
<div class="controls">
{{ Form::submit('Register',array('class'=>'btn btn-success')) }}
</div>
</div>
{{ Form::close() }}
#stop
I am getting BadMethodCallException
Method [validateAplhaNum] does not exist.
Method [validateAplha] does not exist.
It's because you've spelt alpha_num wrong in your confirm_password validation.
Related
I don't know why the page has to load twice to display comments.
Here is my route: Route::post('/addComment', 'CommentsController#addComment');
Here is my controller:
public function addComment(Request $request)
{
$this->validate($request, [
'name' => 'required',
'body' => 'required',
]);
$lesson_id = $request->lesson_id;
$comment = new Comment;
$comment->name = $request->input('name');
$comment->body = $request->input('body');
$comment->parrent_id = '0';
$comment->lesson_id = $request->lesson_id;
$comment->save();
return back();
}
Here is my view:
<div class="leave_review">
<h3 class="blog_heading_border"> コメント </h3>
{!! Form::open(['action' => ['CommentsController#addComment'], 'method' => 'POST', 'id' => 'postForm' ]) !!}
<input type="hidden" name="_token" value="{{ csrf_token() }}" />
<input type="hidden" id ="lesson_id" name="lesson_id" value="{{$lesson->id}}" />
</div>
<div class="row">
<div class="col-sm-6">
#error('name')
<div class="alert alert-danger">{{ $message }}</div>
#enderror
{{Form::label('name','名前')}}
{{Form::text('name', '', ['class' => 'form-group', 'id' => 'name' ]) }}
</div>
<div class="col-sm-12">
#error('body')
<div class="alert alert-danger">{{ $message }}</div>
#enderror
{{Form::label('body','メッセージ')}}
{{Form::textarea('body', '', ['class' => 'form-group', 'id' => 'body']) }}
</div>
</div>
<div class="row">
<div class="col-md-12">
</div>
</div>
{{Form::submit('Submit', ['class' => 'send mt_btn_yellow pull-right', 'id' => 'submit'])}}
{!! Form::close() !!}
{{-- End add comment --}}
{{--Display comment--}}
<ol class="review-lists">
#foreach ($comment as $value)
<li class="comment">
<div class="activity_rounded">
<img src="/storage/icon/icon.jpg" alt="image"> </div>
<div class="comment-body">
<h4 class="text-left">{{$value->name}}
<small class="date-posted pull-right">{{ \Carbon\Carbon::parse($value->created_at)->diffForHumans() }}</small>
</h4>
<p>{{$value->body}} </p>
<button class="pull-left mt_btn_yellow" onclick="toggleReply('{{$value->id}}')">返事</button>
{{-- ENd Display comment--}}
#foreach ($comment as $value)
<li class="comment">
<div class="activity_rounded">
<img src="/storage/icon/icon.jpg" alt="image"> </div>
<div class="comment-body">
<h4 class="text-left">{{$value->name}}
<small class="date-posted pull-right">{{ \Carbon\Carbon::parse($value->created_at)->diffForHumans() }}</small>
</h4>
<p>{{$value->body}} </p>
<button class="pull-left mt_btn_yellow" onclick="toggleReply('{{$value->id}}')">返事</button>
{{-- ENd Display comment--}}
you don't have a #endforeach
Laravel required if validation issue
Blade:
{{ Form::open(['route' => ['updateEmailSettings']]) }}
<div class="form-group row">
{{ Form::label('driver','Mail Driver',['class' => 'col-md-3 col-form-label required']) }}
<div class="col-md-9">
{{ Form::select('driver',$drivers, null,['class' => 'form-control', 'placeholder' => 'Select']) }}
</div>
</div>
<div class="form-group row">
<label class="col-md-3 col-form-label font-weight-bold">Mandrill</label>
</div>
<div class="form-group row">
{{ Form::label('mailgun_secret','Secret',['class' => 'col-md-3 col-form-label']) }}
<div class="col-md-9">
{{ Form::text('mailgun["secret"]',null,['class' => 'form-control', 'id' => 'mailgun_secret']) }}
</div>
</div>
<div class="form-group row">
<div class="col-md-9 ml-md-auto">
{{ Form::button('<i class="far fa-save"></i> Save',['class'=>'btn btn-primary mr-3','type'=>'submit']) }}
<a class="btn btn-danger" href="{{ route('emailSettings') }}"><i class="far fa-times-circle"></i> Cancel</a>
</div>
</div>
{{ Form::close() }}
Form Request:
return [
'driver' => 'required',
'mailgun.*.domain' => 'required_if:driver,mailgun'
];
Validation always fails. Please suggest me if i miss anything.
Resolved myself
Blade: Removed double quotes inside the bracket.
{{ Form::text('mailgun[secret]',null,['class' => 'form-control', 'id' => 'mailgun_secret']) }}
Form Request: Removed asterisk
'mailgun.domain' => 'required_if:driver,mailgun'
I developed admin panel to add,edit,delete users in my website with Restfull Api. edit and delete work fine but add not.
when I add user it may work or this error may appear
The localhost page isn’t working
localhost is currently unable to handle this request.
This is my routes
Route::resource('admin/users','AdminUser');
This is store function in Admin user resource
public function store(Request $request)
{
//rules
$rules = array(
'name' => 'required',
'email' => 'required|email',
'password' => 'required|min:6|confirmed',
'password_confirmation' => 'required|min:6'
);
/*
validate the data user
**/
$validator = Validator::make($request->all(),$rules);
if ($validator->fails()) {
return view('admin.User.create_user')
->withErrors($validator)
->withInput(['page' => 'home']);
}
/*
Store the data user in the database
**/
$user = new User;
$user->name =$request->input('name');
$user->email = $request->input('email');
$user->password=bcrypt($request->input('password'));
$user->role='user';
$user->save(); //error here
//redirect
return redirect('admin/users')->with('message', 'Successfully added user!');
}
And this is create_user.blade.php
#extends('layouts.layout')
#section('content')
<section id="advertisement">
<div class="container">
<img src="{{asset('images/shop/advertisement.jpg')}}" alt="" />
</div>
</section>
<section>
<div class="container">
<div class="row">
<div class="col-sm-3">
<div class="left-sidebar">
#include('shared.sidebaradmin')
</div>
</div>
<div class="features_items"><!--features_items-->
<h2 class="title text-center">Add New Product</h2>
#if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
{{ Form::open(array('url' => 'admin/users')) }}
<div class="form-group">
{{ Form::label('name', 'Name') }}
{{ Form::text('name',null, array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('email', 'Email') }}
{{ Form::email('email',null, array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('password', 'Password') }}
{{ Form::password('password', array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('password_confirmation', 'Confirm Password') }}
{{ Form::password('password_confirmation', array('class' => 'form-control')) }}
</div>
{{ Form::submit('Add a new user !', array('class' => 'btn btn-primary')) }}
{{ Form::close() }}
</div><!--features_items-->
</div>
</div>
</section>
#endsection
UserController.php
public function edit_profile(){
$input = Input::all();
$res = array();
$_m = "";
$rules = array(
'userName' => 'required',
'fullName' => 'required',
'shopName' => 'required',
'userType' => 'required',
'email' => 'required',
'aboutMe' =>'required',
'address' =>'required',
'city' =>'required',
'country' =>'required',
'contactNo' =>'required|regex:/[0-9]{10,11}/',
);
$validator = Validator::make($input, $rules);
if (!$validator->fails())
{
$user = User::find(Auth::user()->get()->id);
$user->fullname = $input('fullName');
$user->username = $input('userName');
$user->businessname = $input('shopName');
$user->usertype = $input('userType');
$user->email = $input('email');
$user->aboutme = $input('aboutMe');
$user->address = $input('address');
$user->city = $input('city');
$user->country = $input('country');
$user->phone = $input('contactNo');
$user->save();
$res['success'] = true;
$res['message'] = "NO";
return Redirect::to('user.profile');
}
else
{
return Redirect::to('login');
}
}
profile.blade.php
<div class="tab-pane fade" id="p2">
<h2>Account details</h2>
<div class="hr hr-12 hr-double"></div>
{{ Form::open(array('url'=>'profile','class' =>'form-horizontal')) }}
<div class="form-group">
<label class="col-sm-3 control-label">Full Name:</label>
<div class="col-sm-3">
{{ Form::text('fullName',ucwords(Auth::user()->fullname),array('class'=>'form-control', 'placeholder'=> 'Full Name Here' )) }}
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">User Name:</label>
<div class="col-sm-3">
{{ Form::text('userName',ucwords(Auth::user()->username),array('class'=>'form-control', 'placeholder'=>'e.g ali.ali')) }}
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Shop Name:</label>
<div class="col-sm-5">
{{ Form::text('shopName',ucwords(Auth::user()->businessname),array('class'=>'form-control', 'placeholder'=>'e.g Niazi Traders')) }}
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Email</label>
<div class="col-sm-4">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-envelope"></i></span>
{{ Form::email('email',ucwords(Auth::user()->email),array('class'=>'form-control', 'placeholder'=>'e.g john.smith#example.com')) }}
</div>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">User Type:</label>
<div class="col-sm-4">
<div class="input-group">
<span class="input-group-addon"></span>
{{ Form::select('userType',array('D'=>'Distributor', 'W'=>'Whole Saler','R' =>'Retailer'),ucwords(Auth::user()->usertype)) }}
</div>
</div>
</div>
<hr class="separator">
<div class="form-group">
<label class="col-sm-3 control-label">About Me:</label>
<div class="col-sm-9">
{{ Form::textarea('aboutMe',ucwords(Auth::user()->aboutme),array('class'=>'form-control','id'=>'about-editor','placeholder'=>'Some thing about yourself......')) }}
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Address:</label>
<div class="col-sm-7">
{{ Form::text('address',ucwords(Auth::user()->address),array('class'=>'form-control','placeholder'=>'')) }}
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">City:</label>
<div class="col-sm-3">
{{ Form::text('city',ucwords(Auth::user()->city),array('class'=>'form-control','placeholder'=>'e.g Islamabad')) }}
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Country:</label>
<div class="col-sm-4">
{{ Form::text('country',ucwords(Auth::user()->country),array('class'=>'form-control','placeholder'=>'e.g Pakistan')) }}
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Phone Number:</label>
<div class="col-sm-3">
{{ Form::text('contactNo',ucwords(Auth::user()->phone),array('class'=>'form-control','placeholder'=>'e.g 0300xxxxxx')) }}
</div>
</div>
<div class="form-actions">
<div class="form-group">
<div class="col-sm-offset-3 col-sm-9">
{{ Form::submit('Submit',array('class'=>'btn btn-primary')) }}
{{ Form::submit('Cancel',array('class'=>'btn btn-inverse')) }}
</div>
</div>
</div>
{{ Form::close() }}
</div>
Routes.php
Route::get('logout','UserController#get_logout');
Route::get('profile', 'UserController#get_profile');
Route::post('profile', 'UserController#edit_profile');
User.php
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'users';
/**
* The attributes excluded from the model's JSON form.
*
* #var array
*/
protected $hidden = array('password', 'remember_token');
}
Error lies in this line
$user = User::find(Auth::user()->get()->id);
get is a function of Builder class used for getting a collection of record, Auth::user() returns a single user so you just need to access its id property
change above line to
$user = User::find(Auth::user()->id);
but Auth::user you give the current user so you dont need to find it again, you can access its property directly
$user = Auth::user();
I've created a task list which displays several tasks. I can create lists in my page and the task itself, but, I'm receiving an error on the constraint as it has to come with the ID of the task that it's being created.
Currently the task is created under a loop in the view on laravel 4 (tasks/index.blade.php).
#foreach ($tasks_lists as $task_list)
<div class="col-lg-3 col-md-4 container_tasks">
<h4>{{ $task_list->title }} <small>(Nueva tarea)</small><button class="close" type="button" data-toggle="modal" data-target=".delete-task-list-modal{{ $task_list->id }}">×</button></h4>
<ul>
#foreach ($task_list->tasks as $task)
<li>
{{ Form::open(array('url' => 'tareas.marcar', 'class' => 'createTaskList')) }}
<input
type="checkbox"
onclick="this.form.submit()"`
{{ $task->done ? 'checked' : '' }}
/>
<input type="hidden" name="id" value="{{ $task->id }}" />
{{ $task->name }}
{{ Form::close() }}
</li>
#endforeach
</ul>
</div>
<div class="modal fade create-task-modal{{ $task_list->id }}" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true" >
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Crear lista de tareas</h4>
</div>
<div class="modal-body">
{{ Form::open(array('url' => 'tareas', 'class' => 'createTask')) }}
<div class="form-group">
{{ Form::label('name', 'Nombre') }}
{{ Form::text('name', Input::old('name'), array('class' => 'form-control')) }}
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cerrar</button>
{{ Form::submit('Crear tarea', array('class' => 'btn btn-primary')) }}
{{ Form::close() }}
</div>
</div>
</div>
</div>
And this is my controller (TaskController.php):
public function store()
{
$task_list =
// Reglas de validación
$rules = array(
'name' => 'required|min:3|max:30'
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
return Redirect::to('tareas')
->withErrors($validator)
->with('notice_type', 'danger')
->with('notice', 'Los siguientes errores ocurrieron:')
->withInput();
} else {
// store
$task = new Task;
$task->name = Input::get('name');
$task->save();
return Redirect::to('tareas')
->with('notice_type', 'success')
->with('notice', 'La tarea ha sido agregado satisfactoriamente.');
}
}
How would I go about adding the ID of the current task list when creating the task, so that the task is added properly to its list when clicking the .create-task-modal
Lucus
The easist way would be to add a hidden field after
{{ Form::open(array('url' => 'tareas', 'class' => 'createTask')) }}
so it's like
{{ Form::open(array('url' => 'tareas', 'class' => 'createTask')) }}
{{ Form::hidden('task_list_id', $task_list->id) }}
then, in your controller do
$rules = array(
'task_list_id' => 'required|exists:task_lists,id', //check the docs for exists
'name' => 'required|min:3|max:30'
);
...
// option 1
$task = new Task;
$task->task_list_id = Input::get('task_list_id');
$task->name = Input::get('name');
$task->save();
// option 2 (depending on your fillable
$task = new Task(Input::only('task_list_id', 'name');
$task->save();