Skip to content

Instantly share code, notes, and snippets.

@kleberksms
Last active December 30, 2015 19:59
Show Gist options
  • Save kleberksms/7878066 to your computer and use it in GitHub Desktop.
Save kleberksms/7878066 to your computer and use it in GitHub Desktop.
error message =/
<?php
/**model**/
App::uses('AppModel', 'Model');
/**
* User Model
*
* @property Wishlist $Wishlist
*/
class User extends AppModel
{
/**
* Display field
*
* @var string
*/
public $displayField = 'id';
/**
* Validation rules
*
* @var array
*/
public $validate = array(
'id' => array(
'numeric' => array(
'rule' => array('numeric'),
),
),
'email' => array(
'email' => array(
'rule' => array('email'),
'message' => 'Não é uma conta de email valida',
),
'unique' => array(
'rule' => array('isUnique'),
'message' => 'Esta conta de email já esta cadastrada.',
),
'notempty' => array(
'rule' => array('notempty'),
'message' => 'É necessário preencher este campo',
),
),
'password' => array(
'minLength' => array(
'rule' => array('minLength', 8),
'message' => 'A senha deve conter no mínimo 8 caracteres',
'on' => 'create'
),
'notempty' => array(
'rule' => array('notempty'),
'message' => 'A senha do não pode ser vazia',
'on' => 'create'
),
),
'terms' => array(
'checkterms' => array(
'rule' => array('checkTerms'),
'message' => 'Os termos tem que ser aceitos para continuar',
),
),
);
public function beforeValidate($options = array())
{
//Desconsiderar esta parte....
$user[$this->alias] = array(
'first_name' => $this->data[$this->alias]['first_name'],
'last_name' => $this->data[$this->alias]['last_name'],
'username' => $this->data[$this->alias]['username'],
'email' => $this->data[$this->alias]['email'],
'password' => $this->data[$this->alias]['password'],
'oauth_uid' => $this->data[$this->alias]['oauth_uid'],
);
unset($this->data[$this->alias]);
$this->data[$this->alias] = $user;
die(debug($this->data[$this->alias]));
parent::beforeValidate($options);
}
/**
*
* @return boolean
*/
public function checkTerms()
{
if ($this->data[$this->alias]['terms'] == 1) {
return true;
}
return false;
}
public function beforeSave($options = array())
{
if (isset($this->data[$this->alias]['password'])) {
$password = &$this->data[$this->alias]['password'];
if (!empty($password)) {
$password = Security::hash($password);
} else {
unset($this->data[$this->alias]['password']);
}
}
return parent::beforeSave($options);
}
//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* hasMany associations
*
* @var array
*/
public $hasMany = array(
'Wishlist' => array(
'className' => 'Wishlist',
'foreignKey' => 'user_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
}
//Controller
/**
* Users Controller
*
* @property User $User
*/
class UsersController extends AppController
{
public function register()
{
if ($this->request->is('post')) {
$this->User->create();
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('<p>Dados salvos com sucesso</p>'), 'default', array('class' => 'alert alert-success'));
$this->redirect(array('action' => 'login'));
}
$this->Session->setFlash(__('<p>Os dados não foram salvos, tente novamente!</p>'), 'default', array('class' => 'alert alert-danger'));
$this->redirect($this->referer());
}
$this->redirect($this->referer());
}
public function login()
{
$this->set('title_for_layout', 'Login');
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$this->User->id = $this->Auth->user('id'); // target correct record
$this->User->saveField('last_login', date(DATE_ATOM)); // save login time
$this->redirect(array('controller' => 'categories', 'action' => 'getCategories'));
}
}
}
public function logout()
{
$this->Auth->logout();
$this->redirect('/');
}
public function new_password()
{
}
}
?>
//view
//path: Home/index.ctp
<?php echo $this->Form->create('User', array('action' => 'register', 'class' => 'form-inline')) ?>
<div class="row">
<div class="col-xs-12">
<div class="form-group">
<?php
echo $this->Form->input('User.email', array(
'type' => 'text',
'class' => 'form-controll',
'placeholder' => 'Seu Email',
'label' => '',
)
);
?>
</div>
<div class="form-group">
<?php
echo $this->Form->input('User.password', array(
'type' => 'password',
'class' => 'form-controll',
'placeholder' => 'Sua Senha',
'label' => '',
)
);
?>
</div>
<?php echo $this->Form->button('Cadastre-se', array('type' => 'submit', 'class' => 'btn btn-default btn-cadastre')) ?>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<div class="checkbox">
<label>
<?php
echo $this->Form->input('User.terms', array(
'type' => 'checkbox',
'escape' => false,
'label' => 'Li e concordo com os ' . $this->Html->link('termos de uso', array(
'controller' => 'pages',
'actions', 'terms')))
);
?>
</label>
</div>
</div>
</div>
<?php echo $this->Form->end(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment