Symfony2, validation on getters, show error on form - forms

I have a problem with errors using Symfony2 FormComponent.
I've validation for getters in bundle:
getters:
someGetter:
- MyOwnValidator: { message: "Fail"}
After that, I want to validate form and show errors:
{{form_errors(form)}}
returns to me bunch of errors (including error for someGetter), but
{{form_errors(form.someGetter)}}
doesn't return anything.
Any ideas?
UPDATE
I've found the solution. Field, for which I implemented validation (someGetter) is a collection type without 'error_bubbling' => 'false' option.
When I added error_bubbling, everything works fine.

Other options to have the getters error is with http://symfony.com/doc/current/reference/forms/types/entity.html#error-mapping and http://symfony.com/doc/current/reference/forms/types/entity.html#error-bubbling

I've found the solution. Field, for which I implemented validation (someGetter) is a collection type without 'error_bubbling' => 'false' option.
When I added error_bubbling, everything works fine.
Answer extracted from updated question.

Related

Laravel Backpack - Undefined array key "relation_type"

Laravel v9.9.0, Backpack v5.0.14, Backpack Pro v1.0.13
I'm getting an error when I try to save changes in Laravel Backpack CRUD: Undefined array key "relation_type"
Through trial and error, I've pinned the cause down to one of the fields defined in SetupCreateOperation (a standard Backpack method).
ListingCrudController setupCreateOperation:
CRUD::field('listing_category_id')
->label('Category')
->type('select2')
->entity('listingCategory')
->model("App\Models\ListingCategory")
->attribute('category_name')
->size(6)
->tab('Main');
I don't have any guards in play that would affect this, and believe the relationship is defined properly in the model.
Listing:
public function listingCategory()
{
return $this->belongsTo(ListingCategory::class);
}
Other fields are defined in a very similar way, and they all work as expected. The only difference (that I can see) is that this is the only entity/method name that is camel cased, but I've tried renaming 'listingCategory' to 'category'. I've also tried using 'select' instead of 'select2', neither edit made any difference.
I believe I could add relationship_type to the field definition in the controller but my (admittedly limited) understanding of the Backpack internals is that I shouldn't do this.
For clarity, the CRUD views (both create and update) seem fine and all of the options are populated for this field, it's only upon save that I see the error. If I remove the field from the setUpCreateOperation everything works just fine.
If relevant, this is all running in a local dev environment (PHP 8.0) on Windows 10.
I'd appreciate ANY suggestions as to what I'm doing wrong, or how to go about identifying the root issue.
Edit: output of dd() as requested...
"listing_category_id" => array:8 [▼
"name" => "listing_category_id"
"entity" => "listingCategory"
"label" => "Category"
"type" => "select2"
"model" => "App\Models\ListingCategory"
"attribute" => "category_name"
"wrapper" => array:1 [▼
"class" => "form-group col-md-6"
]
"tab" => "Main"
]
since you are using PRO, can you just use:
CRUD::field('listingCategory')
->label('Category')
->attribute('category_name')
->size(6)
->tab('Main');
If you are validating listing_category_id in request please validate the relation name listingCategory. It should work just fine.
Cheers

Get object from form with errors in play framework 2.0

I am getting an error when I try to extract a user from a form with validation errors.
I have the following route configured in my routes file:
GET /users/:user controllers.UsersController.viewUser(user: models.User)
GET /users/:user/edit controllers.UsersController.editUser(user: models.User)
This is fine at this point, and I can render a link to the user view from my scala templates:
routes.UsersController.viewUser(myUserObject)
My problem is that in my user edition form I need to get myUserObject from a Form[User] object. What I am currently doing is:
routes.UsersController.viewUser(userForm.get)
However, when the userForm has any errors, the get method raises an exception, as shown in the documentation.
The approach I have taken is passing an additional User parameter to the scala view, together with the Form[User] parameter I was passing up to now, I mean,
userEdit.render(user, userForm)
instead of just
userEdit.render(userForm)
However, I would like to know if there is a more suitable solution that does not involve including an additional parameter.
The documentation states that you can prefill a form with existing data:
val filledForm = userForm.fill(User("Bob", 18))
Given the preexisting User, it should be trivial to adapt to your example.

CakePHP 3 and form validation errors

I use cakePHP 3 and I have a sign in form with Form->input().
If on purpose I make an error, this error doesn't whow up under the Form field. It doesn't appear anywhere.
My code is like this:
$newUser = $this->Users->newEntity($this->request->data());
if (!$this->Users->save($newUser)) {
debug($newUser->errors());
$this->Flash->error('Error');
return;
}
Debug show the errors, but shouldn't they appear under each form element automatically?
ok I found the error.
I wasn't passing in Form->create the entiry but null. I did it like
$this->Form->create($entity...
and works nicely.

Rails 4 with CanCan: unknown attribute error after including load_and_authorize_resource

I'm working in Rails 4 and have gotten CanCan to work well with instructions from this issue, except for one use case that I think might be relatively common.
I have a Comment model, which has_many :comments, through: :replies for nested comments. All of this is working well, until I add load_and_authorize_resource to my comments controller. The problem seems to stem from a hidden field sending an optional :parent_comment_id attribute to my create action.
I've permitted this attribute via strong parameters:
def comment_params
params.require(:comment).permit(:content, :parent_comment_id, :post_id, :comment_id, :user_id)
end
So that I can create the association if a :parent_comment_id is included:
if comment_params[:parent_comment_id] != nil
Reply.create({:parent_comment_id => comment_params[:parent_comment_id], :comment_id => #comment.id})
end
But once I add load_and_authorize_resource, I get an unknown attribute error for :parent_comment_id. What am I missing?
Solution came to me in my sleep. Here's what I did to solve the problem:
The only reason comment_params wasn't normally having a problem on create, was because I was excluding the extra :parent_comment_id parameter, like this:
#comment = post.comment.create(comment_params.except(:parent_comment_id))
When CanCan used the comment_params method however, it did no such sanitation. Hence, the problem. It would have been messy to add that sanitation to CanCan on a per-controller basis, so I did what I should have done all along and instead of passing the :parent_comment_id inside :comment, I used hidden_field_tag to pass it outside of :comment and accessed it through plain, old params.
I hope this helps someone else who makes a similar mistake!

Validation / Error Messages in ASP.Net MVC 2 View Unrelated to a Property

What pattern can I use to display errors on an MVC 2 view that are not related to a single property?
For example, when I call a web service to process form data, the web service may return an error or throw an exception. I would like to display a user-friendly version of that error, but have no logical means to relate the error to any given property of the model.
UPDATE:
Trying to use this code as suggested, but no summary message is displayed:
MyPage.spark:
Html.ValidationSummary(false, "Oopps it didn't work.");
Controller:
ViewData.ModelState.AddModelError("_FORM", "My custom error message.");
// Also tried this: ViewData.ModelState.AddModelError(string.Empty, "My custom error message.");
return View();
UPDATE 2
What does this mean?
next to each field.
Instead of always displaying all
validation errors, the
Html.ValidationSummary helper method
has a new option to display only
model-level errors. This enables
model-level errors to be displayed in
the validation summary and
field-specific errors to be displayed
next to each field.
Source: http://www.asp.net/learn/whitepapers/what-is-new-in-aspnet-mvc#_TOC3_14
Specifically, how does one add a model level error (as opposed to a field-specific error) to the model?
UPDATE 3:
I noticed this morning that Html.ValidationSummary is not displaying any errors at all, not even property errors. Trying to sort out why.
Simply adding a custom error to the ModelState object in conjunction with the ValidationSummary() extension method should do the trick. I use something like "_FORM" for the key... just so it won't conflict with any fields.
As far as patterns go, I have it setup so that my Business Logic Layer (called via services from the controller) will throw a custom exception if anything expected goes wrong that I want to display on the view. This custom exception contains a Dictionary<string, string> property that has any errors that I should add to ModelState.
HTHs,
Charles