zend form how to add lots of custom html - zend-form

I have lots of zend_form, and I want to add lots of custom html in them, what are the possible options. For e.g. I have a page where user can attach multiple emails with the account, so I have a field to add new email address and on top of that I want to show add other email addresses user has attached with the account. To display a form field I am using decorators and generating following html
< div>
< label for="accountDetailsForm-email" class="required">Email:< /label>
< input type="text" name="accountDetailsForm[email]" id="accountDetailsForm-email" value="" class="inputText" size="47" />
< /div>
to display the uneditable data I want to generate
< dl>
< dt class="required" >Email: < /dt>
< dd >user#email.com< /dd>
< /dl>

There are two things you can do.
The first is to write your own form elements and decorators, which I recommend if you want to reuse your code.
The the other thing is you can use e.g.
$form = $this->form;
echo $form->email->renderViewHelper ();
in the corresponding view file to customize your form, so do not call just
echo $form;
That would echo the form. But echo the elements manually ;)

Related

How can I get the ID of selected order in the list of orders Prestashop?

I am using Prestashop for an e-comercial web site. I want to export orders into an excel file. For that I added a button into the order by adding these lines
{block name=preTable}
<div><button type="button">Exporter Excel!</button>
<button type="button">Exporter PDF </button></div>
{/block}
into the file \admin\themes\default\template\controllers\orders\helpers\list. To execute the necessary query I need to have the id of the selected orders, but I really don't know how I can get it.
I guess you are using Prestashop 1.6 version.
You don't need any modifications. You can filter the orders you want to export (by date or other parameters) with Search and then press the Export button.
Checkboxes with the name "orderBox[]" contain ID value of the order. Every row that is selected will have row ID in array orderBox. So in your post method you access it through:
$orders = Tools::getValue('orderBox[]');
foreach ($orders as $order_id) {
// do something with ids
}

Variable in html code

I'm super new to html
All I need is the code for a field where a User can type his Staff Number and then a button which takes him to a URL that is made up of his Staff Number somewhere in the path.
Eg:
The User enters '123' in the text field and when clicking the 'Submit' button must be taken to this document:
www.mysite.com/Staff123.pdf
Not sure about the syntax but with an example I would be able to edit to suit what I need if I can get the code to create both the text field as well as the button.
Thanks a lot
You need to create a form in html. Basically, a form is a block which let user input some values (text, password, email, date, integer, file, ...) and that send these values, once submitted through a submit button, to a certain file that will process these datas.
A classic example is the login form that you can see on nearly each site you know.
It could be like that:
<form action="processing_script.php" method="post">
<input type="email" name="user_mail" placeholder="Please enter your mail here">
<input type="password" name="user_password" placeholder="Please enter your password here">
<input type="submit" value="Click here to send the form">
</form>
You can see some attributes used in this example, I will describe each of them:
action attribute for form tag: it's the script that will receive and process the values from this form.
method attribute for form tag: it's the way that values will be sended to the destination script. It can be etheir "post" or "get". The post method will send the values through http headers, so it's hidden for users (but it can be seen with tools like Wireshark). The get method will send values through the adress bar like this (this is the url you see once you submitted the form): http://yourWebsite.com/processing_script.php?user_mail=johndoe#liamg.com&user_password=mYp#$$W0rD
type attribute for form tag: it depends on the type of data you want the user to inquire. Your web browser will use this attribute to determine which way he will show the input to the user. For example, user will see a little calendar widget if you wrote type="date". The browser will also do some basic verification on the data type when the user will click the submit button (in fact, the browser will not let someone validate the form if for example the input type is "email" and the value entered by the user is "zertredfgt#" or "erfthrefbgthre", but it will pass if the mail is "johndoe#liamg.com"). Type can be email, text, date, password, file, submit, and some others.
name attribute for input tag: it's the name of the variable that will be used in the destination script to access to the value entered by user in the field of the form.
placeholder attribute for input tag: it's the text shown in the fields when they're still empty. The text is not in black, it's some kind of grey.
The last thing to explain is the :
it's displayed as a button, and the text on it comes from the value attribute.
In your case, I think you only need to use some JavaScript:
Create a JavaScript method that will redirect you to the right pdf url based on what is entered in a text input.
Create a small form, without action or method fields.
Create an input type text (for the staff number) with a good attribute name like this: name="staffNumber".
Create a button (not a submit button) like this:
To redirect to a specific url in JavaScript, you want to read this: How do I redirect to another webpage?
To read the value from an input in JavaScript, you can proceed like that:
...
var staff_number = getElementsByName("staffNumber")[0].value;
...
To create the full url of the right PDF, just use the concatenation operator (it's + in JavaScript), so something like that should work:
...
var base_url = "http://youWebsite.com/Staff";
var file_extension = ".pdf";
var full_url = base_url + staff_number.toString() + file_extension;
...
(the .toString() is a method that ensure it's processed as a string, to concatenate and avoid some strange addition that could occur I guess)
I think you've got everything you need to create exactly what you need.
Please keep us up to date when you've tried !

Yii: Receiving multiple records for a single model from view in POST method: To save Parent Child data

I am in a situation where I have more than 4 child tables associated with one Parent table. I need to create a user experience in which user presses Save button only once, meaning by, user enters all the data in parent model fields, then enters data in all four child model fields and then presses the save button. As far as I know, having relations in the model allows you to make associated rows inserted easily but the main problem is how to receive multiple rows from view in POST method for a single model (here I essentially mean the child models). I have tried it manually by repeating the attributes of child model in view but when I save the record, only the last rowset gets stored in the child table along with parent table, one row for the child table gets missed. Kindly note that I am using CActiveForm and other Bootstrap widgets in my View files.
Is it possible in Yii or I am too wishful....any suggestions or comments ????
Many thanks in advance.
Regards,
Faisal
I got the solution but with all the help from here and also from other forums. I followed the post by Kiran and tested it by generating additional HTML attributes using jQuery. On the submit, I got all the rows exactly how I wanted. In the controller, first I counted the total number of models submitted in the post request and then iterated over each one for desired processing. Following is the code snippet.
if(!empty($_POST))
{
$v=count($_POST['Address'])+1;
Yii::log(count($_POST['Address']));
for ($i=1; $i<$v; $i++){
$addressModel_1->attributes=$_POST['Address'][$i];
Yii::log('Dumping Data from '.$i.' model');
Yii::log($addressModel_1->city);
Yii::log($addressModel_1->street);
Yii::log($addressModel_1->state);}}
On the view side, I generated the HTML using jQuery function. All this function did was to add another set of html to allow the user to enter data. Important thing to note while generating the HTML is the name of model or else it wouldn't land where you want in controller.
Following is the code snippet of this function. Please note that I am hardcoding the "3" as id since I already had two sets of rows in DOM. I am going to further improve this code but rest assured, the logic works.
function createNewAddress(){
var newdiv = document.createElement('div');
var inner_html='<div class="row">';
inner_html+='<label for="Address_3_street">Street</label> <input name="Address[3][street]" id="Address_3_street" type="text" maxlength="200" /> </div>';
inner_html+='<div class="row">';
inner_html+='<label for="Address_3_city">City</label> <input name="Address[3][city]" id="Address_3_city" type="text" maxlength="200" /> </div> ';
inner_html+='<div class="row">';
inner_html+='<label for="Address_3_state">State</label> <input name="Address[3][state]" id="Address_3_state" type="text" maxlength="200" /> </div>';
newdiv.innerHTML=inner_html;
$('#user-form').append(newdiv);
}
This way, I can have n-number of child rows added on the fly from browser and user will save all data by pressing the Save or Submit button only once.
Thanks to everyone for their support.
Regards,
Faisal
You should be using tabular input, that way you can receive data for multipe instances of the same type, you can then save the parent and use its id to fill the child foreign keys.
You could make a new CFormModel to handle all the form fields validation, and then manually set the attributes after the $model->validate in the POST function. EG:
if ($model->validate){
$model_one = new ModelOne;
$model_one->name = $model->model_one_name;
$model_one->surname = $model->model_one_surname;
....
$model_two = new ModelTwo;
$model_two->name = $model->model_two_name;
$model_two->surname = $model->model_two_surname;
....
}

Tags getting appended to dojo editor content

I have a dojo editor on a jsp page. The dojo editor is one of the required fields and i have a validation in place for it. There is a scenario in which some tags are getting appended. I cannot find a particular pattern when it gets appended but most of the times it occurs after one selects and copies all the content and pastes on the editor. So the editor content in this case was
<div id="dijitEditorBody">content which user entered</div>
Issue: When the user deletes all content which was entered the tags are still there and get submitted. In this case atleast visually editor has no content but the field holds the following value:
<div id="dijitEditorBody"></div>
or
<div id="dijitEditorBody"><br /></div>
So it skips validation and displays an empty editor when data is retrieved from DB?
I am confused about why these tags are getting appended?
In RichText.js, this snippet :
if(dojo.isIE || dojo.isWebKit || (!this.height && !dojo.isMoz)){
// In auto-expand mode, need a wrapper div for AlwaysShowToolbar plugin to correctly
// expand/contract the editor as the content changes.
html = "<div id='dijitEditorBody'></div>";
setBodyId = false;
}else if(dojo.isMoz){
// workaround bug where can't select then delete text (until user types something
// into the editor)... and/or issue where typing doesn't erase selected text
this._cursorToStart = true;
html = " ";
}
Explains the reason why that tag is added...
Although you see it in your alertbox, I believe it's not present in the posted contents... right ?
The editor should take care of removing the extra-tags => not tested but pretty sure...

Problem retrieving values from Zend_Form_SubForms - no values returned

I have a Zend_Form that has 4 or more subforms.
/**
Code Snippet
**/
$bigForm = new Zend_Form();
$littleForm1 = new Form_LittleForm1();
$littleForm1->setMethod('post');
$littleForm2 = new Form_LittleForm2();
$littleForm2->setMethod('post');
$bigForm->addSubForm($littleForm1,'littleForm1',0);
$bigForm->addSubForm($littleForm2,'littleForm2',0);
On clicking the 'submit' button, I'm trying to print out the values entered into the forms, like so:
/**
Code snippet, currently not validating, just printing
**/
if($this->_request->getPost()){
$formData = array();
foreach($bigForm->getSubForms() as $subForm){
$formData = array_merge($formData, $subForm->getValues());
}
/* Testing */
echo "<pre>";
print_r($formData);
echo "</pre>";
}
The end result is that - all the elements in the form do get printed, but the values entered before posting the form don't get printed.
Any thoughts are appreciated...I have run around circles working on this!
Thanks in advance!
This is what I did -
$bigForm->addElements($littleForm1->getElements());
Then, iterated over the form elements like so:
$displayGroup1 = array();
foreach($bigForm->getElements() as $name=>$value){
array_push($displayGroup1, $name);
}
Then, add a displayGroup to $bigForm:
$bigForm->addDisplayGroup($displayGroup1, 'test',array('legend'=>'Test'));
And repeat for multiple display groups.
I'm sure there is a better way to do it, but I'm currently unable to find one out.
This is currently one way I can think of to retrieve all the form values via $_POST, if a form is made up of one or more subforms.
If any one knows of a better solution, please post it!
A Zend_Form does not automatically retrieve values from the $_POST variable. Use:
$bigform->populate($_POST)
Or alternatively:
$bigform->populate($this->_request->getPost())
Another thing to keep in mind is that if the sub forms contain elements with the same name they will clash. To check this use the option View => Page Source in your browser and look at the HTML that is generated. When you see two <input> elements with the same name attribute, then this is the problem.
The Zend solution for this is to give the sub form elements different names using setElementsBelongTo:
$littleForm1->setElementsBelongTo('littleForm1');
$littleForm2->setElementsBelongTo('littleForm2');
Furthermore you should leave out these calls as they serve no purpose (but you should set them for the $bigForm):
$littleForm->setMethod('post');