I have added a second submit button to my form, now Symfony2 complains about it: "This form should not contain extra fields"
Although I added this option in the formtype:
public function getDefaultOptions(array $options)
{
return array(
'csrf_protection' => false,
);
}
Any ideas?
You can most certainly have multiple submit buttons. Make sure the button is not in the same array as the other form fields.
So, for example, if your form fields have a name FormType[field_name], you can't have FormType[submit_btn] as the name of the button and you must choose a different one.
Your controller can act differently depending on the button pressed. If your submit buttons are named submit_1 and submit_2 you can have something similar to
if($this->getRequest()->request->has('submit_1')) {
// do stuff
} else {
// do other stuff
}
Related
I have a form which contains a Collection of an unspecified number of subforms. I want to have functionality allowing the user to add a new, blank item to the Collection for them to fill in. The Symfony docs tell us how to do this using Javascript clientside to add new blank form controls, which are then submitted and persisted as normal, but I'd like to do it serverside in the controller, without Javascript.
The problem I'm encountering is to do with the way Symfony Forms work. I have an "Add" button added to my main form, and I intend to detect whether it is that button which has been clicked, so that I can add the blank item to the Collection and re-render the form. But to detect the click I need to call $this->createForm and at that point the form is fixed with the original set of items, it's too late to add an extra one.
//Symfony Action
//A Person has many Selections
$person = $this->getPerson($id)
//All fields are frozen at this point, according to data in $person!
$form = $this->createForm(new SelectionsType($lookups), $person);
$form->handleRequest($request);
//Ideally I'd somehow do this test earlier, but I need $form to do it...
if ($form->get('add')->isClicked() )
{
//TOO LATE!
$person->getSelections()->add(new Selection() );
}
if ($form->isValid())
{
if ($form->get('save')->isClicked() )
{
//Persist
}
}
//Render page etc
Things I've thought about:
Putting the Add button in a completely different form on the same page, which submits to a different Action which can then do some preparatory work before forwarding to the main Action above
Inspecting submitted HTTP data directly to note that Add has been clicked (shame not to use the standard Symfony method)
Give up and use Javascript as suggested (it might work in this example, but I'd like to have the option of carrying out server-side activity (without AJAX...) as part of adding the new blank item)
How can I best achieve this in a proper Symfony way?
EDIT Just seen this: https://github.com/symfony/symfony/issues/5231, which is essentially a feature request to allow what I'm after. One suggestion a commenter makes is to add a blank item to the Collection and then remove it if it's not needed - I don't know how one would do that, but it sounds promising.
ANOTHER EDIT It occurs to me that, because I need two different aspects of the $form I'm creating, I could maybe just make the $form, use it to handle the request, detect the button click, and then throw that $form away, before altering my model and creating another $form. I don't know if that would somehow fall foul of some rules about handling the submission twice.
I'm not 100% but I think you could do the following...
//Symfony Action with (Request $request, ...)
//A Person has many Selections
$person = $this->getPerson($id)
//All fields are frozen at this point, according to data in $person!
$form = $this->createForm(new SelectionsType($lookups), $person);
if ($request->isMethod('POST')) {
$form->submit($request);
if ($form->get('add')->isClicked()) {
// Add thing
} elseif ($form->isValid()) {
// or
// } elseif ($form->get('save')->isClicked() && $form->isValid()) {
// Persist and what not
}
}
//Render page etc
I haven't tested it so I don't know whether it will trigger the form errors (or if it will actually work) so if it does (or it doesn't) I apologise.
What I did in the end was have my Add button hit a separate Action, which then delegates to the main action with a flag to say "add a new Selection", as below:
public function selectionsAddAction(Request $request, $id)
{
return $this->selectionsAction($request, $id, true);
}
public function selectionsAction(Request $request, $id, $addNew = false)
{
$person = $this->getPerson($id);
//Also use "add mode" if we just deleted the last one!
if (!$person->getSelections()->count())
{
$addNew = true;
}
//$addNew is set by a separate action, hit by a different form with the Add button in
if ($addNew)
{
$person->getSelections()->add(new Selection() );
}
//We now have the right number of items, and can build the form!
$form = $this->createForm(new SelectionsType($lookups), $person);
//...
}
I have the following code in my module:
function mymodule_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'comment_node_blog_form') {
$form['#node']->content['links']['comment']['#links']['comment-add']['title'] = t('Post a comment');
$form['#node']->actions['submit'] = t('Post comment');
unset($form['#node']->actions['preview']);
var_dump($form);
}
When I look at the var_dump, the form fields are set as intended. However, in looking at the form, the title still has "Add new comment", the submit button is unchanged, and the preview button is still present.
In searching the page content (including the var_dump output) the ONLY place 'Add' exists is in the text of the form ... NOT in the content of the var_dump. The only way I can see that happening is if the form array is being changed back after my hook, so I tried changing the module weight to 99 (via mysql) and clearing cache. No difference.
function modulename_form_alter(&$form, &$form_state, $form_id)
{ unset($form['actions']['preview']); }
I am having trouble getting a captcha element to display in a signup form. It was working previously, but now for some reason when I try to add the captcha element to the form the entire form does not display.
Below is the function in my controller file to create the form
private function getSignupForm()
{
// create form
$form = new Zend_Form();
$form->setAction('success');
$form->setMethod('post');
$form->setAttrib('sitename', 'loudbite');
// add elements
require "form/elements.php";
$loudbiteElements = new Elements();
// create username field
$form->addElement($loudbiteElements->getUsernameTextField());
// create email field
$form->addElement($loudbiteElements->getEmailTextField());
// create password field
$form->addElement($loudbiteElements->getPasswordTextField());
// add captcha
$form->addElement($loudbiteElements->getCaptcha());
// create submit button
$form->addElement('submit', 'submit');
$submitElement = $form->getElement('submit');
$submitElement->setLabel('Create My Account!');
return $form;
}
and here is the action to pass the form to the corresponding view
public function newAction()
{
// get the form
$form = $this->getSignupForm();
// add the form to the view
$this->view->form = $form;
}
If I comment out the line in my getSignupForm() function where I add the captcha to the form then the form will display ok minus the captcha element. This leads me to assume that there is a problem with the captcha contruction.
Below is the function in an external model file where the captcha is constructed
public function getCaptcha()
{
$captchaElement = new Zend_Form_Element_Captcha(
'signup',
array('captcha' => array(
'captcha' => 'Figlet',
'wordLen' => 6,
'timeout' => 600))
);
$captchaElement->setLabel('Please type in the words below to continue:');
return $captchaElement;
}
All the other form elements are created in this external model file. Based on the fact that the other elements display correctly if I do not add the captcha element, I assume it is not a problem with accessing the model file.
The last time I used this signup form everything was working ok! And I don't remember having changed anything between then and now.
Any help is greatly appreciated.
Regards
Roan
$emailmessage = new Zend_Form_Element_Hidden('emailmessage');
the hidden filed value, i.e 'emailmessage' retrieves the value of the same field name inside the database. However on loading the page, value of the 'emailmessage' cannot be seen, since the element is hidden.
Is there any way to display it without using any other form elements. I want it without using text, textarea, etc.
in controller you need to assign it to view and in the view you can echo it where ever you want:
controller
$form = new Your_Form();
$this->view->emailmessage = $emailmessage;
View
echo $this->escape($this->emailmessage);
You need to set formNote decorator for the element.
You do it by extending Hidden element or by setting decorator in the form.
Form
public function init()
{
// ...
$emailMessage = new Zend_Form_Element_Hidden();
$emailMessage->setDecorators(
array(
array('ViewHelper', array('helper' => 'formNote'))
)
);
$this->addElement($emailMessage, 'emailMessage');
// ...
}
I am using Dojo ValidationTextBox through Zend Framework in the following way:
$form->addElement('ValidationTextBox', 'procedureName', array(
'label' => _('Procedure Name'),
'value' => $alarm->procedureName,
'attribs' => array(
'required' => true,
'invalidMessage' => 'Required!'
)
));
Likewise, my form validation is set up in this way:
if (formWidget.isValid()) {
return true;
} else {
alert('Invalid form.');
return false;
}
Form validation will prevent the form from submitting if "procedureName" text box is blank, but Dojo will not mark the form element as invalid, nor display the "Required!" message. In fact, it will mark the form element only if I click it previously (but still, will not display the invalid message).
How can I reconstruct the behavior from this page, where you can click the submit button without clicking on any of required fields previously, and Dojo will mark all required fields?
Thanks.
Dojo allows both declarative and programmatic usage of many of its features.
Declarative usage uses standard HTML elements with non-standard attributes that are parsed when the page is loaded. Can cause issues so Zend Framework uses programmatic usage by default; the various view helpers will generate javascript and push it to the dojo() view helper for inclusion when rendered.
To specify declarative usage, simply call the static setUseDeclarative() method:
Zend_Dojo_View_Helper_Dojo::setUseDeclarative();
When in your form initialization.
This will allow any non standard options like "required" = "true" to be included in your input tag.
dojox.validate function will then work.
I use
<script type="dojo/method" event="onSubmit">
if (this.validate()) {
//return confirm('Form is valid, press OK to submit');
} else {
//alert('Form contains invalid data. Please correct first');
return false;
}
return true;
</script>
inside my form tags as my validation setup.
The error was that I should've used formWidget.validate(), instead of formWidget.isValid().