My problem is I have an HTML page that includes a short form on it. What I'd like is when this page posts/gets into my Yii model form, to be able to grab and pre-populate the empty form for the model with the values from the incoming form...
I dont think I can use the pagination widget because my initial page is HTML. Is there some way I can just pull this POST value in if it's set rather than the model.
Im still new to Yii so if this is a simple answer, I apologize
Edit: To clarify, my initial form has a few values from my main page form. The HTML page has 3 fields, say first name, last name, email address. That form then posts in to my full page form, which asks for additional information to complete the model. I'm hoping though that I can pre-populate the first and last name in the new php/yii form.
Basically I'm hoping when I first render _form.php I can grab a GET/POST value and assign it to the current model, or add it as a default value on the form.
"I'm hoping ... I can grab a GET/POST value and assign it to the current model"
You sure can. Do this in your controller and you'll be set:
$model->first_name = $_GET['first_name'];
Of course, you may also want to validate those values and set a model scenario to make sure you don't end up with bad data being passed in by an attacker. Otherwise you could run into XSS attacks. But the ability to directly assign model attributes is nice and powerful ...
Related
What I want to do
I'm trying to build a form in Symfony 2.8 that requests the user's choice between many types of credentials and add an input box to the form it's similar to this one but I use a select option field instead of buttons.
What I did
I was able to create this form with HTML and JavaScript.The user can select more than one credential type only once except the OTV Option that can be selected without limits.
image here
What's my problem
I need a way to store user's data in a session and get them in my controller.
Is it possible to pass submitted data from HTML form to a Symfony controller and if not how can I build a similar form with Symfony's formBuilder ?
CollectionType is probably what you are looking for.
For your purposes, you can think of it as adding rows to the end of a two-column list. The Left Column has credential_type with Name, Email etc in a <select>; and the Right Column allows for your text values.
You'll have two FormTypes: one for the list, and another for the list item. In the list item FormType, you should be able to set a CallbackConstraint to validate e.g. that email values are valid upon form submission.
All user changes are saved at the same time, only when the form as valid.
I think you can do that. There is CollectionType in Symfony form. You can read about it here and how to implement it here
P.S. The main problem in this case is to generate specific names for dynamically created fields to provide a normal handling data in controller.
I want to get all of the fields that will be submitted in $_POST, but before the page is loaded
$data['event'] = $_POSTKEYSFORFORM; //i don't care if they are empty
$this->load->view('event_v/form',$data);
In my template I have values set to $event-key, which causes an error obviously if the key does not exist.
Thanks!
From what I know you cannot "grab the keys" of the POST before you send the POST request.
The controller has no way of knowing what's loaded in the view itself.
If it's that critical to you, You can pass the POST fields as an array to the view, have them echo out in the view, and so have perfect control(if they change, they change in one place only).
It's far from ideal as it's not dynamic to the amount of fields.
Other dirty hacks include having JS auto submit the form once and then collect the POST fields.
Why do you need the post fields without values though, Answering that question might make it easier for us to help out.
I liked to know how I can show a value from my database into an input field. I'm working with CakePHP.
Right now, I have some empty fields, but I want them filled with the database values when I display my page the first time and of course when there are values in the database relative to these fields. I'm able to retrieve all my database data, but unable to show them into the fields.
Is there an easy way with CakePHP to do it or I need to set each field individually in my controller and retrieve it in my view.
Thank you
You are able to fill forms from the controller, by using $this->form->data.
Example:
$this->request->data = $this->YourModel->findById($id);
Edit:
You have to create your form with the form helpers to attach the form to your model, otherwise it won't work.
echo $this->Form->create('YourModel');
I have a form in my application with a multi select. I'm using CI's form helper to build my forms, so the build of the element looks like this:
return form_multiselect('authors[response][]', $faculty->get_all_for_multiselect(),
$pre_selected, $additional_attributes);
This is all well and good if the items are in the database ($pre_selected gets existing responses). However, I'm also running the form through CI's form validation, and when that happens, if validation fails, then the multi select loses the values that had been selected.
I'm sure this is something simple that I'm just over looking, so hopefully someone can help me out here.
Adding more information
The field is marked as required so it is going through the validator (although it will always pass as I'm automatically selecting the current user).
(I'm assuming $pre_selected is an array of values?)
You can reset selected values after failed form submit using the $_POST array.
Since you're already using $pre_selected, you should be able to use the following:
return form_multiselect('authors[response][]', $faculty->get_all_for_multiselect(),
array_unique(array_merge($pre_selected, $_POST['response'])), $additional_attributes);
I'm using Zend Framework and I currently have an existing form using zend-form which functions as required.
I want to add another optional text field to it, but allow the user to choose to display the field and also display it multiple times. e.g. A user registration form with an 'alternative emails' area, allowing the user to add further text fields for each of their email addresses.
Unfortunately, I'm not sure how to go about this. I think sub-forms might be the way forward, but not too sure.
Ideally, once the form is submitted I'd want the data in an array so that I can process it for storing in a MySQL table afterwards.
See this excellent post by Jeremy Kendall on dynamically adding fields to a form:
Dynamically Adding Elements to Zend_Form
Upshot is to use jQuery on the client-side to add fields and maintain a registry of the new fields. Then on the server-side, call a new preValidate() method on the form object which checks the posted registry and adds the required fields into the $form object before standard processing - like isValid() and getValues() - is invoked.
Why don't you make an AJAX call to an addfieldAction() method in which you generate the HTML of another textfield and return that? You can still make use of Zend Form and the whole form can be processed the normal way.