I create login form andd add in UserController->loginAction:
public function loginAction() {
$form = new Application_Form_Login();
$this->view->loginForm = $form;
}
How to add my form in layouts/scripts/header.phtml, because I try this, but not work:
<?php echo $this->loginForm ?>
If I echo form in views/scripts/user/login.phtml I see login form.
That is my login form:
class Application_Form_Login extends Zend_Form {
public function init() {
$this->setDecorators(array('FormElements', 'Form'))
->setAction("/user/login/");
$username = new Zend_Form_Element_Text(array('name' => 'username', 'class' => 'input-text'));
$username->setRequired(true)
->setDecorators(array('ViewHelper',));
$this->addElements(array($username));
}
}
You are looking for the render function:
<?php
echo $this->loginForm->render();
?>
Since a layout is separate from your view object, you need to assign the form to the layout.
In your controller:
public function loginAction()
{
$form = new Application_Form_Login();
// assign the form to the layout
$this->_helper->layout()->loginForm = $form;
}
In your layout:
<?php if ($this->layout()->loginForm): ?>
<?php echo $this->layout()->loginForm; ?>
<?php endif; ?>
Try this instead:
// in controller:
$form = new Application_Form_Login();
$this->view->placeholder('loginForm')->set($form);
---------------
// in layout script:
$form = $this->placeholder('loginForm');
if ($form instanceof Zend_Form) {
echo $form;
}
Related
I apologise if this is already exists but i could not get it to work..
I need to show the names and store the id's in my table. these name are coming from other model(table). i'm getting the names and id's when i inspect elts in chrome network inspector..
but values are not not visible..but they are populating when i try to click on them its coming up in alert with id..
can anyone pls help me to get names visible --thanks for ur help
my _form.php is something like this:
<div class="row">
<?php echo $form->labelEx($typeModel,'benefit_type'); ?>
<?php
$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
'attribute'=>'name',
'model'=>$typeModel,
'sourceUrl'=>array('benefit/benefit_type_list'),
'value'=>'Please select',
'name'=>'name',
'id'=>'id',
'options'=>array(
'minLength'=>'0',
'select'=>"js:function(event, ui) {
alert(ui.item.id);
// $('#organisation_id').val(ui.item.id);
}",
),
'htmlOptions'=>array(
'id'=>'id',
'size'=>45,
'maxlength'=>45,
),
)); ?>
<?php echo $form->error($typeModel,'benefit_type'); ?>
extension class for getting the names and id's is:
<? class EAutoCompleteAction extends CAction
{
public $model;
public $attribute;
public $id;
private $results = array();
public $returnVal = '';
public function run()
{
if(isset($this->model) && isset($this->attribute)) {
$criteria = new CDbCriteria();
$criteria->compare($this->attribute, $_GET['term'], true);
$model = new $this->model;
foreach($model->findAll($criteria) as $m)
{
// $this->results[] = $m->{$this->attribute};
//$this->results[] = $m-<{$this->id};
$this->results[] = array(
'name' => $m->{$this->attribute},
'id'=> $m->id
);
/* $this->returnVal .= $m->getAttribute('name').'|'
.$m->getAttribute('id'). "\n"; */
}
}
echo CJSON::encode($this->results);
}
}
?>
and in my controller:
public function actions()
{
return array(
'benefit_type_list'=>array(
'class'=>'application.extensions.EAutoCompleteAction',
'model'=>'BenefitType', //My model's class name
'attribute'=>'name', //The attribute of the model i will search
),
);
}
It's been a while since I used this extension but it uses Jquery's auto complete, and the dataset from EAutoCompleteAction probably needs:
$this->results[] = array(
'label' => $m->{$this->attribute},
'value'=> $m->id,
'id'=> $m->id
);
Source: yii forums
I want to prefill form fields in symfony2. The URL looks like this
http://localhost/Symfony/web/app_dev.php/clearance/new?projectId=6
I want now to set projectId in the form to 6.
Here is my controller code
public function newclearanceAction(){
$request = $this->getRequest();
$id = $request->query->get('projectId');
echo $id; //this works, but how to send it to the form?????
$clearance = new Clearance();
$form = $this->createForm(new ClearanceType(), $clearance);
if ($request->getMethod() == 'POST'){
$form->bindRequest($request);
if($form->isValid()) {
$em = $this->getDoctrine()->getEntityManager();
$em->persist($clearance);
$em->flush();
return $this->redirect($this->generateUrl('MyReportBundle_project_list'));
}
}
return $this->render('MyReportBundle:Clearance:new.html.twig',array('form'=>$form->createView()));
And here is the code for the form view
<form action="{{ path('MyReportBundle_clearance_new') }}" method="post" >
{{ form_errors(form) }}
{{ form_rest(form) }}
<input type="submit" />
</form>
Thanks for any help!
This depends on whether your clearance entity has a project related to it. If it does you can do something like:
$request = $this->getRequest();
$id = $request->query->get('projectId');
$em = $this->getDoctrine()->getEntityManager();
$project = $em->getRepository("MyReportBundle:Project")->find($id)
$clearance = new Clearance();
$clearance->setProject($project);
$form = $this->createForm(new ClearanceType(), $clearance);
This will set the project on the clearance object and pass it through to the form.
Currently you cannot do a hidden field for an entity in Symfony2 so my current fix is to create a query builder instance and pass it to the form so that the form select for projects does not get ridiculous when you have 100's of projects. To do this in the action I add:
$request = $this->getRequest();
$id = $request->query->get('projectId');
$em = $this->getDoctrine()->getEntityManager();
$repo = $em->getRepository("MyReportBundle:Project");
$project = $repo->find($id)
//create the query builder
$query_builder = $repo->createQueryBuilder('p')
->where('p.id = :id')
->setParameter('id', $project->getId());
$clearance = new Clearance();
$clearance->setProject($project);
//pass it through
$form = $this->createForm(new ClearanceType($query_builder), $clearance);
and in the form class:
protected $query_builder;
public function __construct($query_builder)
{
$this->query_builder = $query_builder;
}
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('Your field')
// all other fields
// Then below the query builder to limit to one project
->add('project', 'entity', array(
'class' => 'MyReportBundle:Project',
'query_builder' => $this->query_builder
))
;
}
At the moment I have a page where I have retrieved information on a club by the id of that club. I now have a comments box where I want to retrieve the comments about that club, in the comments table I have the club_id and the parameter "club_id" is passed into this page. At the moment I am retrieving all of the comments from the table but I want just the comments for that club. A point in the right direction would be great!
Controller:
class ClubDescriptionController extends Zend_Controller_Action
{
public $auth = null;
public function init()
{
$this->auth=Zend_Auth::getInstance();
}
http://pastebin.com/m66Sg26x
protected function authoriseUser()
{
if (!$this->auth->hasIdentity()) {
$route = array('controller'=>'auth', 'action'=>'index');
$this->_helper->redirector->gotoRoute($route);
}
}
}
Model:
class Application_Model_DbTable_Comments extends Zend_Db_Table_Abstract
{
protected $_name = 'comments';
public function getComment($id) {
$id = (int) $id;
$row = $this->fetchRow('id = ' . $id);
if (!$row) {
throw new Exception("Count not find row $id");
}
return $row->toArray();
}
public function addComment($comment, $club_id) {
$data = array(
'comment' => $comment,
'club_id' => $club_id,
'comment_date' => new Zend_Db_Expr('NOW()'),
);
$this->insert($data);
}
public function deleteComment($id) {
$this->delete('id =' . (int) $id);
}
}
The view:
<div id="view-comments">
<?php foreach($this->comments as $comments) : ?>
<p id="individual-comment">
<?php echo $this->escape($comments->comment);?> -
<i><?php echo $this->escape($comments->comment_date);?></i>
</p>
<?php endforeach; ?>
</div>
I realise I am going to have to use the getComment(); function in my model and query it by the id but I'm getting confused on exactly how...
Thanks
It's been a while since I used Db_Table but I think you want to create a select object, which allows you to build a query that will select comments with the correct club_id:
$comments = new Application_Model_DbTable_Comments();
$select = $comments->select();
$select->where('club_id = ?', $id);
$this->view->comments = $comments->fetchAll($select);
you may want to order the comments by date, if so, you can do this by adding an order clause to the select:
$select->order('comment_date ASC');
take a look at the docs for Zend_Db_Table_Select, which has quite a few examples: http://framework.zend.com/manual/en/zend.db.table.html#zend.db.table.fetch-all
In your controller you are calling
$this->view->comments = $comments->fetchAll();
it should be
$this->view->comments = $comments->getComment($this->_request->getParam('club_id'));
where id variable will be fetched from url.
Here is the working controller:
public function indexAction() {
//authorisation
$this->authoriseUser();
//to get the paramter club_id to query for specific club information
$id = (int) $this->_request->getParam('club_id', 0);
//submit a comment
$form = new Application_Form_Comment();
$form->submit->setLabel('Comment');
$this->view->form = $form;
if ($this->getRequest()->isPost()) {
$formData = $this->getRequest()->getPost();
if ($form->isValid($formData)) {
$comment = new Application_Model_DbTable_Comments();
$comment->addComment($formData['comment'], $id);
} else {
$form->populate($formData);
}
}
//initialise table
$clubs = new Application_Model_DbTable_Clubs();
$clubs = $clubs->getClub($id);
$this->view->clubs = $clubs;
//to get the comments for the club
$comments = new Application_Model_DbTable_Comments();
$select = $comments->select();
$select->where('club_id = ?', $id);
$select->order('comment_date ASC');
$this->view->comments = $comments->fetchAll($select);
}
I have a view helper method which is like this
class Zend_View_Helper_LoginForm extends Zend_View_Helper_Abstract
{
public function loginForm()
{
$script = "<script type='text/javascript'>(function (){ $('#submit').click(function (){alert('hello'); return false;})})</script>";
$this->view->headScript()->appendScript($script, $type = 'text/javascript');
$login = new Application_Form_User();
return $login;
}
}
But this is not working. I also tried
$this->view->headScript()->appendFile($this->view->baseUrl('/js/jquery.js'), 'text/javascript');
but this is not working either. If i try this code in layout.phtml then it works.Any Idea?
In view file:
<?php $this->headScript()->appendFile('your/sript/file.js') ?>
In your layout:
<?php echo $this->headScript() ?>
You have to add setView method:
class My_View_Helper_ScriptPath
{
public $view;
public function setView(Zend_View_Interface $view)
{
$this->view = $view;
}
public function scriptPath($script)
{
return $this->view->getScriptPath($script);
}
}
For example i have echo $this->escape($this->test); in index.phtml and in controller $this->view->test = 'test message';, but i want to do this from bootstrap, becouse i want to show Form in every page (controller).
protected function _initView()
{
$this->view = new Zend_View();
$this->view->test = 'test message';
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
$viewRenderer->setView($this->view);
}
But I would recommend doing this in a controller plugin, not during the bootstrap:
<?php
class My_Controller_Plugin_AddSomethingToViewInAllControllerActions extends Zend_Controller_Plugin_Abstract
{
public function preDispatch()
{
$viewRenderer = Zend_Controller_Action_HelperBroker::getExistingHelper('ViewRenderer');
$viewRenderer->initView();
$view = $viewRenderer->view;
$view->test = 'test message';
}
}
sorry i made it
$view = new Zend_View;
$view->setBasePath(APPLICATION_PATH . "/views");
$view->arr = 'message';
echo $view->render('test.php');