Symfony: render _csrf_token field with sfForm - symfony1

I have a sfForm form myForm with another object form embedded in it.
I print it in the template like this:
<?php echo $myForm; ?>
As far as I know, it's supposed to print the hidden fields by default, but it only prints the id hidden field, and not the _csrf_token, why is that?
If I try to print it with echo $myForm['_csrf_token']; it prints the field just fine.
If I use echo $myForm->renderHiddenFields(); it prints the _csrf_token field and also the id hidden field for the second time..
In myForm I tried to enableLocalCSRFProtection() and it's still doesn't work.
Any idea how to make myForm render _csrf_token field by default?

(placeholder answer, as per comments above)
"It really was the embedding form that was causing the problem! I have a custom SchemaFormatter which ignores the embedded form's label (cause it's ugly when printing) but I messed up the hidden fields printing"

Related

Pass value in textarea in view phtml page when textarea created in zf2 form attributes

I want to create text area with the help of zf2 form attributes and pass value in text area in view page so how to do this? So I want something like:
<textarea class="form-control" name="title">
<?php echo $x;?>
</textarea>
Now I want to change this to zf2 form and pass $x in view html page.
As I understand you wanted to set value for textarea INSIDE the view template.
$form->prepare();
/../
$element = $form->get('input_name');
$element->setValue($x);
/../
echo $this->formInput($element);
echo $this->formElementErrors($form->get('input_name'));
So it is possible to set input's value the way described above.
It would be worth if you post some code examples, in the future.

Struts 2 - Tiles-Result with HTML Anchor

is it somehow possible to add an anchor (or hash) to a Struts 2 Action-URL? To be specific:
I have a html form which can be extended with more fields if the user clicks a button "add more fields". This Button sends a html submit to the backend (Action "thismyaction") where a list object is filled with another set of input fields. The action then returns to a tile "thisismyform" which loads the same jsp as before, where the new fieldset is visible.
(Unfortunately there is no way to achieve that via ajax / JS in this project at the time. I know that you usually add fields that way, but i got the project as it is.)
Each fieldset is counted (fieldset-0, if user adds more fields another set is added "fieldset-1"). The sets always contain the same fields, but enumerated.
What happens here? There is a post to the action which generates another fieldset and redirects back to the same page, where it renders all fieldsets. Important: the result type is "tiles"! I guess this is what makes it difficult.
Now I want to dynamically add an anchor to this URL "thisismyaction.do", like e.g. "thisismyaction.do#fieldset-1". Use Case: User adds another fieldset, post to the action => result type="tiles" => JSP, user sees reload of the page with the new, second fieldset and gets "scrolled" to the second fieldset via the anchor. Is that possible?
I hope I could describe it properly, what I want to achieve... If there are questions, feel free to ask.
Simplest way I can think of right away is
<script type="text/javascript">
window.location.hash = "fieldset-<s:text name="latestIdOfFieldSet" />";
</script>
If you can figure out what's gonna be latestIdOfFieldSet before you render your jsp and set it in your action method. you should get what you are trying to do.

ValidationMessageFor display outside the form

I have this validation message for a field in MVC view outside the begin form. However it is not displaying that.
#Html.ValidationMessageFor(m => m.OldPassword)
Is there any possible way I can achieve this? Since I don't wanna use a Jquery or Javascript function just to validate this.
Keep the validation message inside the form with a Div tag.
Make the Div display none.
On button click, check the IsValid property of the form
If in valid, then copy the HTML() of the Div and put it into another div which will be outside the form.
Its not a clean solution, but the trick will work
ValidationMessageFor only works if the fields / Controls are inside the form post action for that controller. Otherwise need to do separate methods like JavaScript / Ajax / ViewBag to get the error.
It only works if #Html.ValidationMessageFor(m => m.Property) is inside the form. However, I managed to find a workaround that also updates the outside div when there are element changes, i.e., when the error messages change without re-submitting the form:
Place a <div id="outside-form"></div> outside your form.
Place a <div id="inside-form" style="display:none;"></div> inside your form, which contains all #Html.ValidationMessageFor(m => m.Property) statements.
This is the nasty part, in your jquery.validate.js, find the following:
form: function() {.....} and just before the function return add $("#outside-form").html($("#inside-form").html());. This ensures that your outside-div will be updated every time the whole form is validated.
element: function( element ) {.....} and just before the function return add $("#outside-form").html($("#inside-form").html());. This ensures that your outside-div will be updated every time each form element is validated.
That's it! I was wondering if there's some way of extending the form and element objects without changing the .js file itself.. do you have any clue about this?

symfony validation problem

im testing symfony form validation.
the problem is very simple.
no matter what i put in the body text area and post it, i keep getting "Required" back.
i dont know why.
i just have one validation rule.
here is the code:
code
what is wrong/how can i debug?
thanks
UPDATE: it has something to do with the binding in the controller.
cause even if i delete the validation the form will still not be valid and it will be passed to the template but this time without the "Required".
so it wont be valid no matter if ive got the validation or not.
it has something to do with the embedForm() maybe? someone that has validated an embeded form?
i have printed the error messages out with
<?
foreach ($form->getErrorSchema() as $field => $error) {
printf("%s: %s\n", $field, $error->getMessage());
echo "<br />";
}
?>
and i get:
0: Unexpected extra form field named "body".
thread: body [Required.]
_csrf_token: Required.
Body
do they mean that thread:body is required or csrf is required?
thanks
First of all, try echoing the form without being specific, eg:
<?php echo $form; ?>
If this works, then it's something to do with your form echoing code. I'd suggest checking the HTML source of that page then, to see what the default form code renders, and comparing that with what your code outputs above. If the names of the fields are different, therein lies your answer - it could be a form name format as Radu suggests.
On a separate note, don't forget to use:
<?php echo $form->renderHiddenFields(); ?>
as well in your template, so that the CSRF token field gets rendered. This will remove your "_csrf_token: Required" form error.
Have you checked what parameters are sent through POST?
Maybe adding the following in your form setup method will help:
$this->widgetSchema->setNameFormat('thread[%s]');

How to blank out a field in an MVC app using TinyMCE

I've got an MVC app that gives the user textarea's to update some description fields. It's strongly-typed to a table object, and the fields are wrapped in a form with a Submit button.
Occaisionally they don't want any data in a field, but when they delete the text and try to save, the blanked-out field comes back with its original text (i.e. the table object passed to the Save action contains other edits, but attempts to blank out fields result in the original text staying in the field).
I'm assuming this is LINQ trying to determine which fields have been edited, but how do you tell it that it's blank on purpose?
UPDATE: It appears this may be a problem with the TinyMCE jQuery plugin. It adds rich-text functionality to textarea controls. If I turn it off, I can remove text with no problems.
UPDATE 2: It seems to be some kind of javascript bug or something. If I put another dummy field after the problem fields, they work. If I move them to another place in my code, they work. They just don't want to work where they are. Very peculiar.
I'm pretty sure that TinyMCE, by default, puts in <p></p> when the control is emptied.
So if you are checking for "" then you may be disapointed.
This initially caused me some issues but never with saving. I was checking if the field was "" and then doing something else. Once I realised that "" was never going to happen, I adapted my validation accordingly.
I just check that on a recent project using TinyMCE editor, but it indeed send "" for an empty input, and during the implementation we had no issues with that.
alt text http://diarioplus.com/files/pictures/tiny.PNG
The body property is the one with a tinyMCE editor on the client side.
I really think it will be something with the modelBinder or the way you set the values back to the model.

Resources