Zend get nad set back UnfilteredValues - php

I go filter and validation set up in my form, also a use $form->populate to put back $data back to my form when fail validation but it is not working.
if ($this->getRequest()->isPost()) {
if(!$searchForm->isValid($this->getRequest()->getPost()))
{
$searchForm->populate($searchForm->getUnfilteredValues());
$this->view->searchForm = $searchForm;
}
when I run this I get filteredValues in my search field instead UnfilteredValues.
What I going wrong?
thank you.

You should not apply the negation !$searchForm->isValid($searchForm->getValues(), because the isValid method actually populate the form for you,
Your code should look like:
if ($this->getRequest()->isPost()) {
if($searchForm->isValid($this->_request->getPost()))
{
// do insert, upload or others
}
}
As i said before, the isValid method actually populate form in case of invalidation of the form.
Best regards!

Related

PHP OOP message keeps displaying on page refresh

I have a relatively simple class which deletes a post:
function delete_post($postid, $reason){
//Stuff to delete post
$this->delete_response = 'Thanks, your course has been removed.';
}
This function is called at the top of a page with a form on. If the form is submitted, the same page checks the POST[] and carries out the function, like so:
if(!empty($_POST['removecourse'])){
$courseManager->delete_post($_POST['courseid'], $_POST['cancel-reason']);
echo $courseManager->delete_response;
};
So my problem is... when I refresh the page, the message keeps displaying. I know this is because I am re-submitting the form, and because there is no such P/R/G pattern going on, but as i am new to OOP, im wondering if im doing this the right way, or if anyone could suggest a way similar to PRG or something?
Add an if that test if somthing changed, like mysql_affected_rows
function delete_post($postid, $reason)
{
//Stuff to delete post
if(mysql_affected_rows())
{
$this->delete_response = 'Thanks, your course has been removed.';
}
}

Get and post in cakephp

In Codeigniter I do this
$p=$this->input->post();
to get all objects posted but I don't know if there is something similar in cakephp to get all posted variables from a form ? I am writing a function to get posted password and save it into database in place of the old password recorded there.
I use native php to get 'posted' variables from a form, (I am not familiar with cakephp form usage) that is why, so instead of using $_POST['sssss'] what should I do now ?
Thank you for any help.
$value = $this->request->data('key');
Please for further reference, read the manual. It's so much easier and better for yourself to figure it out by yourself.
http://book.cakephp.org/2.0/en/controllers/request-response.html#accessing-post-data
for the GET method
$this->request->query['category-name'];
and POST method
$this->request->data
http://book.cakephp.org/2.0/en/controllers/request-response.html#accessing-querystring-parameters
You can check if posted a form by using
if (!empty($this->data)) {
print_r($this->data);
}
The Post data must be in data to show up in $this->request->data.
Example:
// input field
<input type="text" name="data[foo]" value="bar" />
// in your controller
debug($this->request->data);
To check if posted a form, please use:
if ($this->request->is('post')) {
pr($this->request->data);
}
If you want to get a specific field of the table can move so:
if($this->data["Objetorastreavel"]["id"]){
}
It checks only the ID Objetorestraeval if you want to pick only one field and not post the whole page.
You should able to access form post data with:
For CakePHP 2.x
if ($this->request->is('post')) {
pr($this->request->data);
}
For CakePHP 3.4.x
if ($this->request->is('post')) {
pr($this->request->getData());
}
Documentation for CakePHP 3
You can use following to retrieve post/get data in CakePHP
For post data:
$this->request->data;
For get data:
$this->request->query;

How to make a form self referencing in Drupal? Or any other options?

I would like to make a form I have in my website self referencing. Or if that's not an option, how would I go for, for example, showing the results of a search I make in my site?
I have a site in which you search for places and it returns a list of places for your preferences. At the moment my script creates a new node every time a user searches but this isn't convenient anymore. How do I change it so that the page content is changed and I see the results instead of the search form?
Thanks,
You should redirect your form to a page passing a query string with the string of what the user searched and then use $_GET['search_param'] in your search/restuls page to handle what will be displayed to the user.
function yourform_form($form_state) {
$form = array();
//$form['your_search_field']
$form['#submit'][] = 'yourform_form_submit';
return $form;
}
function yourform_form_submit(&$form, $form_state) {
$query = 'search_param='. $form_state['values']['your_search_field'];
drupal_goto('search/results', query);
}
If you're using Drupal 7 your submit function should look like:
function yourform_form_submit(&$form, $form_state) {
$options['query']['search_param'] = $form_state['values']['your_search_field'];
drupal_goto('search/results', $options);
}
After you submit you should be redirected to http://yoursite.com/search/results?search_param=my_search_value
Note that this technique is used by popular search engines:
https://www.google.com/search?q=my_search_value
Your form should include $form['#action'] to lead you to specific page after submit:
function example_form($form_state) {
$form = array();
//your form code
//...
$form['#action'] = url('search/results');
return $form;
}
On submitting form (example_form_submit) you should take all your values and save them to cookies using user_cookie_save function and on your page you can use this cookies.
You also could serialize your values to deal only with one cookie if you want, and then unserialize them on your page. You can delete cookie using user_cookie_delete function.
You should define path search/results where you could take those cookies data and manipulate with it.

How to validate many fields combined in Codeigniter?

Is there a creative and easy way to check many form fields at once.
I have a form with generated fields on the fly, each has a unique id.
The thing is submitting all the fields are not required, but at least one field must be filled before submitting.
Is there a way to do this in Codeigniter, or how would I go about validating this effectively.
I understand that it is possible to check each field individually, but I'm seeking for a much cleaner way.
I hope it's clear for you guys. Thanks.
Try this:
$_POST['data_you_want_to_validate_together'] = $_POST['first_field'] . $_POST['second_field'];
$this->form_validation->set_rules('data_you_want_to_validate_together','Some Data', 'required|callback_some_function');
Now you can get the data via:
echo $this->input->post('data_you_want_to_validate_together');
Right now, I have a custom function which will batch check every submission on my form to make sure it meets a set of rules. I am thinking that it may work to set up my form as an html array (data[]), retrieve it in code igniter, then use the CI callback_ validation function to make sure everything turns out okay. It seems complex, so I haven't yet entirely wrapped my head around it, but maybe this can get your wheels turning in the right direction.
EDIT:
$this->load->library('form_validation');
// If there is any posted data, then we should assign it to our $post_data array.
$post_data = $this->input->post('project_data');
if (empty($post_data)) {die('empty form');}
// Now, we are ready to validate the incoming data.
// We will send the data through a callback function which will check to make sure it is valid.
// If it is not valid, the callback function will trigger a codeigniter validation error.
// Let's temporarily remove any commas from the submission data to avoid delimiter confusion when sending it through the callback
$post_data = str_replace(",", "DELIMITEDCOMMA", $post_data);
$post_data_str = http_build_query($post_data);
$this->form_validation->set_rules("project_data[errors]", 'Errors', "required|callback__validate_project_data[$post_data_str]");
$this->form_validation->run();
Then, just write your custom validation function based on what it is that you need to validate.
function _validate_project_data($value, $request)
{
// A callback rule check is being attempted by the CI validator
// $value is the actual value of the submission, while $request is the key and value
$request = explode(",", $request);
$request = str_replace("DELIMITEDCOMMA", ",", $request);
// rename the keys in the request back to the original convention
parse_str($request[0], $request);
//var_dump($request);
// perform validation here and return true or false (valid or invalid)
}
Not entirely sure what you mean, as the fields would have to be checked individually whichever way you do it. Unless you're concatenating all the inputs and checking that? Maybe I've missed something there though.
Just go for client side: http://flowplayer.org/tools/demos/validator/index.html
Serverside: http://formigniter.org/
Hope this helps...

Display error when entire form is blank in CodeIgniter

I'm trying to create a fairly simple form that has a few checkboxes and input fields and a textarea. Nothing is required by itself; however, if 'A' checkbox is checked, then 'A' input field is required (and so on for the couple other checkboxes I have).
I have the above functionality in place, but I'm having a tough time figuring out how to have an error returned if the form is submitted blank (since nothing is required by default).
Does anyone know of an easy-ish solution for this? It seems like it should be so simple...
Thanks
I assume that your using the form_validation class..
You will need to write a callback that does something like this:
function _checking()
{
if (isset($_POST['a_checkbox']))
{
if (empty($_POST['a_text_field']))
{
$this->form_validation->set_message('_checking', 'this should not be empty');
return FALSE;
}
return TRUE;
}
}
I hope this is what you are looking for..
Just check if $_POST-array is empty, except for your submitbutton?

Categories