There is a problem when I write add() function for UsersController.
public function add(){
if ($this->request->is('post')) {
if ($this->User->save($this->request->data)) {
$this->Session->setFlash('The new user has been saved.');
$this->redirect(array('action' => 'test'));
}
}
$this->set('title_for_layout', 'Register');
}
This is add view ctp.
<?php
echo $this->Form->create('User');
echo $this->Form->input('username');
echo $this->Form->input('password');
echo $this->Form->end('Save User');
?>
There's always a internal error when I try to access to users/add. Anyone know how to deal with this problem? Thanks.
Have you tried testing for $this->data instead of $this->request->is('post')? It might not matter, but that's typically the way it is done.
Also, for saving, you should most likely (unless you are setting userid manually) do something like:
$this->User->create();
$this->User->save($this->data);
So your add function should look something like:
public function add(){
if ($this->data) {
$this->User->create();
if ($this->User->save($this->data)) {
$this->Session->setFlash('The new user has been saved.');
$this->redirect(array('action' => 'test'));
}
}
$this->set('title_for_layout', 'Register');
}
And you probably want your view to be something like:
<?php echo $form->create('User', array('action' => 'add')); ?>
<?php echo $form->input("username", array('label' => 'Username')) ?>
<?php echo $form->input("password",array("type"=>"password", 'label' => 'password')) ?>
<?php echo $form->submit('Submit'); ?>
Related
I have an add() view for Lessons - in it I have a form:
<?php echo $this->Form->create('Lesson'); ?>
<fieldset>
<legend><?php echo __('Add Lesson'); ?></legend>
<?php
echo $this->Form->input('name');
echo $this->Form->input('course_id', array('selected' => $this->request->data['named']['belongsToCourse']));
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
It seems that when I set $this->request->data['named']['belongsToCourse'] in the controller, it somehow made it so that I am not saving the $this->Form->input('name');
How do I set the course_id without breaking the $this->request->data which should include the new lesson's name?
Here's the error I get when trying to save. Because it thinks I'm saving a duplicate name '':
Error: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '' for key 'name'
Here is the action add():
public function add() {
$this->request->data = $this->request->params;
if ($this->request->is('post')) {
$courseID = $this->request->data['named']['belongsToCourse'];
$this->Lesson->create();
if ($this->Lesson->save($this->request->data)) {
$this->Session->setFlash(__('The lesson has been saved.'));
return $this->redirect(array('controller' => 'courses', 'action' => 'view', $courseID));
} else {
$this->Session->setFlash(__('The lesson could not be saved. Please, try again.'));
}
}
//debug(func_get_args());
$courses = $this->Lesson->Course->find('list');
$this->set(compact('courses'));
//$this->Form->input('course_id') = $belongsToCourse;
}
<div>//login.ctp
<?php echo $this->Form->create('Register'); ?>
<?php echo $this->Form->input('username'); ?>
<?php echo $this->Form->input('password'); ?>
<?php echo $this->Form->end('Login'); ?>
this is login page of my application.
If i give invalid details or valid details it logged in to home.please help hear..
public function login(){
if($this->Auth->loggedIn()){
$this->redirect(array('action' => 'home'));
}
if($this->request->is('post')){
if ($this->Auth->login($this->request->data)) {
$this->Session->write('Register',$this->request->data);
$this->redirect(array('controller' => 'Registers','action' => 'home'));
} else {
$this->Session->setFlash(__('Username or password is incorrect'));
}
}
}
this is RegistersController page..
change your form as below --
<?php echo $this->Form->create('Register'); ?>
<?php echo $this->Form->input('User.username'); ?>
<?php echo $this->Form->input('User.password'); ?>
<?php echo $this->Form->end('Login'); ?>
and check that in your AUTH component you have setted the model name to 'User' and fields mapped to Auth component settings -- and in your login function
$this->Session->write('Register',$this->request->data); this line is not needed any more.
go on below link for detail --
http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html
I have to 2 models that are related ItQuery and ItQueryComment. When a user adds a ItQuery other users should be able to comment on it. What i am trying to achieve is when other users add comments on a query they should be redirect to the view of the query not the index page for the it_query_comments
here is my code for my comments add view
<?php echo $this->Form->create('ItQueryComment'); ?>
<fieldset>
<legend><?php echo __('Add It Query Comment'); ?></legend>
<?php
echo $this->Form->input('it_query_id');
echo $this->Form->input('comment');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
and here is my add function in the controller
public function add() {
if ($this->request->is('post')) {
$this->ItQueryComment->create();
if ($this->ItQueryComment->save($this->request->data)) {
$this->Session->setFlash(__('The it query comment has been saved'));
$this->redirect(array('controller' => 'it_queries','action' => 'view', $itQuery['ItQuery']['id']));
} else {
$this->Session->setFlash(__('The it query comment could not be saved. Please, try again.'));
}
}
$itQueries = $this->ItQueryComment->ItQuery->find('list');
$this->set(compact('itQueries'));
}
If anyone could show me how to do this, that would be awesome. Thanks in advance
try the following
$this->redirect(array(
'controller' => 'it_queries',
'action' => 'view',
$this->request->data['ItQuery']['id'])
);
I am facing a strange problem while creating edit functionality in cakephp 2.1
Error genreated:
Illegal offset type [CORE\Cake\Model\Model.php, line 2689]
My edit.ctp file is
<?php echo $this->Form->create('Task');?>
<fieldset>
<legend>Edit Task</legend>
<?php
echo $this->Form->hidden('id');
echo $this->Form->input('title');
echo $this->Form->input('done');
?>
</fieldset>
<?php echo $this->Form->end('Save');?>
Model: Task.php
<?php
class Task extends AppModel {
var $name = 'Task';
}
?>
Controller :TasksController.php
<?php
class TasksController extends AppController {
var $name = 'Tasks';
var $helpers = array('Html', 'Form');
function index() {
$this->set('tasks', $this->Task->find('all'));
}
function add() {
if (!empty($this->data)) {
$this->Task->create();
if($this->Task->save($this->data)){
$this->Session->setFlash('The Task has been saved');
$this->redirect(array('action'=>'index'),null,true);
}else{
$this->Session->setFlash('Task not saved.Try again.');
}
}
}
function edit($id = null) {
if (!$id) {
$this->Session->setFlash('Invalid Task');
$this->redirect(array('action' => 'index'), null, true);
}
if (empty($this->data)) {
$this->data = $this->Task->find(array('id' => $id));
} else {
if ($this->Task->save($this->data)) {
$this->Session->setFlash('The Task has been saved');
$this->redirect(array('action' => 'index'), null, true);
} else {
$this->Session->setFlash('The Task could not be saved.Please, try again.');
}
}
}
}
?>
I think your find() method is erroneous:
$this->data = $this->Task->find(array('id' => $id));
change to
$this->data = $this->Task->find('all', array('conditions' => array('id' => $id)));
http://book.cakephp.org/2.0/en/models/retrieving-your-data.html
In order to prepopulate the data on the form you need to do the following:
<?php echo $this->Form->create('Task');?>
<fieldset>
<legend>Edit Task</legend>
<?php
echo $this->Form->hidden('id', array('value' => $this->data[0]['Task']['id']));
echo $this->Form->input('title', array('value' => $this->data[0]['Task']['title']));
echo $this->Form->input('done', array('value' => $this->data[0]['Task']['done']));
//var_dump($this->data[0]['Task']['id']);
?>
</fieldset>
<?php echo $this->Form->end('Save');?>
<?php echo $this->Html->link('List All Tasks', array('action'=>'index')); ?><br />
<?php echo $this->Html->link('Add Task', array('action'=>'add')); ?><br />
<?php echo $this->Html->link('List Done Tasks', array('action'=>'index')); ?><br />
<?php echo $this->Html->link('List Pending Tasks', array('action'=>'index')); ?><br />
when user enter the full url..i want to save only youtube id... pregmatch examine and extract video id and then it will be saved into database..the problem is how to make this pregmatch check and extract youtube id before save the full url
thanks for helping
// this is add() function in videos_controller
function add() {
if (!empty($this->data)) {
$this->Video->create();
if ($this->Video->save($this->data)) {
$this->Session->setFlash(__('The Video has been saved', true));
$this->redirect(array('action' => 'admin_index'));
} else {
$this->Session->setFlash(__('The Video could not be saved. Please, try again.', true));
}
}
$vcats = $this->Video->Vcat->find('list');
$this->set(compact('vcats'));
}
// this is add.ctp file
<div class="videos form">
<?php // echo $this->Form->create('Image');?>
<?php echo $form->create('Video'); ?>
<fieldset>
<legend><?php __('Add Video'); ?></legend>
<?php
echo $this->Form->input('vcat_id');
echo $this->Form->input('title');
$url= $this->Form->input('link');
echo $url
?>
</fieldset>
<?php echo $this->Form->end(__('Submit', true)); ?>
</div>
<div class="actions">
<h3><?php __('Actions'); ?></h3>
<ul>
<li><?php echo $this->Html->link(__('List Videos', true), array('action' => 'index')); ?></li>
<li><?php echo $this->Html->link(__('List Vcats', true), array('controller' => 'vcats', 'action' => 'index')); ?> </li>
<li><?php echo $this->Html->link(__('New Vcat', true), array('controller' => 'vcats', 'action' => 'add')); ?> </li>
</ul>
</div>
// we get the unique video id from the url by matching the pattern but where i put this code to match before save
preg_match("/v=([^&]+)/i", $url, $matches);
$id = $matches[1];
Here
function add() {
if (!empty($this->data)) {
$this->Video->create();
$url = $this->data['Video']['link'];
/*assuming you have a column `id` in your `videos` table
where you want to store the id,
replace this if you have different column for this*/
preg_match("/v=([^&]+)/i", $url, $matches);
$this->data['Video']['id'] = $matches[1];
//rest of the code
}
}
I guess a better place for it is in the Model's beforeSave or beforeValidate method:
class Video extends AppModel {
...
public function beforeSave() {
if (!empty($this->data[$this->alias]['link'])) {
if (preg_match("/v=([^&]+)/i", $this->data[$this->alias]['link'], $matches)) {
$this->data[$this->alias]['some_id_field'] = $matches[1];
}
}
return true;
}
...
}