cakephp multiple forms with same action - forms

I've got on my page several News, to every News we can add comment via form.
So actually I've got 3 News on my index.ctp, and under every News is a Form to comment this particular News. Problem is, when i add comment, data is taken from the last Form on the page.
I don;t really know how to diverse them.
i've red multirecord forms and Multiple Forms per page ( last one is connected to different actions), and i don't figure it out how to manage it.
Second problem is, i can't send $id variable through the form to controller ( $id has true value, i displayed it on index.ctp just to see )
This is my Form
<?php $id = $info['Info']['id']; echo $this->Form->create('Com', array('action'=>'add',$id)); ?>
<?php echo $this->Form->input(__('Com.mail',true),array('class'=>'form-control','field'=>'mail')); ?>
<?php echo $this->Form->input(__('Com.body',true),array('class'=>'form-control')); ?>
<?php echo $this->Form->submit(__('Dodaj komentarz',true),array('class'=>'btn btn-info')); ?>
<?php $this->Form->end(); ?>
and there is my controller ComsController.php
class ComsController extends AppController
{
public $helpers = array('Html','Form','Session');
public $components = array('Session');
public function index()
{
$this->set('com', $this->Com->find('all'));
}
public function add($idd = NULL)
{
if($this->request->is('post'))
{
$this->Com->create();
$this->request->data['Com']['ip'] = $this->request->clientIp();
$this->request->data['Com']['info_id'] = $idd;
if($this->Com->save($this->request->data))
{
$this->Session->setFlash(__('Comment added with success',true),array('class'=>'alert alert-info'));
return $this->redirect(array('controller'=>'Infos','action'=>'index'));
}
$this->Session->setFlash(__('Unable to addd comment',true),array('class'=>'alert alert-info'));
return false;
}
return true;
}
}

you are not closing your forms
<?php echo $this->Form->end(); ?>
instead of
<?php $this->Form->end(); ?>
for the id problem you should write
echo $this->Form->create(
'Com',
array('action'=>'add/'.$id
)
);
or
echo $this->Form->create(
'Com',
array(
'url' => array('action'=>'add', $id)
)
);

Related

Zend Framework addaction with success or failure message

public function addAction()
{
$form = new ApplicationForm();
$this->view->form = $form;
if ($this->getRequest()->isPost()) {
$formData = $this->getRequest()->getPost();
if ($form->isValid($formData)) {
$name = $form->getvalue('name');
$class = $form->getvalue('class');
$file = new Application_Model_DbTable_Records();
$file->addRecord($name,$class);
$this->_helper->redirector('index');
}
}
}
Above addAction controller part, here when i am clicking AddAction my form is waiting for user inputs when i click submit my inputs recorded in database.
Now my question is i want add some message after the submit form data whether it success or failure.
Could you please help me on this ?
Many Thanks,
viswa
The docs for the action-helper describe an example. But standard usage goes something like this:
After you add the record, before you redirect, set the desired message in your controller:
public function addAction()
{
$form = new ApplicationForm();
$this->view->form = $form;
if ($this->getRequest()->isPost()) {
$formData = $this->getRequest()->getPost();
if ($form->isValid($formData)) {
$name = $form->getValue('name');
$class = $form->getValue('class');
$file = new Application_Model_DbTable_Records();
$file->addRecord($name,$class);
// Add the message here
$this->_helper->getHelper('FlashMessenger')->addMessage('Record added');
$this->_helper->redirector('index');
}
}
}
Then in your indexAction - the controller to which you are redirecting after successful record addition - get the messages and add them to your view:
public function indexAction()
{
// All your existing processing
// Blah, blah..
// Get the messages from the FlashMessenger
$messenger = $this->_helper->getHelper('FlashMessenger');
$messages = $messenger->hasMessages() ? $messenger->getMessages() : [];
// Add the messages into the view
$this->view->messages = $messages;
}
Finally, somewhere in the index view-script where you want the messages to appear, check for the messages and render, something like:
<?php if ($this->messages): ?>
<div id="refresh-messages">
<ul>
<?php foreach ($this->messages as $message): ?>
<li><?= $message ?></li>
<?php endforeach ?>
</ul>
</div>
<?php endif ?>
The wrapping div is just to assist with styling by providing a DOM element id to which you can target your CSS.
Disclaimer: Not tested directly, just coding from memory.

How to set validation rules for custom CActiveRecord attributes in Yii?

I'm working on a Yii project with a database, that contains a table, where almost all it's data is saved in a field as JSON (it's crazy, but it is so as it is):
id INTEGER
user_id INTEGER
data LONGTEXT
This "JSON field" data has following structure and contains inter alia an image:
{
"id":"1",
"foo":"bar",
...
"data":{
"baz":"buz",
...
}
}
Displaying it is no problem, but now I want to make the data ediable. My form looks like this:
<?php
$form = $this->beginWidget('CActiveForm', array(
'id' => 'my-form',
'htmlOptions' => array('enctype' => 'multipart/form-data'),
'enableAjaxValidation'=>false,
));
?>
<div class="row">
<?php echo $form->labelEx($model, 'foo'); ?>
<?php
echo $form->textField($model, 'foo', array(...));
?>
<?php echo $form->error($model, 'foo'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model, 'baz'); ?>
<?php
echo $form->textField($model, 'data[baz]', array(...));
?>
<?php echo $form->error($model, 'data[baz]'); ?>
</div>
It works. But there are multiple problems, that seem to be caused by the same thing -- that he form fields are not referenced to the model attributes/properties:
When I make fields foo and baz required (public function rules() { return array(array('foo, baz', 'required')); } -- the property $foo is defined) foo bahaves as wished, but baz causes an "foo cannot be blank" error. So I cannot set a data[*] as required.
If the form is not valid and gets reloaded, all the data[*] fields are empty.
The data[*] fields are not marked as required.
Is there a to solve this without to change the datase structure? There will not be a correct way for it, but maybe a workaround.
It's impossible to validate fields in such way. First of all if you are using field in model it must be defined or exist in table for active record. So if you want to validate such structure the only right way to do it:
class Model extends CActiveRecord {
// Define public varialble
public $data_baz;
public function rules(){
return array(
// Add it to rules
array( 'data_baz', 'required' )
);
}
public function attributeLabels(){
return array(
// Add it to list of labels
'data_baz' => 'Some field'
);
}
protected function beforeSave(){
if ( !parent::beforeSave() ) {
return false;
}
// Also you may create a list with names to automate append
$this->data['baz'] = $this->data_baz;
// And serialize data before save
$this->data = serialize( $this->data );
return true;
}
}
And your form should looks like
<div class="row">
<?php echo $form->labelEx($model, 'data_baz'); ?>
<?php echo $form->textField($model, 'data_baz'); ?>
<?php echo $form->error($model, 'data_baz'); ?>
</div>

Passing variables in PHP Zend Framework

I think I have just been working too long and am tired. I have an application using the Zend Framework where I display a list of clubs from a database. I then want the user to be able to click the club and get the id of the club posted to another page to display more info.
Here's the clubs controller:
class ClubsController extends Zend_Controller_Action
{
public function init()
{
}
public function indexAction()
{
$this->view->assign('title', 'Clubs');
$this->view->headTitle($this->view->title, 'PREPEND');
$clubs = new Application_Model_DbTable_Clubs();
$this->view->clubs = $clubs->fetchAll();
}
}
the model:
class Application_Model_DbTable_Clubs extends Zend_Db_Table_Abstract
{
protected $_name = 'clubs';
public function getClub($id) {
$id = (int) $id;
$row = $this->fetchRow('id = ' . $id);
if (!$row) {
throw new Exception("Count not find row $id");
}
return $row->toArray();
}
}
the view:
<table>
<?php foreach($this->clubs as $clubs) : ?>
<tr>
<td><a href=''><?php echo $this->escape($clubs->club_name);?></a></td>
<td><?php echo $this->escape($clubs->rating);?></td>
</tr>
<?php endforeach; ?>
</table>
I think I am just getting confused on how its done with the zend framework..
in your view do this
<?php foreach ($this->clubs as $clubs) : ?>
...
<a href="<?php echo $this->url(array(
'controller' => 'club-description',
'action' => 'index',
'club_id' => $clubs->id
));?>">
...
That way you'll have the club_id param available in index action of your ClubDescription controller. You get it like this $this->getRequest()->getParam('club_id')
An Example:
class ClubsController extends Zend_Controller_Action
{
public function init()
{
}
public function indexAction()
{
$this->view->assign('title', 'Clubs');
$this->view->headTitle($this->view->title, 'PREPEND');
$clubs = new Application_Model_DbTable_Clubs();
$this->view->clubs = $clubs->fetchAll();
}
public function displayAction()
{
//get id param from index.phtml (view)
$id = $this->getRequest()->getParam('id');
//get model and query by $id
$clubs = new Application_Model_DbTable_Clubs();
$club = $clubs->getClub($id);
//assign data from model to view [EDIT](display.phtml)
$this->view->club = $club;
//[EDIT]for debugging and to check what is being returned, will output formatted text to display.phtml
Zend_debug::dump($club, 'Club Data');
}
}
[EDIT]display.phtml
<!-- This is where the variable passed in your action shows up, $this->view->club = $club in your action equates directly to $this->club in your display.phtml -->
<?php echo $this->club->dataColumn ?>
the view index.phtml
<table>
<?php foreach($this->clubs as $clubs) : ?>
<tr>
<!-- need to pass a full url /controller/action/param/, escape() removed for clarity -->
<!-- this method of passing a url is easy to understand -->
<td><a href='/index/display/id/<?php echo $clubs->id; ?>'><?php echo $clubs->club_name;?></a></td>
<td><?php echo $clubs->rating;?></td>
</tr>
<?php endforeach; ?>
an example view using the url() helper
<table>
<?php foreach($this->clubs as $clubs) : ?>
<tr>
<!-- need to pass a full url /controller/action/param/, escape() removed for clarity -->
<!-- The url helper is more correct and less likely to break as the application changes -->
<td><a href='<?php echo $this->url(array(
'controller' => 'index',
'action' => 'display',
'id' => $clubs->id
)); ?>'><?php echo $clubs->club_name;?></a></td>
<td><?php echo $clubs->rating;?></td>
</tr>
<?php endforeach; ?>
</table>
[EDIT]
With the way your current getClub() method in your model is built you may need to access the data using $club['data']. This can be corrected by removing the ->toArray() from the returned value.
If you haven't aleady done so you can activate error messages on screen by adding the following line to your .htaccess file SetEnv APPLICATION_ENV development.
Using the info you have supplied, make sure display.phtml lives at application\views\scripts\club-description\display.phtml(I'm pretty sure this is correct, ZF handles some camel case names in a funny way)
You can put the club ID into the URL that you link to as the href in the view - such as /controllername/club/12 and then fetch that information in the controller with:
$clubId = (int) $this->_getParam('club', false);
The 'false' would be a default value, if there was no parameter given. The (int) is a good practice to make sure you get a number back (or 0, if it was some other non-numeric string).

calling echo $this->action('panLogin','user') and $this->action('panRegister','user') on same script

I have a problem, i'm trying to render 2 forms (login and register) on one layout scrpt (header.phtml), every time i submit on one of the forms both actions for the controller are getting fired and i'm unsure how to fix it.
The forms are getting rendered fine within the layout, however when you click 'Login' or 'Register' on the forms the code fires in both the 'login' and 'register actions.
the header layout script snippet:-
<div class="left">
<h1>Already a member? <br>Then Login!</h1>
<?php
echo $this->action('panlogin', 'user');
?>
</div>
<div class="left right">
<h1>Not a member yet? <br>Get Registered!</h1>
<?php
echo $this->action('panregister', 'user');
?>
</div>
the action scripts (phtmls)
panregister.phtml
<div id="pan-register">
<?php
$this->registerForm->setAction($this->url);
echo $this->registerForm;
?>
</div>
panlogin.phtml
<div id="pan-login">
<?php
$this->loginForm->setAction($this->url);
?>
</div>
the user controller actions:-
class Ajfit_UserController extends Zend_Controller_Action
{
protected $_loginForm;
protected $_registerForm;
public function init()
{
$this->_loginForm = new Ajfit_Form_User_Login(array(
'action' => '/user/login',
'method' => 'post',
));
$this->_registerForm = new \Ajfit\Form\User\Registration(array(
'action' => '/user/register',
'method' => 'post'
));
}
//REGISTER ACTIONS
public function panregisterAction(){
$this->registerAction();
}
public function registerAction(){
$request = $this->_request;
if ($this->_request->isPost()){
$formData = $this->_request->getPost();
}
$this->view->registerForm = $this->_registerForm;
}
//LOGIN ACTIONS
public function panloginAction(){
$this->loginAction();
}
public function loginAction(){
$request = $this->_request;
if(!$auth->hasIdentity()){
if ($this->_request->isPost()){
$formData = $this->_request->getPost();
}
}
$this->view->loginForm = $this->_loginForm;
}
}
Please can someone with a little more knowlegde with the action('act','cont'); ?> code with in a layout script help me out with this problem.
Thanks
Andrew
While David is correct where best practices are concerned, I have on occasion just added another if() statement. Kinda like this:
if ($this->getRequest()->isPost()) {
if ($this->getRequest()->getPost('submit') == 'OK') {
just make sure your submit label is unique.
Eventually I'll get around to refactoring all those actions I built early in the learning process, for now though, they work.
Now to be nosy :)
I noticed: $formData = $this->_request->getPost(); while this works, if you put any filters on your forms retrieving the data in this manner bypasses your filters. To retrieve filtered values use $formData = $this->getValues();
from the ZF manual
The Request Object
GET and POST Data
Be cautious when accessing data from the request object as it is not filtered in any way. The router and
dispatcher validate and filter data for use with their tasks, but
leave the data untouched in the request object.
From Zend_Form Quickstart
Assuming your validations have passed, you can now fetch the filtered
values:
$values = $form->getValues();
Don't render the actions in your layout. Just render the forms:
<div class="left">
<h1>Already a member? <br>Then Login!</h1>
<?php
echo new \Ajfit\Form\User\Login(array(
'action' => '/user/login',
'method' => 'post'
));
?>
</div>
<div class="left right">
<h1>Not a member yet? <br>Get Registered!</h1>
<?php
echo new \Ajfit\Form\User\Registration(array(
'action' => '/user/register',
'method' => 'post'
));
?>
</div>
Then, whichever form gets used will post to its own action.

Create a form for uploading images

I want to let users upload images from their drives. Searching around the net, here's what I've found :
The form :
class ImageForm extends BaseForm
{
public function configure()
{
parent::setUp();
$this->setWidget('file', new sfWidgetFormInputFileEditable(
array(
'edit_mode'=>false,
'with_delete' => false,
'file_src' => '',
)
));
$this->setValidator('file', new sfValidatorFile(
array(
'max_size' => 500000,
'mime_types' => 'web_images',
'path' => '/web/uploads/assets',
'required' => true
//'validated_file_class' => 'sfValidatedFileCustom'
)
));
}
}
the action :
public function executeAdd(sfWebRequest $request)
{
$this->form = new ImageForm();
if ($request->isMethod('post'))
if ($this->form->isValid())
{
//...what goes here ?
}
}
the template :
<form action="<?php echo url_for('#images_add') ?>" method="POST" enctype="multipart/data">
<?php echo $form['file']->renderError() ?>
<?php echo $form->render(array('file' => array('class' => 'file'))) ?>
<input type="submit" value="envoyer" />
</form>
Symfony doesn't throw any errors, but nothing is transfered. What am I missing ?
Youre missing an impotant part which is binding the the values to the form:
public function executeAdd(sfWebRequest $request)
{
$this->form = new ImageForm();
if ($request->isMethod('post'))
{
// you need to bind the values and files to the submitted form
$this->form->bind(
$request->getParameter($this->form->getName())
$request->getFiles($this->form->getName())
);
// then check if its valid - if it is valid the validator
// should save the file for you
if ($this->form->isValid())
{
// redirect, render a different view, or set a flash message
}
}
}
However, you want to make sure you set the name format for your form so you can grab a the values and files in the fashion... In your configure method you need to call setNameFormat:
public function configure()
{
// other config code
$this->widgetSchema->setNameFormat('image[%s]');
}
Also in configure you dont need to call parent::setUp()... That is called automatically and is actually what invokes the configure method.
LAstly, you ned to have to correct markup - your emissing the form name from your tag:
<form action="<?php echo url_for('#images_add') ?>" name="<?php echo $form->getName() ?>" method="POST" enctype="multipart/data">
Personally I like to use the form object to generate this as well as it looks cleaner to my eyes:
<?php echo $form->renderFormTag(
url_for('#images_add'),
array('method' => 'post') // any other html attriubutes
) ?>
It will work out the encoding and name attributes based on how youve configured the form.