I have form data posted by client, I want to manipulate one of the forms value before I run it through $this->form_validation->run().
Is this possible
i.e something like;
//Get user form inputs
$input = $this->input->post();
//generate slug - my custom code
$input['slug'] = sf_generate_slug($input['slug']);
if ($this->form_validation->run()) {
...
You can reassign any post value before $this->form_validation->run() like
$_POST['slug'] = sf_generate_slug($_POST['slug']);
While if you use your above method it will validate because it didn't overrides the $_POST values
Hope it makes sense
Related
I've a form with 120 fields to insert into the DB. The form is inserting fine and the approach I used is below:
I'm fetching all the fields from the view as below in the controller and passing the array($postdata) to the model file to insert.
**View**
$postdata = array(
'firstname' => $this->input->post('firstname'), //1st field
'lastname' => $this->input->post('lastname'), // 2nd field
'age' => $this->input->post('age'),
....
....
'test' => $this->input->post('test') // 120th field.
);
$this->Form_Model->insertdata($postdata);
**Model:**
function insertdata($data = array()) {
$sql_query = $this->db->insert('form_insert', $data);
redirect('Form');
}
My question is Is there any better way to insert. This approach feels bit repetitive.
If you simply want to get an array of all the data submitted, you can do it like this:
$postdata = $this->input->post();
This means, all the data submitted from the form will be there in this array.
And if you want to remove any particular element from this array, you can use unset().
Say for example, you may have named your submit button as "submit_btn" like this:
<input type="submit" name="submit_btn" />
then this value would be there in the above returned array. You can remove it like this:
$postdata = $this->input->post();
unset( $postdata['submit_btn']);
Btw, I have a couple of suggestions. The logic part is done in a Controller(you referred it by mistake as View). A View is simply for the displaying. And the Model is for the database communication.
Also, it would always be better to do some validations on the input that you received from the User through form submissions. We may don't even know what data they are sending!
And move that redirect() you used in the Model to the Controller from where you were trying to call that insertdata() method. In that Model, you just return a value (true or false or maybe something else) and do the business logic inside the Controller
You were kind of mixing up everything. That's why I thought to give you some pointers to help you.
Hope it helps :)
Using Symfony, version 2.3 and more recent, I want the user to click on a link to go to the edition page of an already existing entity and that the form which is displayed to be already validated, with each error associated to its corresponding field, i.e. I want
the form to be validated before the form is submitted.
I followed this entry of the cookbook :
$form = $this->container->get('form.factory')->create(new MyEntityFormType, $myEntity, array('validation_groups' => 'my_validation_group'));
$form->submit($request->request->get($form->getName()));
if ($form->isValid()) {
...
}
But the form is not populated with the entity datas : all fields are empty. I tried to replace $request->request->get($form->getName()) with $myEntity, but it triggered an exception :
$myEntity cannot be used as an array in Symfony/Component/Form/Extension/Csrf/EventListener/CsrfValidationListener.php
Does anyone know a method to feed the submit method with properly formatted datas so I can achieve my goal ? Note : I don't want Javascript to be involved.
In place of:
$form->submit($request->request->get($form->getName()));
Try:
$form->submit(array(), false);
You need to bind the the request to the form in order to fill the form with the submitted values, by using: $form->bind($request);
Here is a detailed explanation of what your code should look like:
//Create the form (you can directly use the method createForm() in your controller, it's a shortcut to $this->get('form.factory')->create() )
$form = $this->createForm(new MyEntityFormType, $myEntity, array('validation_groups' => 'my_validation_group'));
// Perform validation if post has been submitted (i.e. detection of HTTP POST method)
if($request->isMethod('POST')){
// Bind the request to the form
$form->bind($request);
// Check if form is valid
if($form->isValid()){
// ... do your magic ...
}
}
// Generate your page with the form inside
return $this->render('YourBundle:yourview.html.twig', array('form' => $form->createView() ) );
I know to use set_value() for setting default values in a CodeIgniter form, but how can I set the default values for the validation if either the value isnt submitted or its before a form submission?
public function index($uri = NULL)
{
$this->load->helper('form');
$this->load->library('form_validation');
//pull from DB or Session
$data = array(
'Status' => 'users_default_status',
'Order' => 'users_default_order',
'Asc' => 'users_default_asc'
);
$this->form_validation->set_data($data);
$this->form_validation->set_rules('Status', 'Status', 'numeric|trim|required|strtolower');
$this->form_validation->set_rules('Order', 'Order', 'trim|required');
$this->form_validation->set_rules('Asc', 'Asc', 'trim|required|strtolower');
if ($this->form_validation->run() === FALSE) // validation failed
{
// validation failed but maintain submitted values in form/feedback
}else{
// validation successful, do whatever
}
}
So that if there was a form submission it uses the POST values, and if not it uses defaults (but still validated). I would like the defaults to work on a variable by variable basis, so some can be defaults some can be user submitted.
I know to use set_value() for setting default values in a CodeIgniter form, but how can I set the default values for the validation if either the value isn't submitted or it's before a form submission?
Simply check if the value exists and use it as the default, otherwise it's blank (or a different default).
In the Controller, decide if you're going to load a blank form, if not, send the data for the fields....
$data['fieldname'] = "whatever"; // from the database
$this->load->view('yourpage', $data);
Then in your View, check for the existence of this data for each field. If the data was sent, use it. Otherwise, set a blank value.
<?php $value = isset($fieldname) ? $fieldname : ''; ?>
<input name="fieldname" value="<?php echo set_value('fieldname', $value); ?>" type="text" />
If you do not send the data from the Controller, the field will be blank (you could also set a default)
If you send the data from the Controller, the field will be filled out with this data from your Controller (database).
If you submit the form and validation fails, set_value() function will reload the field with the data from the most recent post array.
These are just some thoughts...
The validation rules act upon the posted data. So if you are using set_value('order',$default_order), when the form is submitted it will either take the new user entered value or the one you provided.
If the user empties a prefilled or default input, you can't have it set as "required" in the rules. What you would do is use a callback function to handle that case to check if it's empty and provide a default value and return TRUE.
I ended up using a session array and storing data there so that I can reference it again later. I just added my post data from each form into this array and referenced it later in my else block. Thanks for the help!
I am using CodeIgniter for a school project. I have some experience with PHP but am relatively new to this framework. I am having trouble using two forms on one page.
I have one form that displays a dropdown of artists. After clicking the submit button for this form, it updates the second form (another dropdown) on the same page with the portfolios belonging to the artist selected in the first dropdown.
I am trying to echo the values from each form just for testing purposes right now, I will implement other features later. My issue is that after my second form is submitted, the post value for the first form is changed. If I echo the selected value of the first form before submitting the second form, it shows the value that was selected. If I echo the value of the first form after both forms have been submitted, it shows the first available value from that dropdown.
I need to be able to take the values from both of these forms and then use them later after both forms have been submitted. So I can't have the values changing right when I need to use them, obviously, any help would be appreciated. Thank you much.
Controller
public function formtest(){
//Making a call to the model to get an array of artists from the DB
$data = $this->depot_model->get_artists_list();
$this->form_validation->set_rules('artist', 'Artist');// Commenting this out for now, 'required');
$this->form_validation->set_rules('ports', 'Portfolios', 'required');
if ($this->form_validation->run() == FALSE)
{
//Building the artists dropdown form
$data['data'] = form_open('formtest', 'class="superform"')
. form_label('Artist<br/>', 'artist')
. form_dropdown('artist', $data)
. form_submit('mysubmit', 'Submit')
. form_close();
//Setting up a temp array of the selected artist's portfolios
$ports = $this->depot_model->get_portfolios(url_title($data[$this->input->post('artist')]));
//Culling out the names of the portfolios from my temp array
$newdata = array();
foreach($ports as $port){array_push($newdata, $port['name']);}
//Building the artist's portfolio dropdown
$newdata['data'] = form_open('formtest', 'class="superform"')
. form_label('Portfolios<br/>', 'ports')
. form_dropdown('ports', $newdata)
. form_submit('mysubmit', 'Submit')
. form_close();
//Send the information to my view
$this->load->view('formtest', $data);
$this->load->view('formtest', $newdata);
}
else
{
//This echos out the first available value from my dropdown rather than the one I selected.
echo $data[$this->input->post('artist')];
echo "success";
}
}
The forms are separate, so when the second gets submitted, there is in effect no value received from the first form, as it isn't included as a field in the second. So you can do that, include say a hidden field in the second form that has the value of the artist. eg:
$newdata['data'] = form_open(
'formtest',
'class="superform"',
array('artist' => $this->input->post('artist'))
);
I have a Zend form where there are lot of textboxes. I need to set the value of these textboxes from the respective controller and present the form while performing an action. How can I acheive the same?
Try to use like this one:
$form = new Zend_From();
$textfield = new Zend_Form_Element_Text('textfield');
$textfield->setValue('your value');
$form->addElement($textfield);
<?= $form ?>