Zend_Form: when print elements of form in view- form tag dod't created - zend-framework

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;

Related

Form input OR drop menu

Hi I'm trying to make a form where on the name field you can use EITHER text or drop menu (example the drop menu would contain names already in the database whereas the text input would be for new names)
<div class="form-group form-group-sm" style="margin: 1px;">
<label class="col-sm-2 control-label" for="Owner">Owner Name: </label>
<div class="col-sm-4"><input class="form-control" type="text" id="Owner" name="Owner" <?php $q = $opened['Owner']; if(isset($opened['IDNumber'])) { echo "value=$q"; } else { echo "placeholder='Enter Owner Name'";}; ?> required></div>
<div class="col-sm-1">Or Select</div>
<div class="col-sm-4"><select class="form-control" id="Owner" name="Owner" required>
<option>Select</option>
<?php
$sql3="SELECT DISTINCT Owner FROM list Order by Owner asc";
$results = mysqli_query($dbcon,$sql3) or die();
while($row = mysqli_fetch_array($results)) {
echo '<option value="'.$row['Owner'].'">'.htmlspecialchars($row['Owner']).'</option>';
}
?>
</select></div>
</div>
how would i get it to input the data from one OR the other? not both

Render Zend sub form elements in array notation

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.

Using ReCaptcha with my custom form in Joomla

I'm trying to use JFormFieldCaptcha to work on my custom jForm. I managed to get the job done with registration and contact forms. However i want to build my own contact form which is based on an XML file somehow look like this:
<form>
<fieldset addfieldpath="<path to JFormFieldCaptcha class>">
<field
name="captcha" label="Captcha" description="COM_DEZTOUR_ORDER_CAPTCHA_DESC"
type="text" validate="captcha"
/>
</fieldset>
</form>
i cannot figure out why this code not working. Any help would be appriciated!
In order to use Joomla ReCaptcha plugin -
1)Get recaptcha keys from http://www.google.com/recaptcha
2)Set these keys to recaptcha plugin and activate it if it's not.
3) Go to Global Configuration=>Site=>Default Captcha
and set "Default Captcha"=>"Captcha - ReCaptcha"
4)Create xml form instance which has your captcha field
$form =& JForm::getInstance('myform','path/to/form/form.xml');
5)Create fields inside form-
$fieldSets = $form->getFieldsets();
foreach ($fieldSets as $name => $fieldSet) :
?>
<?php
foreach ($form->getFieldset($name) as $field):
?>
<p>
<?php if (!$field->hidden) : ?>
<span class="formlabel"><?php echo $field->label; ?></span>
<?php endif; ?>
<span class="control"><?php echo $field->input; ?></span>
</p>
<?php
endforeach;
?>
<div class="clr"></div>
<?php
endforeach;
6)After form submission validate form-
$post = JRequest::get('post');
jimport( 'joomla.form.form' );
$form =& JForm::getInstance('myform','path/to/form/form.xml');
$res = $form->validate($post);
XML form example-
<?xml version="1.0" encoding="utf-8"?>
<form
addfieldpath="/administrator/components/com_franchise/models/fields">
<fieldset name="information">
<field id="name"
name="name"
type="text"
label="Name"
description=""
class="inputbox"
size="30"
default=""
required="true"
/>
<field
name="captcha"
type="captcha"
label="COM_CONTACT_CAPTCHA_LABEL"
description="COM_CONTACT_CAPTCHA_DESC"
validate="captcha"
/>
</fieldset>
</form>
You can also try this-
How to use joomla recaptcha plugin to my custom Module

Contact page template - redirect to 'thank you page' not working

I'm trying to submit a form via custom page template but the problem is that it only works with form action="<?php the_permalink() ?>" and I need the form to be submitted and redirected to something like this form action="<?php bloginfo('url')?>/message-sent?id=<?php the_ID() ?>"
Full code:
<?php
$emailError = '';
if(isset($_POST['submitted'])) {
$email = trim($_POST['email']);
//setup self email address
$emailTo = $email;
$subject = "[reminder] Don't forget to download " . get_the_title();
$body = "Name: $name \n\nEmail: $email \n\nComments: $comments";
$headers = 'From: Myemail reminders <no-reply#xyz.com>' . "\r\n";
wp_mail($emailTo, $subject, $body, $headers);
$emailSent = true;
} ?>
<section class="box grid_9 list_posts">
<div class="inner">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<div class="entry-content">
<div class="contact-form clearfix">
<?php if(isset($emailSent) && $emailSent == true) { ?>
<div class="thanks">
<?php _e('Thanks, your email was sent successfully.', 'framework') ?>
</div>
<?php } else { ?>
<?php the_content(); ?>
<?php if(isset($hasError) || isset($captchaError)) { ?>
<p class="error"><?php _e('Sorry, an error occured.', 'framework') ?>
<?php } ?>
<form action="<?php the_permalink()?>" id="contactForm" method="post">
<ul class="contactform">
<li>
<input type="email" name="email" id="email" value="<?php if(isset($_POST['email'])) echo $_POST['email'];?>" class="required requiredField email" required="required" />
</li>
<li class="buttons">
<input type="hidden" name="submitted" id="submitted" value="true" />
<input type="submit" value="Remind Me!"></input>
</li>
</ul></form>
<?php } ?></div>
</div>
</div>
<?php endwhile; else: ?>
<div id="post-0" <?php post_class() ?>>
<h1 class="entry-title"><?php _e('Error 404 - Not Found', 'framework') ?></h1>
</div>
<?php endif; ?></div>
</section>
I got no php errors in log, page is redirected successfully, but no email is sent. When using the_permalink, everything works just fine.
When submitting the form data to a different script, make sure the code for (validating the input and) sending the email is in that very file.
Otherwise, your URL /message-sent might rewrite to a completely different script and the script with the above code isn't involved at all once the submit button gets clicked.
Did that held you? Feel free, to ask, if my wording is incomprehensible or if my description isn't clear to you
Maybe you forgot to put ".php" at the end of your /message-sent?id=xxx file, i.e /message-sent.php?id=xxx?
Another thought: It is always a good idea to filter the user input, because you will receive a lot of spam, put some sort of CAPTCHA validation code and sanitize/validate the whole user input text, i.e. every text, which comes from input fields of your form.
For email:
$email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
For name and comments:
$name = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
$comments = filter_var(strip_tags($_POST['comments']), FILTER_SANITIZE_STRING);

accessing request parameters inside paginator partial

1)how do i access the search $keyword inside the paginator partial to create search freindly urls ? clearly, passing the keyword as $this->view->paginator->keyword doesnt work.
2) currently, the search button's name is also send as param . for eg when searching for 'a' the url becomes http://localhost/search/index/search/a/submit//page/2. any way to stop this ?
Search Form:
<form id="search" method="get" action="/search">
<input id="searchfield" onClick="this.value=''" type="text" name="search" value="Search wallpapers"/>
<input id="searchbutton" type="submit" value="" name="submit"/>
</form>
Action inside searchController:
public function indexAction()
{
$keyword=$this->_request->getParam('search');
$alnumFilter=new Zend_Filter_Alnum();
$dataModel=new Model_data();
$adapter=$dataModel->fetchPaginatorAdapter("title LIKE '%".$keyword."%'", '');
$paginator=new Zend_Paginator($adapter);
$paginator->setItemCountPerPage(18);
$page=$this->_request->getParam('page', 1);
$paginator->setCurrentPageNumber($page);
$this->view->paginator=$paginator;
$this->view->keyword=$keyword;
}
index.phtml (view) file:
partialLoop('wallpaper/table-row.phtml',$this->paginator) ;?>
<div id="page-links">
<?= $this->paginationControl($this->paginator,'Sliding','partials/search-pagination-control.phtml');?>
</div>
search-paginator-control.phtml file:
if ($this->pageCount){
$params=Zend_Controller_Front::getInstance()->getRequest()->getParams();
if(isset($this->previous)){
?>
Previous
<?php
}
else{
?> Previous <?php
}
foreach($this->pagesInRange as $page){
if($page !=$this->current){
?>
<?=$page;?>
<?
}
else{
echo $page;
}
}
if(isset($this->next)){
?>
Next
<?php
}
else{
?> Next <?php
}
}
The paginationControl view helper accepts a 4th parameter, that is a array of parameters, so in your case you could do something like this:
<div id="page-links">
<?= $this->paginationControl($this->paginator,'Sliding','partials/search-pagination-control.phtml', array('keyword' => $this->keyword));?>
</div>
Then you access it inside your pagination using $this->keyword.