I'm trying to create a registration form for a user with Cake PHP. The user can be registered only, when the company ID exist and if he is allowed to register. The problem is that during registration I ask the user to specify the Company ID (company is created by an administrator, and to allow user registration the company ID must exists in database. User belongs to company)
These are my tables in database:
users id INT(11) NN, email VARCHAR(30) NN, password VARCHAR(255) NN, name VARCHAR(30) NN, company_id INT(11) NN, created DATE NN, modified DATE NN
companies id INT(11) NN, name VARCHAR(30) NN, phone VARCHAR(30), email VARCHAR(30) NN, address1 VARCHAR(50) NN, address2 VARCHAR(50) NN, town VARCHAR(30) NN, postcode VARCHAR(20), password VARCHAR(255), country_id INT(11) NN, created DATE, modified DATE,
This is User's Registration Page (View):
<div class="index large-4 medium-4 large-offset-4 medium-offset-4 columns">
<div class="panel">
<h3 class="text-center">Register New User</h3>
<?= $this->Form->create($user); ?>
<?= $this->Form->input('companyid'); ?>
<?= $this->Form->input('name'); ?>
<?= $this->Form->input('email'); ?>
<?= $this->Form->input('password', array('type' => 'password')); ?>
<?= $this->Form->submit('Register', array('class' => 'button')); ?>
<?= $this->Form->end(); ?>
</div>
</div>
**This is UsersController:**
//register
public function register(){
$user = $this->Users->newEntity();
//if the form is submitted
if($this->request->is('post')){
$user = $this->Users->patchEntity($user, $this->request->data);
if($this->Users->save($user)){
$this->Flash->success('Congratulation! You have successfully registered. Now you can log in');
return $this->redirect(['action' => 'login']);
} else {
$this->Flash->error('Something went wrong! You did not register to the system');
}
}
$this->set(compact('user'));
$this->set('_serialize', ['user']);
}
**AppController- Function Initialize**
public function initialize()
{
parent::initialize();
$this->loadComponent('RequestHandler');
$this->loadComponent('Flash');
$this->loadComponent('Auth', [
'authenticate' => [
'Form' => [
'fields' => [
'username' => 'email',
'password' => 'password'
]
]
],
'loginAction' => [
'controller' => 'Users',
'action' => 'login'
]
]);
}
Now. I just don't know how should I implement Registration for User in UserController in a way: Check if CompanyID exists in companies table- if yes- allow the user Registration and insert id, email, password, name, and company_id into users table. If doesn't exists display error "Company ID doesn't exists".