Creating a form view in Zend - php

Is there a reliable way to create a custom view for a Zend_Form? The decorators are pretty cryptic and using them in fancy ways sometimes is so complicated that I'd prefer to just write the HTML by hand. Is there a way to do it and still make the form fully cooperate with the controller (eg. call $form->isValid() and expect everything to validate properly)? If so, are there any caveats to look out for (like taking care about validation errors display)?
The ideal solution would be to create a form and pass the elements array (containing the necessary data like names, IDs, input types and all needed to render the HTML) - does Zend Framework permit this?
[EDIT]
Instead of just echoing the form object, I have tried adding this code in the view (a simple login form):
<?php
$userid = $this->form->getElement('userid');
$pass = $this->form->getElement('password');
$remember = $this->form->getElement('remember');
$submit = $this->form->getElement('submit');
?>
<form enctype="<?php echo $this->form->getEnctype(); ?>" method="<?php echo $this->form->getMethod(); ?>" action="<?php echo $this->form->getAction(); ?>" id="<?php echo $this->form->getId(); ?>">
name: <input type="text" id="<?php echo $userid->getId(); ?>" name="<?php echo $userid->getName(); ?>" /><br />
pass: <input type="password" id="<?php echo $pass->getId(); ?>" name="<?php echo $pass->getName(); ?>" /><br />
remember: <input type="checkbox" id="<?php echo $remember->getId(); ?>" name="<?php echo $remember->getName(); ?>" /><br />
submit: <input type="submit" id="<?php echo $submit->getId(); ?>" name="<?php echo $submit->getName(); ?>" value="<?php echo $submit->getValue(); ?>" />
</form>
The form seems to work OK and validate (although I don't get redirected the the page I came from - but that's a different problem, I believe, as I pass that via GET, not in the form). Is that acceptable, or am I doing something horridly wrong without knowing it?

A lot depends of the design and the final layout.
Where do you want to display validation errors? How? Via Error decorator?
Do you want also use descriptions?
Do you need to use other decorators?
The best way seems to be to create just your own Zend_Form_Element_ThreadIcons element. This is as easy as subclassing one of Zend_Form elements and implementing custom _render() method returning HTML you need. You may even use your own View instance there.
Then you may pass array of icons as an element option and handle it the way you need.
If you decide using decorators, you my find this presentation very useful to master the technique:
http://www.slideshare.net/weierophinney/leveraging-zendform-decorators

Related

Is it possible to pass a variable to 'get_search_form()' in Wordpress?

I've tried to read the official docs but I can't find this information.
I'm developing a custom Wordpress theme, and I have created a search form in the searchform.php file, which is called by using the get_search_form() template tag.
I'm using the same form on multiple locations on my theme; it has the same design, the same fields, and so on, it should only differ for a single value that I wish to pass as a variable when calling it with get_search_form().
Is it possible to do so? And if not possible, what could be the best way to accomplish this, without having to duplicate a whole .php file just to have a single value to be different?
I think you could pass optional arg (preferably an array) to get_search_form() like so
get_search_form($options).
Inside the function you could do something like:
Seems that the only reasonable thing to do is to hook to the function. Check out this post: https://developer.wordpress.org/reference/functions/get_search_form/
or else you could create a $_SESSION variable or a cookie where you would store desired value. Depending on location, the value would change and you would end up with a sample form looking something like this:
<form action="/" method="get">
<label for="search">Search in <?php echo home_url( '/' ); ?></label>
<input type="text" name="s" id="search" value="<?php the_search_query(); ?>" />
<input type="image" alt="Search" src="<?php bloginfo( 'template_url' ); ?>/images/search.png" />
<?php if(isset($_SESSION['your_value'])) : ?>
<input type="hidden" id="additional_parameter" value="<?php echo $_SESSION['your_value']; ?>">
<?php endif; ?>
</form>

MSSQL: multiple INSERTs with PHP arrays and echoing back the data

I have a form with the following structure:
<input type="text" name="projNo[1]" id="projNo[1]" value="<?php echo $row['ProjNo'
[1];>"
/>
<input type="text" name="projBudget[1]" id="projBudget[1]" value="<?php echo
$row['ProjBudget'][1]; ?>" />
<input type="text" name="projDateFrom[1]" id="projDateFrom[1]" value="<?php echo
$row['ProjDateFrom'][1]; ?>" />
<input type="text" name="projDateTo[1]" id="projDateTo[1]" value="<?php echo
$row['ProjDateTo'][1]; ?>" />
<input type="text" name="projNo[2]" id="projNo[2]" value="<?php echo $row['ProjNo'
[2];>"
/>
<input type="text" name="projBudget[2]" id="projBudget[2]" value="<?php echo
$row['ProjBudget'][2]; ?>" />
<input type="text" name="projDateFrom[2]" id="projDateFrom[2]" value="<?php echo
$row['ProjDateFrom'][2]; ?>" />
<input type="text" name="projDateTo[2]" id="projDateTo[2]" value="<?php echo
$row['ProjDateTo'][2]; ?>" />
There are two more groups like this with indexes 3 and 4. Upon submit, four separate records must be created in the DB if the user has filled in all four lines. My question is twofold: How would I structure my query to accomplish this? And: Have I set up my code correctly? When the form is loaded, I would like the correct output to be displayed. I've never been confronted with a request like this before, so I'm flying a bit blind.
Use PDO and prepared statements. Prepare a statement like this:
$s = $db->prepare('INSERT INTO PROJECT (no, budget, from, to) VALUES (?,?,?,?)')
Execute the statement for each set like this:
for ($i = 1; $i<=4; $i++) {
$s->execute(array($projNo[$i], $projBudget[$i], $projDateFrom[$i], $projDateTo[$i]));
}
(You need to add error checking and validation. This includes something that loads the stuff from $_POST to the arrays I used in the above example.)
Currently, you have a XSS security issue in your code. You cannot just echo stuff that comes from the user - you need to escape it. If you are putting it inside HTML, including double-quoted attribute values like in your case, use echo htmlspecialchars($_GET[...]);.
You may want to create a "htmlout" function that does nothing else than echo htmlspecialchars, just to have a nicer, easier-to-write name for it, and use it everywhere. That way, you can search your code for instances of "echo", and unless you have protected them otherwise, this indicates you probably need to add some escaping.

Sending PHP variables back and forth between pages

I've created a registration form that successfully passes its variables from the registration page (go-gold.php) to a summary/verfication page (go-gold-summary.php). The data appears correctly on the second page.
However, I want to able to use an image button to return back to the registration page, in case the user made an entry error. Going back, the original form should now be populated with the data that was first entered.
The problem is that I cannot re-send/return the data from the second page, back to the first. My text fields appear blank. I do NOT want to use Session variables.
The code is truncated from the entire page.
Registration Page (go-gold.php):
<?php
$customer_name = $_POST['customer_name'];
?>
<form action="go-gold-summary.php" method="post">
Name: <input type="text" name="customer_name" id="customer_name" value= "<?php echo $customer_name ?>" />
<input name="<?php echo $customer_name ?>" type="hidden" id="<?php echo $customer_name ?>">
</form>
Summary Page (go-gold-summary.php)
<?php
$customer_name = $_POST['customer_name'];
?>
<form action="go-gold.php" method="post">
Name: <?php echo $customer_name ?> <input type="hidden" id="<?php echo $customer_name ?>" name="<?php echo $customer_name ?>">
<INPUT TYPE="image" src="images/arrow_back.png" id="arrow" alt="Back to Registration"> (Button to go back to Registration Page)
</form>
Thanks!
go-gold-summary.php should be changed like this.
<?php
$customer_name = $_POST['customer_name'];
?>
<form action="go-gold.php" method="post">
Name: <?php echo $customer_name ?> <input type="hidden" value="<?php echo $customer_name ?>" name="customer_name">
<INPUT TYPE="submit" src="images/arrow_back.png" id="arrow" alt="Back to Registration"> (Button to go back to Registration Page)
</form>
notice how I've changed this line
<input type="hidden" id="<?php echo $customer_name ?>" name="<?php echo $customer_name ?>">
into this
<input type="hidden" value="<?php echo $customer_name ?>" name="customer_name">
$_POST is an associative array and as you submit the form it will be populated like this:
$_POST["index"] = value;
where "index" is the text field "name" and value is the text field value.
You've missed that one in your code. Just update it with my code and it will work
Why you would not want to use the php session? Please give any reason for not to use it. I am asking this way since my reputation does not allow me to comment questions or answers any other than my own. Plese do not -1 for this.
Another way could be using cookies to store the data temporarily, but that and posting the data back and forth in the post request is really insecure compared to session.
there are very few ways to maintain variables across pages. The alternative is to have separate form on the second page with hidden text fields containing the $_POST data, and the submit button calls the previous page. No way of getting around the "back button" on a browser though unfortunately.
I missed the bold text about the session variables - disregard if this does not apply:
one way to maintain variables across pages on the server side is to use $_SESSION
first include the following at the top of your PHP pages to keep a session active:
session_start();
once you submit the for and move to page 2, add the following:
$_SESSION['customer_name'] = $_POST['customer_name'];
As well, on the first page, you could change the form element as such:
<input type="text" name="customer_name" value="<?PHP if isset($_SESSION['customer_name'] || !empty($_SESSION['customer_name'])) { echo $_SESSION['customer_name']; } ?>">
this will keep the filled in data and display it when the user returns tot he page, and if they put in something different it will be updated when they hit page 2 again.

Return an php array to jquery ajax and populating a list. With some if()'s

Trying to make an ajax driven, sortable contact list.
A menu with 3 alternatives should upon click render a new list with updated data.
Been searching around a bit but havn't found any good info on this (as from what I understand atleast..)
This is what populates my list at the moment. I know, bad choice of variables..
<?php
if($c['ischeckedin'])
{
$checkedin='Check Out';
$checked='checkedin';}
else{
$checkedin='Check In';
$checked='';}
?>
<li id="<?php print $c['id']; ?>" class="clearfix gradient1 <?php print $checked; ?>">
<?php print $c['name']; ?>
<img src="options.png" class="options rounded3" alt="options" width="16" height="16" />
<?php if($c['ischeckedin']): ?>
<input
type="button"
class="button checkerout"
value="<?php print $checkedin; ?>"
name="<?php print $c['id']; ?>" />
<?php else: ?>
<input
type="button"
class="button checkerin"
value="<?php print $checkedin; ?>"
name="<?php print $c['id']; ?>" />
<?php endif; ?>
</li>
<?php endforeach; ?>
So basicly it's a list of contacts. Some are checked in, therefore the different classes and buttons. (Any cleaner suggestions are more than welcome!)
But now I need to populate the list via Ajax, since I need to be able to sort it. So I send some more data along with the call and my php/mysql returns some filtered data.
<li> -
ID should be the id from the db.
class needs to differ between 'checkedin' and '' depending on result from mysql.
<input> -
class should be 'checkerin' or 'checkerout' depending on result from mysql.
name should be the id from the db.
The problem, what do I do with the returned data from php -> ajax -> print?
And how do I translate all those nasty if()'s so that I can make difference amongst checked and nonchecked contacts? Edit it before returning it?
Let php create the data as an associative array and return it to the ajax as a json_encoded string, then walk through the json and create your list. Or, you could have the ajax return the already created page and populate your div with that info, just make sure you set the option on the ajax call to allow javascript to execute if you need it to.

CodeIgniter highlight fields with errors on form submission

I could use some guidance in how to proceed on form validation in CodeIgniter. I am using CodeIgniter's built-in form validation and works fine as far as it goes. It returns individual error messages for each field where there is an error by using and wrapping it in some HTML/CSS for styling:
<?php echo validation_errors('<p class="error">'); ?>
But what we want to do is highlight the fields where there are errors. CI will let you put the error messages next to where the form errors are. But it requires you to use the error message for the value, like this:
<input type="text" name="email" value="<?php echo set_value('email'); ?>" size="50" />
which by the way is in CI's manual in non-CI syntax, which puzzles me. Anyway, we also want the data from the field when the form is submitted to be preserved and returns. So I've done:
$email = array('name' => 'email', 'value' => $em);
?><div style="padding:5px;">Email* </div><?php echo form_input($email) . '<br/>';
$em is returned from the controller like this:
$data['em'] = $this->input->post('email');
So my question is, how do I accomplish all of what is outlined above? Obviously, what CI suggests and what I have done collide. But I don't know how else to do it, so I could use some help.
EDIT: Upon further digging, I see that you can put the error message next to the field by doing this:
<?php echo form_error('email'); ?>
But I'm not getting any message upon an error, even though I have the rule written and I get an error with the first line of code above.
form error($field) returns an empty string '' so better use:
<input type="text" name="email" <?php if (form_error($email) !=='') { echo 'class="error"'; } ?> value="<?php echo set_value('email'); ?>" size="50" />
Tested.
In order to display error individually you should use the function form_error('email'). And for you to get a value of a field being checked, use the function set_value('email'). For these two functions to work, you would have had to, in your controller, set a rule for the 'email' field. Where you specify wich validation rules apply to that field.
<?php echo form_error('email'); ?>
<input type="text" name="username" value="<?php echo set_value('email'); ?>" size="50" />
source: http://codeigniter.com/user_guide/libraries/form_validation.html#individualerrors
untested, but form_error($field) should return true if there is an error:
So perhaps:
<input type="text" name="email" <?php if (form_error($email)) { echo 'class="error"'; } ?> value="<?php echo set_value('email'); ?>" size="50" />
untested. worth a shot?
Perhaps also consider using JQuery or similiar to validate and style the form fields, and use CI as a fallback (for presentation purposes, obviously).
That way your form validation can be styled as you required without CI limitations for 99% of validation rules, and then anything else, the default CI way can kick in.

Categories