CakePHP form data to session - forms

I'm facing a problem with CakePHP. I have two pages, on first page user will enter data in form and on second page, he will see a printable data for the form (sort of confirmation).
Since the form is big and contains 20+ fields, I don't want to read them using $this->params.
I'm using the following code:
function birthconfirm(){
$this->loadModel('Birth');
$birth = new Birth();
$birth->set($this->data);
$this->Session->write("birth", $birth);
$this->set("birth", $birth);
}
Birth is the name of the model here. There is another view birthconfirm.ctp which is not reflecting the data passed on the last line. I tried to use following lines on the next view but of no use:
echo $this->birth["Birth"]["birth_date"];
echo $this->birth->birth_date;

Use the below code to set birth for birthconfirm.ctp
function birthconfirm(){
$this->Session->write("birth", $this->data);
$this->set("birth", $this->data);
}

Related

SilverStripe custom form retrieve values for form fields

I have a problem with my front end admin update page. How can I retrieve values from a SiteTree page and populate it into a custom form? Is the setValue($values) function the only way? If yes, which is the best method to get the page variable?
I am using this:
$evens = Versioned::get_by_stage('PageCalendrierEvenement', 'Stage')->byID($evenID);
I'm getting values from an ID of a draft page. After I get variables and values like that:
$field = new TextField('Titre', 'Titre');
$field->setValue($evens->Titre);
or
new TextField('Titre','Titre', $evens->Titre);
Which is the better solution?
The solution is 2 part:
Use TextField::create($constructor, $args) instead of new (optional, but not doing so is a pet hate of mine).
Set all fields at once: http://api.silverstripe.org/3.1/class-Form.html#_loadDataFrom

Create a downloadable table from a form submit

Ok, so my last question was closed because I didn't quite ask it the proper way... What I want to do is, create an HTML form that when submitted outputs the form in a table format and allows you to download that file, or does it automatically, so I guess in short... user navigates to page, fills out form, hits submit, and HTML table asks to download. I hope I asked this correctly . This is something that would help agents at my call center organize and save their abundance of login names and passwords, Thanks
i did a quick google.
you will want to get the form to submit the data (with possible client side validation)
then do any server side data validation if needbe.
then output the data as a table.
you can also export the table with php code like explained here if you will be opening it with excel for instance.
Have you tried existing services, such as Google Forms?
To expand on jskye, it would be something like this to create an HTML table.
//Get POST data from Form
$formData_Name= $_POST['Name'];
$formData_Age= $_POST['Age'];
$formData_Message= $_POST['Message'];
//Modify header information so it knows to download this and how to save it
header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=table.htm");
//Print headers to table
print "<html><body><table>\n\t".
"<tr>\n\t\t".
"<th>Name</th>\n\t\t".
"<th>Age</th>\n\t\t".
"<th>Message</th>\n\t".
"</tr>";
//Print form data to table
print "<table>\n\t".
"<tr>\n\t\t".
"<td>". $formData_Name ."</td>\n\t\t".
"<td>". $formData_Age ."</td>\n\t\t".
"<td>". $formData_Message ."</td>\n\t".
"</tr>";
//Close form data
print .= "</table></body></html>";

CodeIgniter repopulate form from both session data & form validation?

I have a form with a couple search options, like a checkbox array and radio button. By using the form validation library I have the form repopulating after a submit, like so:
echo form_checkbox('check_track[]', '1', set_checkbox('check_track[]', '1', TRUE));
echo form_dropdown('select_year', $options, set_value('select_year', '2013'), $attribs);
I also save all the form options (by storing the post) into session userdata. Is it possible to repopulate all the fields from the session data if $_SERVER['REQUEST_METHOD'] !== 'POST' but keep repopulating based on form validation otherwise?
The easier would probably be to separate the form generation from the value generation. In the snippet you provide, the value is read directly from the submitted form.
I would advise you, in you controller or your model to generate a data structure, each field corresponding to one of the form field.
For each, the value would either be the default, either the one stored in the session if it matches you condition ie: valid data and not after a POST if I understood you well.
I ended up just faking that a POST had happened before the form validation stuff ran to get repopulation to work:
if(!isset($_POST['something']) && $this->session->userdata('something'))
{
$_POST = $this->session->all_userdata();
}
$this->form_validation->set_rules('something', 'stuff', 'required');
.
.
.

Zend creating forms based on requests within one controller/action

I don't really know how to word the title well, but here's my issue. I decided instead of having 25 controllers to handle pages, I have one PageController with a viewAction that takes in a :page parameter - for example, http://localhost/website/page/about-us would direct to PageController::viewAction() with a parameter of page = about-us. All of the pages are stored in a templates folder, so the viewrenderer is set to render application\templates\default\about-us.phtml.
I did this so I can consolidate and it seemed like a better approach. My question is the following: lets say when the page request is contact-us, I would need a Zend_Form to be used within the contact page. So, I would need a way within PageController::viewAction() to recognize that the page needs to have a form built, build the form, and also upon submission the need to process it (maybe this should be handled in an abstract process method - not sure).
I have no idea how to implement this. I thought maybe I can store a column with the name of a form and a connecting page identifier. Even better, create a one-to-many page to forms, and then in the submission loop through the forms and check if submitted and if so then process it (maybe there is a isSubmitted() method within zend_form. I really don't know how to handle this, and am looking for any help i can get.
Thanks!
Here is something that came to mind that may work or help point you in a direction that works for you.
This may only work well assuming you were to have no more than one form per page, if you need more than one form on a page, you would have to do something beyond this automatic form handling.
Create a standard location for forms that are attached to pages (e.g. application/forms/page). This is where the automatic forms associated with pages will be kept.
In your viewAction, you could take advantage of the autoloader to see if a form for that page exists. For example:
$page = $this->getParam('page');
$page = ucfirst(preg_replace('/-(\w)/ie', "strtoupper('$1')", $page)); // contact-us -> ContactUs
$class = 'Application_Form_Page_' . $page;
// class_exists will invoke the autoloader to map a class to a file
if (class_exists($class)) {
// a form is defined for this page
$form = new $class();
// check if form was posted
if ($this->getRequest()->isPost()) {
if ($form->isValid($this->getRequest()->getPost()) {
// form is valid - determine how to process it
}
}
// assign the form to the view
$this->view->pageForm = $form;
}
All this really leaves out is the action you take to process a specific form. Since the contact form will likely generate an email, and another form may insert data into a database, you will need some sort of callback system or perhaps another class that can be mapped automatically which contains the form processor code.
Anyway something along those lines is what came to mind first, I hope that helps give you some more ideas.

Zend Framework multiplay forms

I want get the follow thing working.
I've a page with a couple of text articles, each article has his own 'id' in the database. Below every article I want to make it possible to discuss about it. So I setup a discuss form witch I print with my article trough a 'foreach'.
In the form I added a Zend_Form_Element_Hidden. In the view I want to set the value of the hidden field with 'article_id', this likes me the best way to put it in the database?
In the foreach I try the follow thing but when I do this, the form is gone and I only get the element where I add the value.
My code in the view:
foreach ($this->paginator as $article):
echo $this->form->getElement('article')->setValue($article['id']);
endforeach;
I hope some one can make this a bit more clear for me :)
With kind regards,
Nicky
I am guessing you want to print the form inside the loop but only the element is being printed.
If that is your problem, the reason is because setValue() returns the element and not the form.
// Your Code
// This will only print the element and not the entire form
echo $this->form->getElement('article')->setValue($article['id']);
You will have to change your code to:
// Set the element value first
$this->form->getElement('article')->setValue($article['id']);
// Then render the form
echo $this->form;