Call to undefined method CI_Form_validation::substr() - codeigniter

When i try to update user record, i get this error "Call to undefined method CI_Form_validation::substr()". As far as i investigate form validation return error for email.
if($this->input->post('stu_emailid')){
$this->form_validation->set_rules('stu_emailid', 'Email', 'trim|required|valid_email|edit_unique[users.user_email.user_id.' . $student_user_id . ']|edit_unique[student.stu_emailid.student_id.' . $user_id . ']',
array(
'required' => 'You have not provided %s.',
'is_unique' => 'This %s already exists.'
));
}

edit_unique is not a Rule for CI form validation, it should be is_unique
if($this->input->post('stu_emailid')){
$this->form_validation->set_rules('stu_emailid', 'Email', 'trim|required|valid_email|is_unique[users.user_email.user_id.' . $student_user_id . ']|is_unique[student.stu_emailid.student_id.' . $user_id . ']',
array(
'required' => 'You have not provided %s.',
'is_unique' => 'This %s already exists.'
));

Related

Laravel 8 \Stripe\Exception\CardException when entered card declined not working

I am using Laravel Cashier but this time I am calling the StripeClient.
I am trying to display the CardException errors when a user entered expired card number. But it is just returning a response of paymentMethods->create instead CardException errors.
Here is the code
try{
$stripe = new \Stripe\StripeClient(config('app.stripe_secret'));
echo $stripe->paymentMethods->create([
'type' => 'card',
'billing_details' => [
'name' => '123',
'email' => '123#gmail.com',
],
'card' => [
'number' => '4000000000000002',
'exp_month' => 11,
'exp_year' => 2023,
'cvc' => '314',
],
]);
} catch(\Stripe\Exception\CardException $e){
echo 'Status is:' . $e->getHttpStatus() . '\n';
echo 'Type is:' . $e->getError()->type . '\n';
echo 'Code is:' . $e->getError()->code . '\n';
echo 'Param is:' . $e->getError()->param . '\n';
echo 'Message is:' . $e->getError()->message . '\n';
}
inside try
first check stripe token create or not
$token = $stripe->tokens()->create(
[
'card' => [
'number' => $request->get('card_no'),
'exp_month' => $request->get('ccExpiryMonth'),
'exp_year' => $request->get('ccExpiryYear'),
'cvc' => $request->get('cvvNumber'),
],
]
);
if (!isset($token['id'])) {
// Session::flash('error', 'The Stripe Token was not generated correctly');
// return Redirect::back();
$json['type'] = 'error';
$json['message'] = 'The Stripe Token was not generated correctly';
return $json;
}

Get filename of failed uploaded file in Laravel 5.4

How do I get the filename of failed uploaded file and pass it on validation error message? For example: "The file.0 must be 1024 kilobytes." be like "The failed-file-sample.jpg must be 1024 kilobytes."
Here's the sample code arrangement:
$messages = [
'mimes' => 'File(s) must be of type: :values.',
'size' => 'The ' . $request->file('file_field')->getClientOriginalName() . ' must be :size kilobytes.'
];
$this->validate(
request(),
['file_field' => 'bail|required|mimes:gif,jpg,jpeg,png|size:1024'],
$messages
);
Use:
$validationArray = [];
foreach ($request->file('file_field') as $key => $file) {
$validationArray['file_field.'.$key.'.size'] => 'The ' . $file->getClientOriginalName() . ' must be 1024 kilobytes.';
}
$messages = [
'file_field.*.mimes' => 'File(s) must be of type: :values.',
$validationArray
];
$this->validate(
$request,
[
'file_field' => 'bail|required|array',
'file_field.*' => 'mimes:gif,jpg,jpeg,png|size:1024'
],
$messages);

Laravel Homestead Development: AJAX POST request gets redirected for unknown reason

I'm on Laravel 5.3 and I was working on a form I POST submit to the server. What I want to point out right at the beginning is that I have been working on this form for days, with no issues what so ever. Yesterday I was working in the store method of my controller, when all of a sudden the form won't submit at all anymore.
The opening form:
{!! Form::model($addon = new \App\Addon, ['name' => 'FinalForm', 'route' => 'addons.store', 'enctype' => 'multipart/form-data']) !!}
My route:
Route::resource('addons', 'AddonController');
php artisan route:list:
Method: POST
URI: addons
Name: addons.store
Action: App\Http\Controllers\AddonController#store
Middleware: web,auth
So what happens now is, the form gets posted, but then, for some reason, get's redirected. This is what chrome says:
POST request to /addons
And after that, for some reason, GET request to /addons/
As you can see I'm getting a 403 Forbidden response. Why is the request being redicted to /addons/ ??
As I said before, I changed NOTHING, I was working with the inner logic of the store method on the controller when this popped up. I even restarted the homestead box (and my whole machine), I don't know what is causing it.
EDIT (controller):
/**
* Store the addon.
*
* #return Response
**/
public function store(CreateAddonRequest $request, ImageHandler $imageHandler)
{
$input = $request->only('title', 'body', 'author', 'slogan', 'version', 'revision', 'published_at', '_img_data');
$session = $request->session()->all();
$slug = SlugService::createSlug(Addon::class, 'title', $input['title'], ['unique' => true]);
$imageHandler->move($slug, $input['body'], 'description');
$body = $imageHandler->body;
$addon = Auth::user()->addons()->create([
'body' => $body,
'title' => $input['title'],
'slogan' => $input['slogan'],
'author' => $input['author'],
'locales' => $session['locales'],
'published_at' => $input['published_at']
]);
$addon->categories()->attach($request->input('categories'));
Storage::makeDirectory('addons/' . $slug . '/files/');
$extension = Dir::extension('temp/' . $session['file_name']);
if ($input['revision'] != "") {
$file = $addon->slug . '-v' . $input['version'] . '-r' . $input['revision'] . '.' . $extension;
} else {
$file = $addon->slug . '-v' . $input['version'] . '.' . $extension;
}
Storage::disk('local')->move('temp/' . $session['file_name'], 'public/addons/' . $addon->slug . '/files/' . $file);
// With 5.4 we don't need that check anymore
if ($input['revision'] == "") {
$input['revision'] = null;
}
$addon->files()->create([
'file_name' => $file,
'hash' => $session['hash'],
'version' => $input['version'],
'revision' => $input['revision'],
'game_version' => $session['interface'],
'virustotal' => $session['virustotal']
]);
dd('done');
// if ($request->hasFile('images'))
// {
// Storage::makeDirectory('addons/' . $slug . '/images/');
// foreach ($request->file('images') as $image)
// {
// $imageName = uniqid() . '.' . $image->getClientOriginalExtension();
// //$image->storeAs('addons/' . $slug . '/images/', $imageName);
// $addon->images()->create(array('image_name' => $imageName));
// }
// }
// flash('The addon has been uploaded.');
// return redirect('addons');
dd('done');
return response()->json([
'success' => true,
'message' => 'Images checked',
]);
}

CodeIgniter 2 and Ion Auth - edit own user account

I am using the latest Ion Auth files along with the latest version of CodeIgniter 2.
There is a function called edit_user within the auth.php Controller file. This function is restricted to use only by members within the "Admin" group, and any Admin member can edit any other member using the function via this URL...
/auth/edit_user/id
The problem is that I don't see any Controller function or View that allows a regular (non-Admin) user to edit their own account details.
Would this be a new Controller function I'd need to write (modify edit_user function?) or is this something that Ion Auth should already do? If so, how?
Here is the stock Ion Auth edit_user function contained within the auth.php Controller...
function edit_user($id)
{
$this->data['title'] = "Edit User";
if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin())
{
redirect('auth', 'refresh');
}
$user = $this->ion_auth->user($id)->row();
//process the phone number
if (isset($user->phone) && !empty($user->phone))
{
$user->phone = explode('-', $user->phone);
}
//validate form input
$this->form_validation->set_rules('first_name', 'First Name', 'required|xss_clean');
$this->form_validation->set_rules('last_name', 'Last Name', 'required|xss_clean');
$this->form_validation->set_rules('phone1', 'First Part of Phone', 'required|xss_clean|min_length[3]|max_length[3]');
$this->form_validation->set_rules('phone2', 'Second Part of Phone', 'required|xss_clean|min_length[3]|max_length[3]');
$this->form_validation->set_rules('phone3', 'Third Part of Phone', 'required|xss_clean|min_length[4]|max_length[4]');
$this->form_validation->set_rules('company', 'Company Name', 'required|xss_clean');
if (isset($_POST) && !empty($_POST))
{
// do we have a valid request?
if ($this->_valid_csrf_nonce() === FALSE || $id != $this->input->post('id'))
{
show_error('This form post did not pass our security checks.');
}
$data = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'company' => $this->input->post('company'),
'phone' => $this->input->post('phone1') . '-' . $this->input->post('phone2') . '-' . $this->input->post('phone3'),
);
//update the password if it was posted
if ($this->input->post('password'))
{
$this->form_validation->set_rules('password', 'Password', 'required|min_length[' . $this->config->item('min_password_length', 'ion_auth') . ']|max_length[' . $this->config->item('max_password_length', 'ion_auth') . ']|matches[password_confirm]');
$this->form_validation->set_rules('password_confirm', 'Password Confirmation', 'required');
$data['password'] = $this->input->post('password');
}
if ($this->form_validation->run() === TRUE)
{
$this->ion_auth->update($user->id, $data);
//check to see if we are creating the user
//redirect them back to the admin page
$this->session->set_flashdata('message', "User Saved");
redirect("auth", 'refresh');
}
}
//display the edit user form
$this->data['csrf'] = $this->_get_csrf_nonce();
//set the flash data error message if there is one
$this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));
//pass the user to the view
$this->data['user'] = $user;
$this->data['first_name'] = array(
'name' => 'first_name',
'id' => 'first_name',
'type' => 'text',
'value' => $this->form_validation->set_value('first_name', $user->first_name),
);
$this->data['last_name'] = array(
'name' => 'last_name',
'id' => 'last_name',
'type' => 'text',
'value' => $this->form_validation->set_value('last_name', $user->last_name),
);
$this->data['company'] = array(
'name' => 'company',
'id' => 'company',
'type' => 'text',
'value' => $this->form_validation->set_value('company', $user->company),
);
$this->data['phone1'] = array(
'name' => 'phone1',
'id' => 'phone1',
'type' => 'text',
'value' => $this->form_validation->set_value('phone1', $user->phone[0]),
);
$this->data['phone2'] = array(
'name' => 'phone2',
'id' => 'phone2',
'type' => 'text',
'value' => $this->form_validation->set_value('phone2', $user->phone[1]),
);
$this->data['phone3'] = array(
'name' => 'phone3',
'id' => 'phone3',
'type' => 'text',
'value' => $this->form_validation->set_value('phone3', $user->phone[2]),
);
$this->data['password'] = array(
'name' => 'password',
'id' => 'password',
'type' => 'password'
);
$this->data['password_confirm'] = array(
'name' => 'password_confirm',
'id' => 'password_confirm',
'type' => 'password'
);
$this->load->view('auth/edit_user', $this->data);
}
Unless I'm missing something, I ended up modifying the edit_user function within the auth.php Controller as follows.
I changed this line which checks to see that the user is "not logged in" OR "not an admin" before dumping them out...
if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin())
{
redirect('auth', 'refresh');
}
...into this, which check to see that the user is "not logged in" OR ("not an admin" AND "not the user") before dumping them out...
if (!$this->ion_auth->logged_in() || (!$this->ion_auth->is_admin() && !($this->ion_auth->user()->row()->id == $id)))
This seems to be working...
Admin can edit all accounts
User can only edit his own account
and somebody not logged in, can't edit any account.
Edit: However, the user also has access to the "groups" setting and could simply put themself into the "admin" group. Not good.
Ion Auth's developer refers to the files he provides as working "examples". Therefore, it's up to the end-developer to edit Ion Auth to suit the needs of the project.
To prevent the user from being able to make himself an "admin" requires a simple change to the edit_user.php view file.
Verifies the user is already an "admin" before creating the checkboxes...
<?php if ($this->ion_auth->is_admin()): ?>
// code that generates Groups checkboxes
<?php endif ?>
Then you'll also need to thoroughly test and adjust as needed. For example, after editing a user profile, you're redirected to the auth view. Since the user doesn't have permission to see the auth view, there is a "must be an admin" error. In the controller file, you'll have to add the appropriate logic to properly redirect the user when they're not an "admin".
No Ion Auth doesn't do this as is - it's pretty light weight. But it's not hard to do and your on the right track, just grab that edit_user method and take out the admin checks and make it so the user can only edit their own account, just alter it so that it only updates user details for the currently logged in user.
Check the ion auth docs, have a crack at it and come back with some code if you have any problems.

CakePHP 2 & Translate Behavior: save multiple translations in a single form

Its possibile save multiple translations of the same field in a single form?
I have a model with Behavior Translate to translate the name field. The three translations (deu, eng, ita) are properly recorded in the i18n table but the field is not properly validated! Any suggestions?
app/Model/Category.php
class Category extends AppModel {
public $actsAs = array('Translate' => array('name' => 'TranslateName'));
public $validate = array(
'name' => array(
'notempty' => array(
'rule' => array('notempty'),
'message' => 'Error notempty',
),
),
);
...
app/View/Categories/admin_edit.ctp
<?php
echo $this->Form->create('Category');
echo $this->Form->input('Category.id');
echo $this->Form->input('Category.name.deu', array('label' => __d('Category', 'Name Deu')));
echo $this->Form->input('Category.name.eng', array('label' => __d('Category', 'Name Eng')));
echo $this->Form->input('Category.name.ita', array('label' => __d('Category', 'Name Ita')));
echo $this->Form->end(__d('app', 'Submit'));
?>
app/View/Controller/CategoriesController.php
if ($this->Category->save($this->request->data)) {
$this->Session->setFlash(__d('Category', 'The category has been saved'));
} else {
$this->Session->setFlash(__d('Category', 'The category could not be saved. Please, try again.'));
}
I have a similar Problem
But you can try out this - it should solve it for you: https://github.com/zoghal/cakephp-MultiTranslateBehavior

Resources