Validation doesn't working in cakephp2.x - cakephp

I am new in cakephp. I wrote a validation ctpfile in cakephp 2.6.7 for viewing login and logout word but the validation doesn't work.
My code is:-
<?php
if (!$authUser) {
echo $this->element('logout-header');
} else {
echo $this->element('login-header');
}
?>
How can I write validation in ctp file for viewing login and logout word in my page header?

Why you wrote a validation ctp? put your validation rules in model
http://book.cakephp.org/2.0/en/models/data-validation.html

In your AppController's beforeRender() callback set the authUser view variable by retrieving the logged in user:-
public function beforeRender() {
parent::beforeRender();
$this->set('authUser', $this->Auth->user());
}
Then the view code in your question should work as expected.

I have a solution, which I regularly use when I worked in cakephp.
in AppController.php
class AppController extends Controller{
public function beforeFilter() {
parent::beforeFilter();
$userInfo = array();
if($this->Auth->user('_id')){
$userInfo['User'] = $this->Auth->user();
Configure::write($userInfo);
}
}
}
And after this in view .ctp file
<?php
$authUser = Configure::read('User');
if (!$authUser) {
echo $this->element('logout-header');
} else {
echo $this->element('login-header');
}
?>

Related

Submit form to another application

So I started to play around with cakePHP after i did the blog tutorial (yes im new), and i would like to do something complicated. Like, theres a comment form, which is simple it contains Name and the Comment. And I would like to send the data to another application in the same host, which is save this comment in the DB. Currently the Comments/add.ctp saves it.
Thanks for any advice!
So theres the CommentsControll.php
<?php
class CommentsController extends AppController{
public $components = 'Session'
public function add(){
if($this->request->is('POST')){
$this->Comment->create();
if($this->Comment->save($this->request->data)){
$this->Session->setFlash('Your comment is saved!')
}
}
}
}
?>
And theres the Comments/add.ctp file
<?php
echo $this->Form->create('Comment');
echo $this->Form->input('name', array(
'label' => 'Your Name'
));
echo $this->Form->input('commenttext',array(
'label' => 'Your Comment'
));
echo $this->Form->end('Submit');
?>
Solution HttpSocket
CakePHP includes an HttpSocket class which can be used easily for making requests. It is a great way to communicate with external webservices, or remote apis.
// in your controller
App::uses('HttpSocket', 'Network/Http'); // This should be at the top of your Controller
class CommentsController extends AppController{
public $components = 'Session'
public function add(){
if($this->request->is('POST')){
$this->Comment->create();
$HttpSocket = new HttpSocket();
$response = $HttpSocket->post('http://example.com/add', $this->request->data));
// Get the status code for the response.
$code = $results->code;
if($code == 200) {
$this->Session->setFlash('Your comment is saved!');
} else {
$this->Session->setFlash('Opps! Somthing is wrong!');
}
}
}
}
Also see here CakePHP HttpSocket

passing data to controller from view in cakephp

I'm new to cakephp. I went through Andrew Perkins's video tutorials. It is a great tutorial for absolute beginners.
What I want to do is get the username to a hidden field from login page and keep it in a view.and pass it to the controller and save it to the database when it is necessary.
right now I have a add.ctp, BabiesController.php as the controller and baby.php as the model.
since I haven't completed the login page I would like to hard code the username for instance.
here are my codes. hope you guys can help me.
this is the database.emails address has been used as primary key for babyprents table.parent column int the babies table is a foreign key to the email address in the babyparents table.
Model : baby.php
<?php
class Baby extends AppModel {
var $name = 'Baby';
}
Controller : BabiesController.php
<?php
class BabiesController extends AppController {
var $name = 'Baby';
function index()
{
$this->set('babies',$this->Baby->find('all'));
}
function add()
{
if(!empty($this->data))
{
$this->Session->setFlash($this->data);
if($this->Baby->save($this->data))
{
$this->Session->setFlash('Successful');
$this->redirect(array('action'=>'index'));
}
else{
$this->Session->setFlash('unsuccessful');
}
}
}
}
View: add.ctp
<h2>Sign up</h2>
<?php
echo $this->form->create('Baby',array('action'=>'add'));
echo $this->form->input('firstName');
echo $this->form->input('middleName');
echo $this->form->input('lastName');
echo $this->form->input('birthday');
echo $this->form->input('age');
echo $this->form->input('doctor');
echo $this->form->end('Sign up');
?>
all the data has been saved except for email address.any guide will be appreciated. thank you all.
In your controller use saveAll($this->data):
function add()
{
if(!empty($this->data))
{
$this->Session->setFlash($this->data);
if($this->Baby->saveAll($this->data))
{
$this->Session->setFlash('Successful');
$this->redirect(array('action'=>'index'));
}
else{
$this->Session->setFlash('unsuccessful');
}
}
}
In your add.ctp put this line
echo $this->form->input('Babyparent.email');

Passing Data from Dropbox Chooser to controller/model in Cakephp

EDIT:
I am getting the JSON results from the dropbox chooser, but they are not being parsed properly to be put into the database. I can't figure out what I am doing wrong, if I should be doing a JSON decoder in my controller or if it is something else.
Controller Code:
<?php class DropboxfilesController extends AppController {
public function add() {
if ($this->request->is('post')) {
$this->File->create();
if ($this->File->save($this->request->data)) {
$this->Session->setFlash(__('Your file is now available :)'));
$this->redirect($this->referer());
} else {
$this->Session->setFlash(__('Something went wrong!'));
}
}
}}?>
View Code:
<?php echo $this->Form->create('File'); ?>
<input type="Dropboxfilechooser" name="selected-file" style="visibility: hidden;"/>
<?php echo $this->Form->end('Finish'); ?>
Model Code:
<?php class File extends AppModel {}?>
The actual problem is looking at the source code, the form action is not going to the proper controller, it is just going to the cake/homes. Just change that and the problem is solved.

cakephp: login form not redirecting

When I click the login button on the login form, nothing happens. It is not redirecting to /merry_parents/report_card. I have specified $this->Auth->loginRedirect in beforeFilter. Does anyone know on what i'm doing wrong? thanks in advance.
Following is my merry_parents controller, login view and report_card view
app_controller.php
class AppController extends Controller {
var $components=array('Auth','Session');
function beforeFilter(){
if (isset($this->Auth)){
$this->Auth->userModel='MerryParent';
$this->Auth->loginAction=array('controller'=>'merry_parents','action'=>'login');
$this->Auth->loginRedirect=array('controller'=>'merry_parents','action'=>'report_card');
$this->Auth->allow('signup','login','logout');
$this->Auth->authorize='controller';
//$this->Auth->actionPath='controllers/';
}
else
$this->Session->setFlash('Auth has not been set');
}
function isAuthorized(){
return true;
}
}
merry_parents_controller.php
class MerryParentsController extends AppController{
var $name='MerryParents';
var $components=array('Acl','Auth','Session');
function report_card(){
}
function register(){
}
function login(){
}
function logout(){
$this->redirect($this->Auth->logout());
}
function signup(){
print_r($this->data);
if (!empty($this->data)){
//$this->Auth->password($this->data['MerryParent']['password2'] used to get what the hashed password2 would look like.
$this->MerryParent->set($this->data);
//validates calls invalidFields methods which in turn populates validationErrors arrays. Validates is used to validate a record prior to updating. Needed only when you want to update certain fields in an existing record.
if ($this->MerryParent->validates(array('fieldList'=>array('username','email','password','password2')))){
if ($this->data['MerryParent']['password']==$this->Auth->password($this->data['MerryParent']['password2'])){
$this->MerryParent->id=$this->MerryParent->field('id',
array('MerryParent.username'=>$this->data['MerryParent']['username'],
'MerryParent.email'=>$this->data['MerryParent']['email'])
);
echo $this->MerryParent->id;
//die(debug($this->MerryParent->validationErrors));
if ($this->MerryParent->save($this->data,false))//record with $this->MerryParent->id is updated
{
$this->Auth->login($this->data); //automatically logs a user in after registration
$this->redirect('/merry_parents/report_card');
}
else
echo $this->Session->setFlash(__('Your admission could not be saved, please try again!',true));
}//end if ($this->data['MerryParent']['password']....
else
echo $this->Session->setFlash('Typed passwords did not match');
}//if ($this->MerryParent->validates
}//end if (!empty($this->data))
}
}
?>
login.ctp
<?php
echo $this->Form->create('MerryParent',array('action'=>'login'));
echo $this->Form->input('username',array('label'=>'Name'));
echo $this->Form->input('password', array('value'=>''));
echo $this->Form->end('Login');
?>
report_card.ctp
<?php
echo 'HALLO';
?>

cakephp: login link not taking me to login page instead it is taking me to loginRedirect page

My login link on register page is not taking me to login page. Instead it is taking me to report_card page which is the loginRedirect page.
In beforeFilter i've set autoRedirect to false coz' i'm setting cookies in login function and then i'm setting $this->redirect($this->Auth->redirect());
Can someone please help me? thanks in advance.
my code:
register.ctp
<?php
echo $this->Html->link('Sign Up','/merry_parents/signup',array()).' for new user |'.$this->Html->link('Login','/merry_parents/login',array()).' for existing user';
?>
app_controller.php
class AppController extends Controller {
var $components=array('Auth','Session','Cookie');
function beforeFilter(){
if (isset($this->Auth)){
$this->Auth->userModel='MerryParent';
$this->Auth->loginAction=array('controller'=>'merry_parents','action'=>'login');
//var_dump($this->data);
$this->Auth->loginRedirect=array('controller'=>'merry_parents','action'=>'report_card');
$this->Auth->allow('signup','login','logout','display');
$this->Auth->authorize='controller';
}
else
$this->Session->setFlash('Auth has not been set');
}
function isAuthorized(){
return true;
}
merry_parents_controller.php
<?php
class MerryParentsController extends AppController{
var $name='MerryParents';
var $helpers=array('Html','Form');
function beforeFilter(){
$this->Auth->autoRedirect=false;
parent::beforeFilter();
}
function report_card(){
}
function register(){
}
function login(){
if ($this->Auth->user()){
if (!empty($this->data)){
$this->MerryParent->id=$this->MerryParent->field('id',array(
'MerryParent.username'=>$this->data['MerryParent']['username'],
'MerryParent.password'=>$this->data['MerryParent']['password']
)
);
echo 'id: '.$this->MerryParent->id;
$this->Cookie->write('MerryParent.id',$this->MerryParent->id,false,0);
$this->set('id',$this->Cookie->read('MerryParent.id'));
}
$this->redirect($this->Auth->redirect());
}
}
report_card.ctp
<?php
var_dump($this->data);
echo 'HALLO';
if (isset($id))
echo $id;
else
echo 'id has not been set';
?>
Actually the problem was when I clicked on login link for the first time, login link displays fine. But, the next time i click on login link again, login page doesn't display, instead report_card page (ie. the login redirect page) displays. The reason is, i didn't have a logout button anywhere on my webpage, so the user was logged on all the time. Thanks.
in register function of merry_parents_controller.php
function register(){
$this->set('id',$this->Session->read('Auth.MerryParent.id'));
}
in register.ctp
<?php
if (isset($id)){
echo $this->Html->link('Logout',
array('controller'=>'merry_parents','action'=>'logout'),array());
}
else{
echo $this->Html->link('Sign Up','/merry_parents/signup',array()).' for new user |'.
$this->Html->link('Login','/merry_parents/login',array()).' for existing user';
}
?>
now, login and logout works fine.

Resources