symfony validation problem - symfony1

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]');

Related

How to display a name after successful remote validation?

My business web application will have a lot of input controls where the user is expected to enter some kind of code (Country, Zip, Employee ID, etc.).
I'm using RemoteAttribute to validate code entries against my database to ensure that users entered the correct code.
The thing is, my standard feature should be to display a name of entered code if the remote validation is successful or error if the validation is not successful.
I'm not sure how to do this.
The obvious answer is to send Name property along with true value in Json object which jquery unobtrusive validation requires. The cshtml could look something like this:
#Html.LabelFor(m => m.Country)
#Html.EditorFor(m => m.Country, new { data-codename-label="lbnCountry" })
<label ID="lbnCountry"></label>
I'm not sure how to implement such an idea though. Any help is appreciated.
Take a look at this answer which should work for you as well.
Do something on success response for remote validation in mvc
If in that example you choose not to send your value through the custom response header, you can always modify the javascript to do an ajax call to an action/webapi that will return the value in question after a successful validation.

Symfony: render _csrf_token field with sfForm

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"

Struts2 and form validation of prefilled forms

I'm using the validate() - method of struts2 to validate the form input. In my struts.xml I can define a result with name "input" which is displayed if the validation fails. This for the context :-)
Now my question: the form I want to validate contains a selectbox which is filled out of a database. The first time the form is displayed everything works fine. But if I validate the form and the "input" - result is displayed, I get an IOException because of the iterator which outputs the db-result into my selectbox. Is there a solution from struts2 or do I have to use a plugin or something like that? Thank you!
When validation fails, it's often necessary to "reload" data for the form page. There's a FAQ entry that covers repopulating controls after validation, mainly detailing the Preparable interface (preferred) and the use of the <s:action> tag (there are some subtle gotchas that can pop up with this, but in general, it's also okay).

ASP.NET MVC3 Custom Unobtrusive Client Side Validation not preventing Ajax form post

I have a "change password" page that needs to hash any passwords entered on the page via Javascript before sending. To complicate it, the page is loaded via a jQuery load() call, and is submitted by a jQuery.Form ajaxForm() call. Had everything working in MVC2, but MVC3 is giving me trouble.
That is, I have a page with a "Change Password" link that when clicked, loads the change password page into a jQuery modal popup, then the form on the change password page get's submitted via the jQuery.Form library (Essentially just wraps a $.ajax call), and returns it's result into the modal same modal popup.
Essentially, I have a model with two properties, OldPassword and NewPassword. I have two hidden fields generated by by view for these. They hold the hashed value of two other fields, PrehashOldPassword and PrehashNewPassword, and get updated via keyup events (I know, this means it does a whole SHA256 hash on every keyup... inefficient, but got the job doen for testing). The key here is that the regex validation and required field validation needs to be executed on these Prehash fields, which exist on the client side only (As obviously I don't want to transmit these fields to the server in any way).
So I manually create these two and add on the data-val-* attributes to the elements, i.e. they are NOT generated by the MVC helpers, etc. I am guessing that this is where I'm missing something. When the form submits with all fields empty, all of the errors popup that should, but the form goes right ahead and submits anyway.
==
So the things I've tried:
Yes, the unobtrusive library parse() method already get's called to parse the AJAX loaded form contents, and it appears to get all of the data validation stuff correctly, since I see the errors show up as fields blur(), and when I hit submit (before the ajax request completes and replaces the content of the popup).
Possible note: this call to the unobtrusive library's parse method happens AFTER the AJAX successfully loads the change password page into the popup... the AJAX form submit binding is put on document.ready of the loaded content, ergo, the AJAX form submission binding MAY be binding prior to, and thus firing before, the validation calls that the parse method may bind to the submit event...
However, (1) I am doing this same sort of thing in other places without issue, the ONLY DIFFERENCE being that I am manually putting these data-val-* attributes on elements I am creating manually! And (2), if I cause some kind of error on the OldPassword or NewPassword fields, i.e. a required field validation error by not loading a value into them, they display their error, and successfully STOP the form from submitting through the jQuery.Form method.
So I think something has to be wrong here:
<input id="PrehashNewPassword" type="password" name="PrehashNewPassword" data-val-required="The password field is required." data-val-regex-pattern="<%= RegexHelper.PasswordRegularExpression %>" data-val-regex="<%= RegexHelper.PasswordRegularExpressionError %>" data-val="true" />
I know that jquery.validate is getting the rules right, since I DO see the errors. It's just not stopping the form from submitting when their is an error in these manually generated elements, unless I do something like this, and add a pre-submit callback on the form's AJAX submission:
$("#ChangePasswordForm").ajaxForm({
beforeSubmit: function () { if (!$('#ChangePasswordForm').valid()) { return false; } },
target: '#overlay'
});
While this works, it is kind of ugly and I believe it causes the validation to be called twice... Not a huge deal, but less than ideal. So is there some other call that I need to make in the unobtrusive library to bind these?
Not sure if you found the problem, but you may try to
return false
in there if the form is not valid...
.
.
.
if (!$('form').valid()) {
return false;
}
// JSON POST...
.
.
.
If that doesn't work, then you could try to use:
$.validator.unobtrusive.parse($("#dynamicData"));
after dynamically adding your custom inputs. "dynamicData" is the ID of an element wrapped around the form
above found from here: http://weblogs.asp.net/imranbaloch/archive/2011/03/05/unobtrusive-client-side-validation-with-dynamic-contents-in-asp-net-mvc.aspx
Out of interest, what happens if you just get the form to validate?
<script type="text/javascript">
$("form").submit(function (evt) {
// validate here should trigger invalid fields
$('form').valid();
// JSON POST...
// stop form submitting
evt.preventDefault();
});
</script>

Asp.Net MVC - ValidationMessage() - how to prevent HTML escaping?

In my scenario I'd like to display a text with URL link within validation message attached to form element on MVC view. I'm using ValidationExtensions.ValidationMessage extension method, like this:
<%=Html.ValidationMessage(Model.Name) %>
The behavior I'm seeing is that validation message is HTML escaped, what effectively prevents me from adding a link to message. Is there a way to circumvent this behavior? My error messages aren't user-supplied, so I don't think I have to worry about output sanitization here...
I'm guessing that since Html.ValidationMessage is built in you're going to either create your own version, or if you're feeling creative, since it returns a string, assign that and then unescape the characters you want to change back.
string validation = Html.ValidationMessage(Model.Name);
validation = Regex.Replace(validation, ">", "<");
//etc...
You could use the HttpUtility.HtmlDecode(...) method along with your Html.ValidationMessage(...) method to get 'err done :D

Resources