I am trying to make back-end validation for my form however it does not work. It always shows that validation failed even when I submitted the values.
Code
$validation = Validator::make(Input::all(), array(
array('email' => 'required'),
array('password' => 'required')
));
if ($validation->fails()) { // This is always failing
echo '<pre>';
print_r(Input::all());
die();
}
die('everything is OK!');
Output
Array
(
[_token] => ZnzZ2aDoTABIZZkvwxZoa7IjHkvK25ndibis5AbA
[email] => somedata
[password] => somemoredata
)
As you can see it failed even tho it clearly showed from Input:all() that values are set.
Try this :
$validation = Validator::make(Input::all(), array(
'email' => 'required',
'password' => 'required'
));
Not double array as you did :
$validation = Validator::make(Input::all(), array(
array('email' => 'required'), // Is shouldn't be an array here.
array('password' => 'required')
));
Change:
$validation = Validator::make(Input::all(), array(
array('email' => 'required'),
array('password' => 'required')
));
to
$validation = Validator::make(Input::all(), array(
'email' => 'required|email', // Note that I added in valid email rule here also
'password' => 'required'
));
Related
I'm trying to figure out how to pass back to my ajax call that there were validation errors if there is so that I can prevent the page from continuing on and display those errors to the user.
/*
* Create User After they complete the first part of the form.
*
*/
public function createUserAndOrder(Request $request)
{
$validation = $this->validate($request, [
'first_name' => 'required',
'last_name' => 'required',
'email' => 'required|confirmed|unique:users,email',
'email_confirmation' => 'required'
]);
$credentials = [
'first_name' => $request->input('first_name'),
'last_name' => $request->input('last_name'),
'email' => $request->input('email'),
'password' => Hash::make(str_random(8)),
];
$user = Sentinel::registerAndActivate($credentials);
$user->role()->attach(5);
return response()->json([
'success' => true,
'errors' => null
]);
}
You can try it by using Validate Facade as:
$validator = \Validator::make($request->all(), [
'first_name' => 'required',
'last_name' => 'required',
'email' => 'required|confirmed|unique:users,email',
'email_confirmation' => 'required'
]);
// Validate the input and return correct response
if ($validator->fails())
{
return response()->json(array(
'success' => false,
'errors' => $validator->getMessageBag()->toArray()
), 422);
}
This would give you a JSON response like this:
{
'success': false,
'errors': {
'first_name': [
'The first name field is required.'
],
'last_name': [
'The last name field is required.'
]
}
}
How i set custom the attributte names in laravel 5.2 I already try this code, but doesn't work:
$attNames = array(
'code' => 'Número',
'contributor' => 'Nº Contribuinte',
'create_date' => 'Data criação',
'address' => 'Morada',
'zip_code' => 'Cod. Postal',
'city' => 'Localidade',
'email' => 'E-mail',
'phone_number' => 'Telefone',
'note' => 'Observações',
);
$validator = Validator::make($client, $this->rules,[],$attNames);
$validator->setAttributeNames($attNames);
if ($validator->fails()) {
// send back to the page with the input data and errors
$errors = $validator->messages();
return Redirect::to('/client/create')->withInput()->withErrors($errors);
}
You have passed wrong arguments to Validator::make.
You can pass only three arguments.
As per Documentation,
If needed, you may use custom error messages for validation instead of
the defaults. There are several ways to specify custom messages.
First, you may pass the custom messages as the third argument to the
Validator::make method.
$messages = [
'required' => 'The :attribute field is required.',
];
$validator = Validator::make($input, $rules, $messages);
I figure out.
controller:
use Validator;
(...)
$attName=array(
'code' => trans('validation.code'),
'contributor' => trans('validation.contributor'),
'create_date' => trans('validation.create_date'),
'address' => trans('validation.address'),
'zip_code' => trans('validation.zip_code'),
'city' => trans('validation.city'),
'email' => trans('validation.email'),
'phone_number' => trans('validation.phone_number'),
'note' => trans('validation.note'),
);
$validator = Validator::make($client, $this->rules, [], $attNames);
validation.php:
'attributes' => [
'code' => 'número',
'contributor' => 'nº contribuinte',
'create_date' => 'data criação',
'address' => 'morada',
'zip_code' => 'cod. postal',
'city' => 'localidade',
'email' => 'e-mail',
'phone_number' => 'telefone',
'note' => 'observações',
],
I have a very strange bug.
Running Laravel 4.1 as my company hasn't upgraded their PHP version yet.
The routes file has this:
Route::controller('survey', 'SurveyController');
And, when you go to /survey/new-survey it ends up being sent to postNewQuestion function instead of postNewSurvey.
public function postNewSurvey() {
$input = Input::all();
//dd($input);
$rules = array(
'name' => 'required|unique:lime_surveys_languagesettings,surveyls_title',
'language' => 'required'
);
...... etc
}
public function postNewQuestion() {
$input = Input::all();
//dd($input);
$rules = array(
'sid' => 'required',
'gid' => 'required',
'type' => 'required',
'question' => 'required',
'mandatory' => 'required',
);
$validator = Validator::make($input, $rules);
if($validator->fails()) {
return Response::json(array('success' => false, 'error' => 'validation',
'messages' => $validator->messages(), 'failed' => $validator->failed()));
}
......... etc
}
Thanks.
I have a page setup to reset the user's email address password and the users zip_code for an application. I have it working correctly however, if for example I want to just change the zip code my validation will not allow it due to it reading the email as already existing in the database and returns an error. What is the best way around this? I could create a controller for each field and do it individually but I feel there is some if statement I could use maybe?
Controller:
enter
public function getEditUser() {
$userdata = array(
'email' => Input::get('email'),
'password' => Input::get('password'),
'confirm_password' => Input::get('confirm_password'),
'user_zip_code' => Input::get('user_zip_code')
);
$rules = array(
'email' => 'required|email|unique:users,email',
'password' => 'required|min:5',
'confirm_password' => 'required|same:password',
'user_zip_code' => 'required'
);
$validation = Validator::make($userdata, $rules);
if ($validation->fails()) {
return Redirect::to('dashboard/edit/account')->withErrors($validation)
->withInput();
}
$userdata['password'] = Hash::make($userdata['password']);
$userdata['confirm_password'] = Hash::make($userdata['confirm_password']);
User::find(Auth::user()->id)->update($userdata);
return Redirect::to('dashboard');
}
Unique needs to be in place obviously so it doesn't get matched with another account
Also my email is set through Auth..Auth::user()->email if that helps
You can set the validation unique rule to ignore the current id
$rules = array(
'email' => 'required|email|unique:users,email,'.Auth::user()->id,
'password' => 'required|min:5',
'confirm_password' => 'required|same:password',
'user_zip_code' => 'required'
);
See here for more info from the docs
I am using the below line to check my add user form to check if the email address does not exist in my database I have a feeling that it is working fine but I am unable to get any error messages.
$this->form_validation->set_rules('userEmail','E-Mail', 'required|valid_email|trim|max_length[99]|xss_clean|is_unique[users.email]');
In my view I have <?php echo validation_errors(); ?>
My controller is formatted as per below:
if($this->form_validation->run() === TRUE)
{
$userData = array(
'fName' => $this->input->post('userFirstName', TRUE),
'lName' => $this->input->post('userLastName', TRUE),
'email' => $this->input->post('userEmail', TRUE),
'password' => sha1($this->input->post('userPassword', TRUE))
);
$this->db->escape($userData);
$this->user_model->addUser($userData);
}
I changed my controller to the following:
$this->form_validation->set_rules('userEmail','E-Mail', 'required|valid_email|trim|max_length[99]|xss_clean|is_unique[users.email]');
$this->form_validation->set_rules('userPassword','Password', 'required|trim|max_length[99]|xss_clean');
if($this->form_validation->run() === TRUE)
{
$userData = array(
'fName' => $this->input->post('userFirstName', TRUE),
'lName' => $this->input->post('userLastName', TRUE),
'email' => $this->input->post('userEmail', TRUE),
'password' => sha1($this->input->post('userPassword', TRUE))
);
$this->db->escape($userData);
$this->user_model->addUser($userData);
}
$data['contentMangement'] = $this->options_model->systemOptions();
$data['pageTitle'] = 'Add User';
$this->load->view('_assets/header', $data);
$this->load->view('addUser', $data);
$this->load->view('_assets/footer');
}
Just use the is_unique predefined validation rule. Pass the table name and column name for the email column.
$this->form_validation->set_rules('userEmail','E-Mail', 'required|valid_email|trim|max_length[99]|xss_clean|is_unique[users.email]');
we must add to table name for is_unique
in my way for exp.
array(
'field' => 'email',
'label' => 'email',
'rules' => 'trim|required|valid_email|is_unique[users.email]',
'errors' => array('required' => 'Please enter a %s.','valid_email' => 'Please enter a valid %s.','is_unique' => 'email already exist.')
),
hope you under stand.