[SOLVED] Registration using Modal #355
-
Hello Shield Team, Am starting on my ci4 project and I want to be registering users. So i followed 333 for some instructions on how to add custom fields. I have two problems
use CodeIgniter\Shield\Entities\User;
$users = model('UserModel');
$user = new User([
'username' => 'foo-bar',
'email' => 'foo.bar@example.com',
'password' => 'secret plain text password',
'first_name' => 'Pooya',
'last_name' => 'Parsa',
]);
$users->save($user); Should it be in my custom Model UsersModel?
Here is my code ### AUTH public array $views = [
'login' => '\App\Views\Shield\login',
'register' => '\App\Views\Admin\Users\all', VIEW<div class="card-body p-md-3 p-1">
<div class="row mb-2"><div class="col"><button class="btn btn-sm btn-warning float-end" data-bs-toggle="modal" data-bs-target="#addUser_Modal">Create User</button></div></div>
</div></div>
<!-- Add User Modal -->
<div class="modal fade" id="addUser_Modal" tabindex="-1" aria-labelledby="addUser_ModalLabel" >
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">User Registration</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form action="<?= base_url('billing/users/add'); ?>" method="post">
<?= csrf_field() ?>
<div class="modal-body">
<!-- First Name & Last Name -->
<div class="row">
<div class="col">
<div class="form-floating mb-3 g-2">
<input type="text" name="first_name" class="form-control" inputmode="text" autocomplete="first_name" id="floatingInput" placeholder="<?= lang('Auth.first_name') ?>" value="<?= old('first_name') ?>" required>
<label for="floatingInput">First Name</label>
</div>
</div>
<div class="col">
<div class="form-floating mb-3 g-2">
<input type="text" name="last_name" class="form-control" inputmode="text" autocomplete="last_name" id="floatingInput" placeholder="<?= lang('Auth.last_name') ?>" value="<?= old('last_name') ?>" required>
<label for="floatingInput">Last Name</label>
</div>
</div>
</div>
<!-- Email -->
<div class="form-floating mb-3 g-2">
<input type="email" class="form-control" name="email" inputmode="email" autocomplete="email" id="floatingInput" placeholder="<?= lang('Auth.email') ?>" value="<?= old('email') ?>" required>
<label for="floatingInput">Email</label>
</div>
<!-- Username -->
<div class="form-floating mb-3 g-2">
<input type="text" class="form-control" name="username" inputmode="text" autocomplete="username" id="floatingInput" placeholder="<?= lang('Auth.username') ?>" value="<?= old('username') ?>" required>
<label for="floatingInput">Username</label>
</div>
<!-- Password -->
<div class="row">
<div class="col">
<div class="form-floating mb-3 g-2">
<input type="password" class="form-control" name="password" inputmode="text" id="floatingInput" autocomplete="new-password" placeholder="<?= lang('Auth.password') ?>" required>
<label for="floatingInput">Password</label>
</div>
</div><div class="col">
<!-- Password (Again) -->
<div class="form-floating mb-3 g-2">
<input type="password" class="form-control" name="password_confirm" inputmode="text" autocomplete="new-password" id="floatingInput" placeholder="<?= lang('Auth.passwordConfirm') ?>" required>
<label for="floatingInput">Retype Password</label>
</div>
</div></div>
<!-- Telephone -->
<div class="row">
<div class="col">
<div class="form-floating mb-3 g-2">
<input type="text" class="form-control" name="telephone" inputmode="text" id="floatingInput" autocomplete="telephone" placeholder="<?= lang('Auth.telephone') ?>" value="<?= old('telephone') ?>">
<label for="floatingInput">Telephone</label>
</div>
</div><div class="col">
<!-- WhatsApp No. -->
<div class="form-floating mb-3 g-2">
<input type="text" class="form-control" name="whatsapp" inputmode="text" autocomplete="whatsapp" id="floatingInput" placeholder="<?= lang('Auth.whatsapp') ?>" value="<?= old('whatsapp') ?>" >
<label for="floatingInput">WhatsApp</label>
</div>
</div></div>
<!-- Full Address -->
<div class="form-floating mb-3 g-2">
<textarea class="form-control" name="full_address" placeholder="Enter your Address" id="floatingTextarea"></textarea>
<label for="floatingTextarea">Full Address</label>
</div>
<!-- Position -->
<div class="row">
<div class="col">
<div class="form-floating mb-3 g-2">
<input type="text" class="form-control" name="position" inputmode="text" id="floatingInput" autocomplete="position" placeholder="<?= lang('Auth.position') ?>" value="<?= old('position') ?>" required>
<label for="floatingInput">Position</label>
</div>
</div><div class="col">
<!-- Branch -->
<div class="form-floating mb-3 g-2">
<input type="text" class="form-control" name="branch" inputmode="text" autocomplete="whatsapp" id="floatingInput" placeholder="<?= lang('Auth.branch') ?>" value="<?= old('branch') ?>" required>
<label for="floatingInput">Branch Posted</label>
</div>
</div></div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary"><?= lang('Auth.register') ?></button>
</div>
</form>
</div>
</div>
</div>
<!-- End Add User Modal--> CONTROLLER<?php
namespace App\Controllers\Admin;
use CodeIgniter\Shield\Controllers\RegisterController;
use App\Models\UsersModel;
class CreateAccount extends RegisterController
{
protected $usersModel;
public function addUser()
{
$data = [];
$usersModel = new UsersModel();
if($this->request->getMethod() == 'post'){
//setting validations
$rules = [
'username' => [
'label' => 'Auth.username',
'rules' => 'required|max_length[30]|min_length[3]|regex_match[/\A[a-zA-Z0-9\.]+\z/]|is_unique[users.username]',
],
'email' => [
'label' => 'Auth.email',
'rules' => 'required|max_length[254]|valid_email|is_unique[auth_identities.secret]',
],
'gender' => [
'label' => 'Auth.gender',
'rules' => 'in_list[male,female,bisexual]',
],
'first_name' => [
'label' => 'First Name',
'rules' => 'required|alpha|min_length[3]',
],
'last_name' => [
'label' => 'Last Name',
'rules' => 'required|alpha|min_length[3]',
],
'telephone' => [
'label' => 'Mobile Number',
'rules' => 'required',
],
'whatsapp' => [
'label' => 'WhatsApp Number',
'rules' => 'required',
],
'branch' => [
'label' => 'Branch Name',
'rules' => 'required',
],
'password' => [
'label' => 'Auth.password',
'rules' => 'required|strong_password',
],
'password_confirm' => [
'label' => 'Auth.passwordConfirm',
'rules' => 'required|matches[password]',
],
];
if($this->validate($rules))
{
//User details into Db
$userdata = [
'username' => $this->request->getVar('username', FILTER_SANITIZE_STRING),
'first_name' => $this->request->getVar('first_name'),
'last_name' => $this->request->getVar('last_name'),
'email' => $this->request->getVar('email'),
'gender' => $this->request->getVar('gender'),
'avatar' => $this->request->getVar('avatar'),
'telephone' => $this->request->getVar('telephone'),
'area_code' => $this->request->getVar('area_code'),
'birth_day' => $this->request->getVar('birth_day'),
'country' => $this->request->getVar('country'),
'state' => $this->request->getVar('state'),
'city' => $this->request->getVar('city'),
'full_address' => $this->request->getVar('full_address'),
'whatsapp' => $this->request->getVar('whatsapp'),
'position' => $this->request->getVar('position'),
'branch' => $this->request->getVar('branch'),
'relative_name' => $this->request->getVar('relative_name'),
'relative_number' => $this->request->getVar('relative_number'),
'description' => $this->request->getVar('description'),
'created_at' => date("Y-m-d h:m:s"),
'password' => $this->request->getVar('password'),
];
if($usersModel->save($userdata))
{
session()->setTempdata('success', 'User Account created', 3);
return redirect()->to('/billing/users');
}
else
{
session()->setTempdata('error', 'Sorry! User Account created', 3);
return redirect()->to('/billing/users');
}
}
else
{
$this->data['validation'] = $this->validator;
}
}
return view('admin/users/all', $data);
}
} MODEL<?php
namespace App\Models;
use CodeIgniter\Shield\Models\UserModel;
use CodeIgniter\Shield\Entities\User;
class UsersModel extends UserModel
{
protected $allowedFields = [
'username',
'password',
'email',
'status_message',
'status',
'gender',
'first_name',
'last_name',
'avatar',
'description',
'telephone',
'area_code',
'birth_day',
'country',
'state',
'city',
'full_address',
'whatsapp',
'position',
'branch',
'relative_name',
'relative_number',
'created_at',
'updated_at',
];
protected $useTimestamps = false;
protected $createdField = 'created_at';
protected $updatedField = 'updated_at';
protected $deletedField = 'deleted_at';
public function getUsers()
{
$builder = $this->db->table('users');
$builder->orderBy('id', 'ASC');
$result = $builder->get()->getResult();
return $result;
}
} Looking forward hearing from you and for any advise for novice like me is highly appreciated. |
Beta Was this translation helpful? Give feedback.
Answered by
sammyskills
Aug 9, 2022
Replies: 1 comment 9 replies
-
This should be in your controller, like so:
Then in your
|
Beta Was this translation helpful? Give feedback.
9 replies
Answer selected by
jlwegaba
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This should be in your controller, like so:
Then in your
addUser()
method, after the validation, you can do something like this:You can read more about
Entities
here and aboutFill
here.