i'm trying to realize a form that show a list of exercices and under each one of them, the input texte for the logged in user to answer ,i don't need embeded forms because i will not change the exercice text i will just save the responses for each exercice of the given exam and user.
in my database i have three tables :exam that has many exercices and table exercice and table response that has the following fields (user, exercice, response, date)
please am stuck help me !
You can insert a form when displaying your exercices unter each of them and create a route & controller for this form but the router/controller will receive as paramater the Id or Code of your exercice so that when it save the response it set directly it's exercice.
So you must get a ManyToOne relation between Exercice & Reponse.
Just tell me if i've not understood (my english is very poor )
I thik you must add dynamicly your form into the template not send via controller but the action of each form will be a controller route that will yse your POST data to edit exercice
route_name:
pattern: /patten/{id} #hier id is the id of the exercice
defaults: { _controller:.....:Response }
ResponseAction(Exercice $e}
{
In this controller you get the POst datas and you can add your response on the
article
}
Twig:
{{ exercice.content }} {# an example #}
{# a form that you will normaly generate will jQuery or another of your choice #}
<form action="{{ path('route_name',{'id':exercice.id}) }}" method="POST">
//
your fields ...
</form>
Related
I am using simpleforms as a subscription form for subscribing to classes. The form uses a use_as and also sends the email to the sender as a confirmation.
I have edited the email twig template and added further informatie about what the subscriber should do next.
My question is if it is possible to set a specific field value in the instruction text. So far I have not yet been able to do this correctly. I would like to only show the field "name:" in this instruction text so that every sender that gets this email is greeted with his own name.
The for loop that is used in the email template is
{% for value in form %} {{ value }} {% endfor %}
And i'm looking for a specific field to be set instead of all the values of the form.
You can directly access the fields in your emailtemplate, you don't need to use a loop for it.
So if you have a field called 'name' in your config file you can use {{ form.name }} in the email template to display the value that the visitor has entered directly.
I use filtration form and pagination on same page to display great number of objects.
If i use POST form, after going through pagination filtration resets. If i use GET form filtration works, but URL is not clear and even have token.
Something like this:
?form[date_from][year]=&form[date_from][month]=&form[date_from][day]=&form[date_to][year]=&form[date_to][month]=&form[date_to][day]=&form[email]=email&form[submit]=&form[_token]=Nk0prilVJiROZaQQKvCt-hRfnKdh0IdDOWOIer
Is any way to make url more clear, hide token and unused parameters?
Well, i find no solution and write service for handling form filtration data in sessions.
Save filtration: use an array in controller:
$filter_array=[];
if ($search_form->isSubmitted() && $search_form->isValid()) {
$search_form->getData();
//save post/get form data without empty fields
$filter_array[$search_form->getName()]=array_filter(
$request->get($search_form->getName()),
function($value) { return $value !== ''; });
//... etc
}
Solution 1- make forms in your page links and add hidden fields with $filter_array
Solution 2- in page links use a script to add a hidden form field with page number and submit the form, simple but the form could be modified before click.
Solution 3- Put the paginator navigation and search results in a div, use jquery.load() for all actions (send form, page links, $( document ).ready() ...). The url will look like domain/page/#page_1:
{% for i in range(1, paginator.getTotalPages()) %}
<li {% if paginator.getPage() == i %} class="active"{%endif%}>
<a href="#page_{{i}}" onClick="
$('{{selector}}').load('{{ paginationPath }}',
{{filter_array|merge({page: i,})|json_encode()}});"
href="#page_{{i}}">{{ i }}</a>
</li>
{%endfor%}
I have a contact form on a page that has a name field, email field and textarea field. It is made in the most simple way...
$contactEntity = new ContactEntity();
$builder-> $this->container->get('form.factory')->createBuilder('form', $kontaktEntity, array();
$builder->add() ... fields added
Then, in the administrative area, I have a search form that searches for the reacivied messages with name and email fields. I create that form with a different entity with the fields name and email that are one of half a dozen other fields in ContactEntity.
The problem is that the search form is rendered in twig as if it was kontakt form.
To clarify, SearchEntity has fields name and email. ContactEntity also has the same fields but with addition of some other fields. When the search form is rendered in twig, it shows the name and email fields as if it were part of ContactEntity.
Also, twig customization doesn't work. I cannot change label text values, I cannot remove HTML5 validation, and can't do anything beacuse nothing works. I tried renaming the fields in the search entity but it doesn't work.
Twig rendering is made on diffrenet twig scripts and is normal and basic in both both forms. This is on a search form. Changing label taxt to Name doesn't work. The submit value, on the other hand, is correctly rendered.
{{ form_start(form, {attr: {'no-validate' : 'novalidate'}}) }}
<div class="search-rom">
{{ form_label(form.name, 'Name') }}
{{ form_errors(form.name) }}
{{ form_widget(form.name) }}
</div>
<div class="search-rom">
{{ form_label(form.email, 'Email') }}
{{ form_errors(form.email) }}
{{ form_widget(form.email) }}
</div>
{{ form_end(form) }}
To summarize, twig is rendering two of the fields of an entity that the built form has nothing to do with.
EDIT
I've just tested if SearchEntity is filled when form is submitted and it is, with the correct values. I really don't know what is going on here.
EDIT 2
This is a code fragment from the messageAction() method in the controller.
$searchEntity = new SearchEntity();
$builder = $this->container->get('form.factory')->createBuilder('form', $searchEntity, array());
$builder->add('name', 'text')
->add('email', 'text')
->getForm();
Then form is rendered in the response with $form->createView()
Maybe you don't need to pass the builder an entity. In other examples you can just create the entity after getting the data.
How to render a form without a class in other service?
http://symfony.com/doc/current/book/forms.html#using-a-form-without-a-class
I found what the problem is. It's translations.
I'm Croatian and site I'm building has two languages, english and croatian with english as the default locale. Translation is done in various ways, but the translation of forms and all that comes with forms is done via messages.en.php.
For translations, i choose array type. message.en.php looks like this...
... Some data to translate ...
'Ime' : 'Personal name or bussiness'
'Email' : 'Email address
... Some other data to translate
So, when twig tried to render this...
form_label(form.ime, 'Ime')
it rendered the translation for key Ime. Same thing happend to email field. Everything else is in order. I just changed the label value to 'Search for name' or something like that.
As to how to limit translations to only one form, I don't know. I read Translations section but couldn't find anything.
I'm using Laravel 4, and I have two tables related 'Many to many': 'Actividad' and 'Material'. Every Actividad can have one or more Materials, and every Material can belong to one or more Actividad.
So, I have made a form to create a new Actividad where you can save one or more Materials. I've done that with a multiselect input. Like that:
{{ Form::label('material_id', 'Material necesario:') }}
<p>{{ Form::select('material_id', $material_id, Input::old('material_id'), array('multiple')) }}</p>
I don't know if I'm doing correctly but, before saving anything, my first problem is that I'm only obtaining one result. I suppose I should get every option 'checked' in the form... The relevant part of my Controller is:
$material = Input::get('material_id');
return var_dump($material);
I should obtain a list of options selected, but the result of that in my browser is:
string(1) "1"
This is the first time I'm working with tables related in this way, and probably I'm doing something wrong (in the form, in my controller, in my Models,...)
Thank you very much for your help!!
Just change your code to this
{{ Form::select('material_id[]', $material_id, Input::old('material_id'), array('multiple')) }}
I hope this helps.
if you are using custom response handlers on the client side such in the case of submitting info with AJAX, all you need to do is to simple add "[]" to the name of the select control.
e.g.
<select name="material_id[]" multiple>
This is the same case as with regular PHP. The other methods are required if you want Laravel to handle the form elements for you when rendering a new response/view. ( page reload ). This reload doesn't happen with REST requests.
I have a simple search form I want to reuse across multiple pages in my codeigniter application. For example, right now I have a search form in the sidebar and I'm planning on displaying that sidebar on the index, about, and other pages.
I want to have the form validation display errors on the same page the users submits the form from.
For example:
User is on About page.
User submits form with invalid data
User sees error in the sidebar on the About page
and
User is on Index page.
User submits form with invalid data
User sees error in the sidebar on the Index page
But I'd like to reuse that form validation logic. I just want it to display the error on whichever page the user posted from.
Any ideas how to do that? Sorry for the noob question, I'm pretty new to CI.
Here you have to think globally.
Step.1 : Make one view file : display.php
which contains :
<div id = "main">
<div id = "header">
[load header file here]
</div>
<?php
if(validation_errors() != '') {
?>
<div id = "error">
<?=validation_errors()?>
</div>
<?php
}
?>
<div id = "content">
<?=$page?>
</div>
<div id = "footer">
[load footer file here]
</div>
</div>
Step.2 : About us Page.(controlller)
.... Your data ....
at end of controller function
$data['page'] = 'aboutus';
$this->load->view('display',$data);
With regards to your comment on the above question you could use Flash data
With the assumption that you have the session library loaded, here is a helper function.
function last_page($page = null){
$ci = get_instance();
if($page === null)
return $ci->session->flashdata('last_page');
$ci->session->set_flashdata('last_page', $page);
}
then you can call last_page('about'); at the top of the about page, and then when you want to find out what the last page you were on was you can just call last_page(); with no params.
In the User Guide, there's different ways to configure sets/groups of rules. Then you can simply have something like:
if ($this->form_validation->run('signup') == FALSE)
{
$this->load->view('myform');
}
else
{
$this->load->view('formsuccess');
}
Which will run your "signup" group of validations. To me, this is the cleanest way to achieve reusable validation rules.
This is a perfectly valid question.
I'm not a PHP expert, nor a CI expert, but the fact is sometimes you want to post to a controller that didn't create the view from which you're posting. Which means posting back to itself is not going to work.
I came across this post on the Ellislab forum:
http://ellislab.com/forums/viewthread/217176/
On this page, There are 2 methods of going about it. Both of which use flashdata and both of which are totally valid, IMHO.
The first: create a helper function
http://ellislab.com/forums/viewreply/1003010/
The second: extend the CI_Form_Validation Class.
http://ellislab.com/forums/viewreply/1047536/
The second is the way I went as it seems cleanest although some may argue whether the form validation class should know anything about flash data.