I am making a sub form in Zend with the following code:
class Admin_Form_StudentAdmission extends Zend_Form
{
public function init()
{
$this->setMethod('post');
$personalDetailsForm = new Zend_Form_SubForm();
$personalDetailsForm->setIsArray(true);
$student_first_name = $this->CreateElement('text','first_name')
->setAttribs(array('placeholder'=>'First Name', 'mendatory'=>'true'))
->setRequired(true)
->addValidators(array(
array('NotEmpty', true, array('messages' => 'Please enter First Name')),
array('stringLength',true,array(2, 10, 'messages'=> 'First Name should be 2 to 10 characters long.')),
))
->addFilter(new Zend_Filter_StringTrim())
->setDecorators(array( array('ViewHelper')
));
$personalDetailsForm->addElement($student_first_name);
$this->addSubForm($personalDetailsForm, 'student_personal_details');
}
}
And now I am rendering this form with below php code:
$personalDetailsForm = $this->form->getSubForm('student_personal_details');
echo $personalDetailsForm->first_name;
But this renders the element as
<input type="text" mendatory="true" placeholder="First Name" value="" id="first_name" name="first_name">
While I want this as below
<input type="text" mendatory="true" placeholder="First Name" value="" id="student_personal_details-first_name" name="student_personal_details[first_name]">
What I'm doing wrong here?
Just use zend MVC pattern, it will work like a charm,
result:
<input type="text" name="student_personal_details[first_name]" id="student_personal_details-first_name" value="" placeholder="First Name" mendatory="true">
Pass the the form as $personalDetailsForm from namecontroller.php action viewfromAction() to viewfrom.phtml like:
$this->view->form = $personalDetailsForm;
Then echo your from in the viewfrom.phtml simply like:
<?php echo $this->form ?>
Zend 1.12 MVC:
[http://framework.zend.com/manual/1.12/en/learning.quickstart.intro.html]
The problem looks like, you used a direct echo $personalDetailsForm->first_name; thats didn't generate the proper html form element tags.
Related
The Bootstrap Example Code
http://getbootstrap.com/css/#forms
Copying a simple email input element from getbootstrap.com suggests we format the HTML in the following way:
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input id="exampleInputEmail1" class="form-control" type="email" placeholder="Email">
</div>
Above we have a <label> tag that closes straight after it's text content, "Email address".
I would now like to create the same form group using Zend Framework.
module/MyApp/src/MyModule/Form/MyForm.php
namespace MyModule\Form;
use Zend\Form\Form;
class MyModuleForm extends Form {
public function __construct($name = null)
{
$this->add(array(
'name' => 'email_address',
'type' => 'Email',
'options' => array(
'label' => 'Email address',
),
'attributes' => array(
'class' => 'form-control',
'placeholder' => 'Email'
)
));
The Zend Framework generated code
<div class="form-group">
<label>
<span>Email address</span>
<input class="form-control" type="email" placeholder="Email" id="exampleInputEmail1">
</label>
</div>
In the above HTML you can see that Zend has not closed the <label> tag. Instead the <label> ecapsulates its children.
How do I change the way the Zend rendering works?
I assume you are using ZF2 FormRow view helper to render your form element in your view script, e.g. $this->formRow($this->form->get('email_address'));
To render it differently you need to use the following view helpers
FormLabel
FormText
FormElementErrors
If for example you wanted to render as a definition list you would use something like
<dl class="zend_form">
<dt><?php echo $this->formLabel($this->form->get('email_address')); ?></dt>
<dd><?php echo $this->formText($this->form->get('email_address')); ?>
<?php echo $this->formElementErrors($this->form->get('email_address')); ?></dd>
</dl>
I hope this points you in the right direction
I wana create a front-end joomla component and this is my first experience.
here is important things:
1-component/controller.php
class TestController extends JControllerLegacy
{
public function display($cachable = false, $urlparams = false)
{
$view= JFactory::getApplication()->input->getCmd('view','items');
JFactory::getApplication()->input->set('view', $view);
parent::display($cachable, $urlparams);
}
}
2: com_test/model/items.php
<?php
defined('_JEXEC') or die();
jimport( 'joomla.application.component.modellist' );
class TestModelItems extends JModelList
{
public function __construct($config = array())
{
if (empty($config['filter_fields']))
$config['filter_fields'] = array('id', 'title', 'catid');
parent::__construct($config);
}
function getListQuery()
{
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select(...)
return $query;
}
}
I can print the query result on default.php on view folder!
but I wana another thing.
I have a form like this in the front page of my site in a custom module:
<form action="" method="post">
<input type="text" name="wordsearch" value="search">
.
.
<input type="submit" />
</form>
Now!
I do not know how can I send this form (with post method) to getListQuery() function in model folder...how can do it?
i wana when sb click submit form, the component filter query sql according to values of form and then show new result to user!
i googled for hourse but no chance to solve. thanks for your help.
You can submit Form from module to component as follows.
Suppose your component name is com_helloworld The in your module form should have the following things.
<form action="" method="post">
<input type="text" name="wordsearch" value="search">
.
.
<input type="hidden" name="option" value="com_helloworld" />
<input type="hidden" name="view" value="yourview" />
<input type="hidden" name="task" value="my_controller_fun" />
<input type="hidden" value="your_controller_file_name" name="controller">
<input type="submit" />
</form>
In this example your controller file should have my_controller_fun method from controller to model you can use regular method. This methods will get all the form data in your controller , then you can pass that to model.
Detailed :
In your controller file.
function my_controller_fun(){
$post_array = $_POST;
$model = $this->getModel('Profile', 'UsersModel');//example for including profile model you can specify your model file name
$model->function_inyourmodel($post_array);//this function should be in model
}
Hope its help..
i generate radio buttons using Yii
the code looks like
<span class="gender">
<?php echo CHtml::activeRadioButton($model,'gender',array('value' => 'male')); ?>
</span>
<span class="gender">
<?php echo CHtml::activeRadioButton($model,'gender',array('value' => 'female')); ?>
</span>
It's generate next HTML code
<span class="gender">
<input id="ytweb\models\register_gender" type="hidden" value="0" name="web\models\register[gender]">
<input value="male" class="male" name="web\models\register[gender]"id="web\models\register_gender" type="radio"></span>
<span class="gender">
<input id="ytweb\models\register_gender" type="hidden" value="0" name="web\models\register[gender]">
<input value="female" class="female" name="web\models\register[gender]" id="web\models\register_gender" type="radio">
</span>
when i get POST from this form, if i checked female it returns female, but if i cheked male it returns 0. I think it is because of identical inputs ids. But how can i avoid it?
You can configure activeRadioButton to not output the "guard input" with value 0. Do this by passing the parameter 'uncheckValue' => null as part of your options array:
echo CHtml::activeRadioButton($model,'gender',
array('value' => 'male', 'uncheckValue' => null));
echo CHtml::activeRadioButton($model,'gender',
array('value' => 'female', 'uncheckValue' => null));
That said, using activeRadioButtonList is the best choice here. You can configure its template parameter to specify your custom HTML:
$genders = array('male' => 'male', 'female' => 'female');
$options = array(
'template' => '<span class="gender">{input</span>',
'uncheckValue' => null,
);
echo CHtml::activeRadioButtonList($model, 'genger', $genders, $options);
I am having some problems working out how to use custom forms in Zend Framework.
I have followed various guides but none seem to work. Nothing at all gets rendered.
Here is the bits of code that I am trying to use (All code below is in the default module). I have simplified the code to a single input for the test.
applications/forms/One/Nametest.php
class Application_Form_One_Nametest extends Zend_Form {
public function init() {
$this->setMethod('post');
$name = new Zend_Form_Element_Text('name');
$name->setLabel('Box Name')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');
$submit = new Zend_Form_Element_Submit('submit');
$submit->setLabel('Submit Message');
$submit->setAttrib('id', 'submitbutton');
$submit->setAttrib('class', 'bluebutton');
$this->addElements(array($name, $submit));
}
}
application/views/scripts/one/formlayout.phtml
<form action="<?= $this->escape($this->form->getAction()) ?>" method="<?= $this->escape($this->form->getMethod()) ?>">
<p>
Please provide us the following information so we can know more about
you.
</p>
<? echo $this->element->name ?>
<? echo $this->element->submit ?>
</form>
application/controllers/IndexController.php
public function formtestAction() {
$form = new Application_Form_One_Nametest();
$form->setDecorators(array(array('ViewScript', array('viewScript' => 'one/formlayout.phtml'))));
$this->view->form = $form;
}
application/views/scripts/index/formtest.phtml
<h1>Formtest</h1>
<?
echo $this->form;
?>
The above code does not throw any errors or render any part of formlayout.phtml including the form tags or text between the p tags.
Can anybody tell me what I might be doing wrong?
I think the problem is your form element's decorator. You should set the decorator to ViewHelper and Error only. It works for me at least.
Here is the code I used and it should work
applications/forms/Form.php
class Application_Form_Form extends Zend_Form {
public function loadDefaultDecorators() {
$this->setDecorators(
array(
array(
'ViewScript',
array(
'viewScript' => 'index/formlayout.phtml',
)
)
)
);
}
public function init() {
$this->setAction('/action');
$this->setMethod('post');
$this->addElement('text', 'name', array(
'decorators' => array('ViewHelper', 'Errors')
));
}
}
application/views/scripts/index/formlayout.phtml
<form action="<?php echo $this->element->getAction(); ?>" method="<?php echo $this->element->getMethod(); ?>">
<div>
<label for="name">Box Name</label>
<?php echo $this->element->name; ?>
</div>
<input type="submit" value="Submit Message" id="submitbutton" class="bluebutton">
</form>
application/views/scripts/index/index.phtml
<!-- application/views/scripts/index/index.phtml -->
<?php echo $this -> form; ?>
application/controllers/IndexController.php
public function indexAction() {
$form = new Application_Form_Form();
$this -> view -> form = $form;
}
Here is a very simple example to get you going adapted from this article.
The form:-
class Application_Form_Test extends Zend_Form
{
public function init()
{
$this->setMethod('POST');
$this->setAction('/');
$text = new Zend_Form_Element_Text('testText');
$submit = new Zend_Form_Element_Submit('submit');
$this->setDecorators(
array(
array('ViewScript', array('viewScript' => '_form_test.phtml'))
)
);
$this->addElements(array($text, $submit));
$this->setElementDecorators(array('ViewHelper'));
}
}
The order in which setDecorators(), addElements() and setElementDecorators() are called is very important here.
The view script _form_test.phtml can be called anything you like, but it needs to be in /views/scripts so that it can be found by the renderer.
/views/scripts/_form_test.phtml would look something like this:-
<form id="contact" action="<?php echo $this->element->getAction(); ?>"
method="<?php echo $this->element->getMethod(); ?>">
<p>
Text<br />
<?php echo $this->element->testText; ?>
</p>
<p>
<?php echo $this->element->submit ?>
</p>
</form>
You instantiate the form, pass it to the view and render it as usual. The output from this example looks like this:-
<form id='contact' action='/' method='post'>
<p>
Text<br />
<input type="text" name="testText" id="testText" value=""></p>
<p>
<input type="submit" name="submit" id="submit" value="submit"></p>
</form>
That should be enough to get you started creating your own forms.
Usually, if you don't see anything on the screen it means that some sort of error happened.
Maybe you have errors turned off or something, maybe not. I'm just trying to give you ideas.
The only things I could spot where the following.
In the below code, you have to still specify the form when trying to print out the elements.
<form>
action="<?php $this->escape($this->element->getAction()) ?>"
method="<?php $this->escape($this->element->getMethod()) ?>" >
<p>
Please provide us the following information so we can know more about
you.
</p>
<?php echo $this->element->getElement( 'name' ); ?>
<?php echo $this->element->getElement( 'submit' ) ?>
</form>
As vascowhite's code shows, once you are inside the viewscript, the variable with the form is called element. The viewscript decorator uses a partial to do the rendering and thus it creates its own scope within the viewscript with different variable names.
So, although in your original view it was called $form, in the viewscript you'll have to call it element.
Also, maybe it was copy/paste haste, but you used <? ?> tags instead of <?= ?> or <?php ?> tags. Maybe that caused some error that is beyond parsing and that's why you got no output.
Problem:
When print elements of form in view, form tag don't created
My View:
<?php
/****** print elements and inser label:: have to be done in this way for integrate cushycms ********/
echo $this->form->empty;
?>
<label>Ad Title</label>
<?php
echo $this->form->adtitle;
?>
<label></label>
<?php echo $this->form->adbody; ?>
MY Form (part of the code):
class MyForm extends Zend_Form
{
function init(){
$empty = new Zend_Form_Element_Hidden("empty");
$empty->removeDecorator('Label');
$title = new Zend_Form_Element('adtitle');
$title->removeDecorator('Label');
$title//->setLabel('Ad Title')
->setRequired('true')
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty')
->setAttrib('MAXLENGTH',100)
->setAttrib('Size',106);
$title->getValidator('NotEmpty')
->setMessage('Company Name can not be empty');
$body = new Zend_Form_Element_Textarea('adbody');
$body->removeDecorator('Label');
}
}
The html that I get (form tag not exist):
<dd id="empty-element">
<input type="hidden" name="empty" value="" id="empty"></dd> <label>Ad Title</label>
<dd id="adtitle-element">
<input type="text" name="adtitle" id="adtitle" value="" MAXLENGTH="100" Size="106"></dd><label></label>
<dd id="adbody-element">
<textarea name="adbody" id="adbody" onKeyDown="javascript:limitText(this.form.countdown,400)" onKeyUp="javascript:limitText(this.form.countdown,400)" rows="24" cols="80"></textarea></dd> <label>chras left (maximum 400): </label>
Thank you very much
I think you have to add the form tag by your self.
<form action="<?= $this->escape($this->form->getAction() ?>"
method="<?= $this->escape($this->form->getMethod() ?>"
>
Or use
echo $this->form;