How to prevent html content that's passed into sfFormField::renderRow() from getting escaped? - forms

In a symfony 1.4 view I'm attempting to pass some html/javascript in the "attributes" parameter of the sfFormField::renderRow function:
<?php echo $form['ownership_status_id']->renderRow(array('onFocus' => 'displayHelp("<p>help text</p>");'), 'Own/Rent')?>
Unfortuantely the when the page gets rendered, all of the javascript/html output is escaped:
<select name="address[ownership_status_id]" onFocus="displayHelp("("<p>help text</p>");" id="address_ownership_status_id">
I'm not clear on how to prevent this content from being escaped, can someone help?

Try to unescape the $form variable as so:
sfOutputEscaperGetterDecorator::unescape($form);
Then call renderRow():
<?php echo $form['ownership_status_id']->renderRow(array('onFocus' => 'displayHelp("<p>help text</p>");'), 'Own/Rent'); ?>

I had to use this:
<?php echo sfOutputEscaperGetterDecorator::unescape($form['ownership_status_id']->renderRow(array('onFocus' => 'displayHelp("<p>help text</p>");'), 'Own/Rent')); ?>

Related

Open FPDF in new tab

I have a pdf generated (fpdf) from a post form. I would like the pdf to open in a new tab and/or window prompting the user to save the pdf. I'm guessing I need to save the output to a string
$data=$pdf->Output("OfficeForm.pdf", "S");
but what exactly can I do with this string to get it to open in a new window. I've attempted something like this but it's not working. Am I on the right track or is window.open not what I need?
echo "<script type=\"text/javascript\">
window.open('$data', '_blank')
</script>";
If you use a form you can do it by specifying target='_blank' in the -tag (next to where you should have submit='something')
Example:
This will open a new Tab (showing whatever "makepdf.php" produces) on submit.
Hope it answers the question correctly
I simply added target="_blank" to my form opening tag and used $_SESSION[]; to pass my form to the FPDF code:
<?php session_start(); ?>
<form id ="buildPDFform" name="buildPDFform" target="_blank" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
...some code for my form
<input type="submit" name="buildPDf" id="buildPDf" class="buildPDFbutton" value="Build the PDF">
</form>
Then when the form is submitted I gather my form items, put them in an array, create a session the array goes into and use a header("Location: testcode.php") to redirect to where my FPDF code is.
if (isset($_POST['buildPDf'])) {
$pdfArray = array();
foreach ($_POST as $key => $value) {
...gather your form items into your array
}
$_SESSION['pdfArray'] = $pdfArray;
header("Location: testcode.php");
}
And don't forget in your FPDF code file (testcode.php in my case) to grab your session that has the array.
<?php
session_start();
$pdfArray = $_SESSION['pdfArray'];
... your FPDF code
$pdf->Output('I');
?>
source: https://www.thesitewizard.com/html-tutorial/open-links-in-new-window-or-tab.shtml
use target="_blank" in your a tag to open it to new tab
Try $pdf->Output("OfficeForm.pdf", "I");

Show 404 error in fuelphp pagination

I want to use pagination in FuelPHP, I'm using this code:
$config = array(
'pagination_url' => 'http://localhost/live/public/index.php/admin/accounts/index/',
'total_items' => Model_Account::count(),
'per_page' => 10,
'uri_segment' => 3,
// or if you prefer pagination by query string
//'uri_segment' => 'page',
);
$pagination = Pagination::forge('mypagination', $config);
$data['example_data'] = Model_Account::query()
->rows_offset($pagination->per_page)
->rows_limit($pagination->offset)
->get();
// we pass the object, it will be rendered when echo'd in the view
$data['pagination'] = $pagination;
and here's the view:
<?php echo Pagination::instance('mypagination')->previous(); ?>
<?php echo Pagination::instance('mypagination')->render(); ?>
<?php echo Pagination::instance('mypagination')->next(); ?>
<?php echo Pagination::instance('mypagination')->last(); ?>
But when I try to click on any pagination in frontend then it sends error: 404 page not found
You will need to have a route set up so fuel knows how to map the URI admin/accounts/index/:page to the correct controller.
Something like the below should work in your routes.php config file. (Might need tweaking depending on your app)
'admin/accounts/index/:page' => 'admin/accounts/index'

Why is CakePHP 2.3.0 adding a '1' to my Form Post Values?

I'm using cakephp 2.3.0. I searched in the manual for quite awhile, but I haven't found the answer. Also, I've searched the Internet, but still haven't found what I'm looking for. SO, I'm posting my question here. Note, I'm fairly new to cakephp.
Scenario:
I have a simple form with two fields: activity and zip code.
I'm using POST on the form.
When I type in some value in those fields and submit, I echo those 'post' values/parameters and display in the browser screen. What I typed in, I can see on the screen, but the number '1' is added to the end of what I typed in the form.
Here is an example. I type in these values in the form, 'walk' and '44555'. Then I click 'Submit'. The post goes to my controller's action, which then calls my view. My view is displayed on the browser screen and I echo out those 'post' values. The results on screen are 'walk1' and '445551'.
Example #2: If I follow the steps above and don't enter any values in my form (I'll add error checking later), what I see on the browser screen is '1' and '1'.
I am unable to figure out why I am getting the value of '1' added to my form's POST values?
I'll be glad to include any other additional php code to this posting, if requested by someone trying to help.
Here is my FORM code (from my view)...I know there are DIV helpers, but I'll get to that later:
echo $this->Form->create(null, array('url' => array('controller'=>'activities', 'action'=>'results'))); ?>
<div class="box1" style="position:relative; top:10px; left:10px; float: left;">
Search here.... <br>
<hr>
<?php echo $this->Form->input('activityName', array('size'=>'30',
'label'=>'Activity Name:', 'value'=>'i.e. walking, etc.'));?>
<br>
<?php echo $this->Form->input('zip', array('size'=>'7', 'label'=>'Postal Code:')); ?>
<br>
</div>
<div class="box1" align="right">
<?php echo $this->Form->end('Go Search');?>
</div>
Here is my controller code:
<?php
class ActivitiesController extends AppController {
public $helpers = array('Html', 'Form');
public function index() {
//other code....
}
public function results() {
$this->layout = 'second';
$name = $this->request->data['Activity']['activityName'];
$pCode = $this->request->data['Activity']['zip'];
$this->set('theName', $name);
$this->set('theZip', $pCode);
$this->set('results', $this->Activity->
find('all', array('conditions' => array('name' => $name, 'postal_code' => $pCode))));
$this->set('title_for_layout', 'Results');
$this->render();
}
}
?>
My final view code. I left off some of the code...just showing the part that matters:
<div style="position:relative; top:10px; left:5px; ">
<?php echo print_r($theName); ?>
<br>
<?php echo print_r($theZip); ?>
Thanks
The 1 comes from printing the return value of print_r() which is true (i.e. 1).
In other words: you shouldn't do echo print_r(), just do print_r(). The function handles the printing by itself, you don't have to print the results manually.
(Also, print_r() is almost never the best choice to print out values except when debugging and even then CakePHP's debug() is much more suitable.)

Form validation problem with CodeIgniter

I have this view:
<?php echo form_open(); ?>
<?php echo form_input('username', '', ''); ?>
<?php echo form_submit('submit', 'Submit'); ?>
<?php echo form_close(); ?>
<?php echo validation_errors(); ?>
and this controller method:
function test() {
$username = $this->input->post('username');
$this->form_validation->set_rules($username, 'Username', 'required|min_length[4]');
if ($this->form_validation->run() !== FALSE) {
echo 'Tada!';
}
$this->load->view('test');
}
but when I leave the username field blank, nothing happens. However, if I type in something in it will tell me the field is required. I've downloaded and given CI a fresh install almost ten times now, trying to load with and without different helpers etc. It's becoming really frustrating, please help.
Try using this:
$this->form_validation->set_rules('username', 'Username', 'required|min_length[4]');
The problem lies in the first parameter of set_rules, which is the field name.
The $username you're passing is basically setting the field name to validate as whatever the user puts in the input field. If you were to type 'username' into the input box, you'd see that your form validates.
Change the line
$this->form_validation->set_rules($username, 'Username', 'required|min_length[4]');
to
$this->form_validation->set_rules('username', 'Username', 'required|min_length[4]');
Maybe is the problem of this line
<?php echo form_open(); ?>
If you leave it blank it basically send back to the controller itself and calling the construct and index function only. In this case your function dealing with form processing is "test()"
try this
<?php echo form_open('yourControllerName/test'); ?> //test is the function dealing with
if it is not working try on this
<?php echo form_open('test'); ?>

symfony 1.2 forms renderRow error message problem

Im trying to render a row field in a template with some extra styles, like this:
<?php echo $form['email']->renderRow(array('class' => 'text')) ?>
<?php echo $form['email']->renderError() ?>
The problem occurs when my form doesnt validate on this field... then it displays the error message 2 times!, i.e the renderRow renders one errorMsg string, and the renderError does it again... How can i stop renderRow from displaying the error message?
If I just do this, then it works:
<?php echo $form['email'] ?>
But in that case I cant style the field as I want....
thanks!
I am pretty sure this is also valid for 1.2. Instead of using renderRow, use something like this:
<?php echo $form['FormElementName']->renderLabel() ?> //display form element label
<?php echo $form['FormElementName']->renderError() ?> //display form element error (if exist)
<?php echo $form['FormElementName']->render(array('class' => 'text')); ?> //display form element
renderRow does them all at once.
EDIT From comments (Flask) - added ->render(array('class' => 'text'));