passing data to controller from view in cakephp - 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');

Related

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 form input filed empty after get submit

echo $this->Form->create('Driver', array('type' => 'get'));
echo $this->Form->input('name');
echo $this->Form->end('Search');
as result $this->request:
query => array(
'name' => 'some name'
)
Problem is input form is empty after search although $this->request->query['name'] = 'some name'
Everything works as expected when change form back to post
Edit. Included the model and the controller. For testing I use clean install.
Model (Driver.php):
App::uses('AppModel', 'Model');
class Driver extends AppModel {
public $displayField = 'name';
}
Controller (DriversController.php):
App::uses('AppController', 'Controller');
class DriversController extends AppController {
public function index() {
$drivers = $this->Driver->find('all');
$this->set(compact('drivers'));
}
}
In your controller code you do not show us where you are trying to access the submitted form values so I will try and give some general information to get you moving.
To access your form data, you need to cool use request. To see exactly what is going on, enter in your controller one of the below...
print_r($this->request->data);
or
print_r($this->request);
Either of those will show you any data registered with CakePHP.
If you want to save this save using your Models. use...
$this->Driver->save($this->request->data)
You might want to check it is a post first though.. lets complete the code...
public function submit() {
if ($this->request->is('post')) {
$this->Driver->create();
if ($this->Driver->save($this->request->data)) {
$this->Session->setFlash('Saved.');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash('FAILED');
}
}
}
The information above can be read in further detail here.
You can set form values by assigning to $this->data.
$this->data = $this->request->query;

cakephp:$this->Auth->user() login not working on production server?

I'm having a problem with the Auth component on the production server. When I enter the username and password and click on login, nothing happens except the password text box clears itself. I'm getting 'I'm in loginFailure'. I have no idea on why it is not going into 'if ($this->Auth->user())' at all.
Earlier on my development server this Auth component was working perfectly fine. I was able to login a user and once the user logins in, the report card pdf file for his/her child will be displayed.
Can anyone point out on what mistake i'm making? Thank you.
merry_parents_controller.php
class MerryParentsController extends AppController{
var $name='MerryParents';
function beforeFilter(){
parent::beforeFilter();
$this->Auth->autoRedirect=false;
//$this->redirect($this->Auth->redirect());
}
function login(){
echo "i'm in login";
if ($this->Auth->user()){
debug($this->Auth->user());
$this->data=$this->Auth->user();
if (!empty($this->data)){
$student_info=$this->MerryParent->Student->getStudents($this->data['MerryParent']['id']);
$this->set('student_info',$student_info);
print_r($student_info);
$this->redirect(array('controller'=>'aptitudes','action'=>'viewpdf',$student_info['Student']['id']));
//$this->render('/aptitudes/viewpdf');
}
else{
echo 'Auth user has not been set';
}
}
else
echo "Failure";
}
}
login.ctp
<?php
//var_dump($this->data);
$this->Session->flash('auth');
echo $this->Form->create('MerryParent',array('action'=>'login'));
echo $this->Form->input('MerryParent.username',array('label'=>'Name'));
echo $this->Form->input('MerryParent.password', array('value'=>''));
echo $this->Form->end('Login');
?>
app_controller.php
class AppController extends Controller {
var $components=array('Auth','Session','Cookie','Email','Security','RequestHandler','Cookie');
var $helpers = array('Html','Form','Ajax','Javascript','Session');
function beforeFilter(){
if (isset($this->Auth)){
$this->Auth->userModel='MerryParent';
$this->Auth->loginAction=array('controller'=>'merry_parents','action'=>'login');
/*var_dump($this->data);
debug($this->Auth->user);*/
$this->Auth->allow('*');
$this->Auth->loginRedirect=array('controller'=>'aptitudes','action'=>'viewpdf');
$this->Auth->logoutRedirect=array('controller'=>'merry_parents','action'=>'register');
$this->Auth->authorize='controller';
}
else
$this->Session->setFlash('Auth has not been set');
}
function isAuthorized(){
return true;
}
try this:
<?php
//var_dump($this->data);
$this->Session->flash('auth');
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');
?>
try this login function:
function login(){
echo "i'm in login";
if (!empty($this->data)){
if($this->Auth->login($this->data)) {
debug($this->Auth->user());
$this->data=$this->Auth->user();
if (!empty($this->data)){
$student_info=$this->MerryParent->Student->getStudents($this->data['MerryParent']['id']);
$this->set('student_info',$student_info);
print_r($student_info);
$this->redirect(array('controller'=>'aptitudes','action'=>'viewpdf',$student_info['Student']['id']));
//$this->render('/aptitudes/viewpdf');
} else {
echo 'Auth user has not been set';
}
}
} else echo "Failure";
}
There were 2 problems. After I modified login function as per CodeParadox's answer, It still wasn't working till I modified core.php's Security.salt and Security.cipherSeed. I just copied the values for salt and cipherSeed from my development server's core.php and it worked! :)
thank you.
Remove extra space in controller last line or other use php file like as "config","components" and "helpers"
<?php ?><Remove space>
your Author.authorize Controller should be a capital letter case like below:
$this->Auth->authorize='Controller';

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';
?>

Validation doesn't working in cakephp2.x

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');
}
?>

Resources