I try it via my Zend_form:
$output .= $this->_view->render('admin/form.phtml'
, array('id' => $this->getName()
, 'action' => $this->getAction()
, 'method' => $this->getMethod()
, 'enctype' => $this->getEnctype()
, 'data' => array('code' => $code
, 'name' => $name
, 'description' => $description)));
but when i <?php echo $this->enctype; ?> in admin/form.phtml i got nothing.
admin/form.phtml is rendered correctly
Choosing render just displays the output, Zend does not pass your data to the view. But Using view partials, you could achieve that.
From the documentation,
The Partial view helper is used to render a specified template within
its own variable scope. The primary use is for reusable template
fragments with which you do not need to worry about variable name
clashes. Additionally, they allow you to specify partial view scripts
from specific modules.
Related
I've searched the web and have come up with nothing. (Multiple search engines too - I have looked!)
I'm trying to have a text link as the 'form submit' button. Any ideas if this is possible in CakePHP?
Current view code below!
<?php
echo $this->Form->create('trainees', array(
'action' => 'reassign'
));
echo $this->Form->input('emailaddress', array(
'value' => 'scott#something',
'type' => 'hidden',
));
echo $this->Form->submit('Re-Assign Mentor', array(
'class' => 'submit mid',
'before' => '<p>',
'after' => '</p>'
));
echo $this->Form->end();
?>
You need to use the HtmlHelper to output a link. In it's simplest form you use the text you want displayed with the URL that it should link to. In this case it will be JavaScript:
$this->Html->link('Submit Form', 'javascript:document.forms["myform"].submit();');
There are two additional parameters (a $options array and $confirmMessage boolean), but they along with the URL are optional.
You can also call your own JavaScript function if you need to do client side verification and call the submit function from there (also verify on the server as clients can lie).
http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::link
I am developing an application with CakePHP 2.3.2 and I am having some trouble with an input select on a form. I am creating an array, in my Controller, which contains a list of states. In my View I find that when I use this variable in the 'options' field of the input I do not get any select options. If I do a print_r on the variable, in the view, I see exactly what I think I should be seeing for the 'options' field. I have even tried copying this print_r output and putting it in the 'options' field and then the input select works fine.
Here is what I have
In Controller
$options = 'array(1 => \'NSW\',2 => \'ACT\',3 => \'NT\');
$this->set('all_states, $options);
In View
<?php
$options = $all_states;
echo $this->Form->create('Refine', array('url => '/ServiceDirectoryResults/view/refine'));
echo $this->Form->input('field' ,array(
'type' => 'select',
'label' => false,
'options' => $options
));
echo $this->Form->end('Refine Search');
?>
When I run this I see a select with no select options
If I add print_r($options) after the echo $this->Form->end('Refine Search'); I see
array(1 => 'NSW',2 => 'ACT,3 => 'NT')
Which is what I would expect as it is the content of the $options variable which was the $all_states variable passed from the controller. If I take this output from the print_r and replace the $option with it in the input select the select drop down works fine and I see the three options. For some reason I can't work out the select is working fine if I hard code the select options but it will not work if I pass a variable containing the array to the input select.
I would really appreciate if if someone could give me a clue what I am doing wrong here.
Kind Regards
Richard
you might try it like below:
echo $this->Form->input('field', array('type'=>'select','label' => false,
'options' => $options,'default'=>'2'));
to the following HTML being generated:
<option value="2" selected="selected">ACT</option>
option two is shown instead any other one.
Likely issue:
Arrays should not be made as strings like you have:
$options = 'array(1 => \'NSW\',2 => \'ACT\',3 => \'NT\');
Instead, just make an array:
$options = array(1 => 'NSW', 2 => 'ACT', 3 => 'NT');
Other notes:
Why are you setting $options to $all_states only to set it back?
Missing quotes all over - make sure if you start quotes, that you also end them
not good practice to hard-code your URLs (like in your Form->create)
I am learning how to use Zend Framework 2 (2.1.4) forms and running into this error.
Call to a member function insert() on a non-object in ... /Zend/Form/Fieldset.php on line 178
I don't want use the form to automatically connect to a database, in fact I only want to use the form to help validate and will pull from and populate it with an array of values. How do I turn off the database connectivity in the form objects?
I am used to dealing with the ZF1 forms so this new form system is confusing. Once I thought about it though, the way we can use the form elements in our view scripts for formatting is going to be nice. Those old decorators were a pain. Anyway, for me, it would be nice to use the forms without dealing with bound database objects. Is this possible? It just seems so overly complicated to need a model class using InputFilterAwareInterface classes in addition to a simple form. One step at a time though, I can't even get the form to display.
I appreciate any help.
Below are my controller, form, and view scripts:
Form class:
namespace FBWeb\Form;
use Zend\Form\Form;
use Zend\Form\Element;
class ClientForm extends Form
{
public function __construct()
{
$this->setAttribute('method', 'post');
$this->add(array(
'name' => 'client',
'type' => 'Zend\Form\Element\Text',
'options' => array(
'label' => 'Client Name',
),
'attributes' => array(
'type' => 'text',
),
));
$this->add(array(
'name' => 'submit',
'attributes' => array(
'type' => 'submit',
'value' => 'Add'
),
));
}
}
Controller class:
namespace FBWeb\Controller;
use Zend\Debug\Debug;
use Zend\Mvc\MvcEvent;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Session\Container;
use Zend\Http\Request;
use FBWeb\Form\ClientForm;
class ClientController extends AbstractActionController
{
public function indexAction()
{
$clientform = new ClientForm();
return array('form' => $clientform);
}
}
index.phtml view script:
<div id="clientformtable">
<?php
$form = $this->form;
$form->setAttribute('action','/app/client/add');
$form->prepare();
echo $this->form()->openTag($form);
$client = $form->get('client');
echo $this->formRow($client);
echo $this->form()->closeTag();
?>
</div>
This, and similar error messages, happen due to the fact that the form isn't properly set up. As you can see within the code above the __construct() function doesn't call the parents constructor. Therefore the internal "bootstrapping" doesn't happen and the error occurs.
You have to make sure to always call the parents constructor when dealing with Zend\Form\Form and/or Zend\Form\Fieldset.
parent::__construct('client-form');
I have created a custom field sugarfield_ast_rec_link_c.php in custom/Extension/modules/Calls/Ext/Vardefs with such content:
`
<?php
$dictionary['Calls']['fields']['ast_rec_link_c'] = array
('name' => 'ast_rec_link_c',
'vname' => 'LBL_AST_REC_LINK_C',
'type' => 'varchar',
'len' => '255',
'source' => 'non-db',
'function' => array('name'=>'getRecordLink',
'returns'=>'html',
'include'=>'custom/modules/Calls/CustomLogic.php')
);
?>
`
Also aded language file in custom/Extension/modules/Calls/Ext/Language. After quick repair my custom field doesn't appear in Studio -> Calls -> Fields. So I can't put it on views. Can anyone help?
You should change
<?php
$dictionary['Calls']['fields']['ast_rec_link_c'] = array(...);
to
<?php
$dictionary['Call']['fields']['ast_rec_link_c'] = array(...);
Remember you should always use bean name (not module one!) as a $dictionary array key while defining new custom fields.
In my opinion best way to check if everything is OK with your custom vardefs is to compare your own ones with existing in cache/modules/<module_name>/BEAN_NAMEvardefs.php
Zend newbie trying to configure and use Zend_Form.
Just for the record I'm on Zend Framework Version: 1.11.1 on Win XP running Apache 2.something. I'm working on a site which for the most part works just fine. (Somebody else started it. I have to extend it).
I am having trouble in the area of forms and am trying to introduce Zend_Form in the hope that this will somehow simplify matters. But trying to use Zend_Form is presenting problems of it's own.
When I try to instantiate the first test form, I'm getting the white screen of death -- without even an error message.
Data as follows:
Dir Structure:
MYAPPNAME
....controllers
....forms
....models
....services
....views
Bootstrap.php contains:
protected function _initAutoLoading()
{
$loader = new Zend_Loader_Autoloader_Resource(array(
'namespace' => 'MYAPPNAME',
'basePath' => APPLICATION_PATH . '/modules/MYAPPNAME',
));
$loader->addResourceTypes(array(
'model' => array(
'path' => 'models',
'namespace' => 'Model'),
'form' => array(
'path' => 'forms',
'namespace' => 'Form'),
'service' => array(
'path' => 'services',
'namespace' => 'Service')));
}
This works fine for models with names like:
class MYAPPNAME_Model_DataRecordName extends Doctrine_Record
{
etc...
But it seems to be failing miserably for forms ... although mind you, this is my first pass at using Zend_Form.
My form is defined in file MYAPPNAME/forms/Formtest.php:
<?php
class MYAPPNAME_Form_Formtest extends Zend_Form
{
public function init($action){
$this->setAction($action)
->setMethod('post')
->setAttrib('id', 'formtestForm');
$email = $this->addElement( 'text', 'email',
array('label', => 'EMail'));
)
$submit = $this->addElement('submit', 'Submit and Be Free!');
}// End init
} // End class def
The form is being displayed in a view defined as:
<div class=""testForm">
<p style="margin-top:20px; margin-bottom:10px"">Explanatory Text</p>
<h2>This is a Form Test</h2>
<?php echo $this->formResponse; ?>
<?php echo $this->form; ?>
<hr>
<p>FORM ABOVE THIS BAR</p>
</div>
The view works just fine.
It is being managed by an action (in a working controller) defined as:
public function formtestAction(){
echo "formtestAction: ENTERED";
$form = new MYAPPNAME_Form_Formtest('ThisController/formtest2');
//$form = "<p>GARBAGE DATA</p>";
if(!empty($form)){$this->view->form = $form;}
else{
$form = "<p>THE FORM VAR IS EMPTY</p>";
$this->view->form = $form;
$formResponse = "<p>INSTANTIATION FAILED</p>";
$this->view->formResponse = $formResponse;
}
}
public function formtest2Action(){
echo "formtest2Action: ENTERED";
}
If I comment out both the form instantiation and the garbage data lines, I get valid output in the view. If I set $form to "GARBAGE DATA" I also get valid predictable output.
However when I try to instantiate the form object I get the white screen of death containing only "formtestAction: ENTERED" (from the echo statement at the top.)
I am going slowly mad.
I can't figure out if this is an autoloader problem, a routing problem, an object instantiation problem, or what.
I'd be very much obliged for any advice.
Thanks for reading.
With Zends, I've run into that several times, and it usually is something annoying as a superflous comma. In
... 'basePath' => APPLICATION_PATH . '/modules/MYAPPNAME',));
it looks just like on of those. Only a quick look, but you might check it anyway.
HTH,
Marcus
mtoepper: Very close. Good catch!
It was indeed an extra comma, only it was in the Form class definition -- preventing successful object instantiation.
These silent failures are VERY annoying.