-
-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add validation (where applicable) of fields to prevent unhandled exception #36
Comments
Sounds good. The core library is supposed to throw the exception if the email is malformed so I'm guessing it's only the validation parts missing in the templates. |
I'll might try to go for a PR on a validation example (e-mail address of comments I believe is a good candidate) of the Razor Template - if not anyone beats me to it, that is. |
I'm struggling with this as well. How can do the following:
I'm using the pre-built templates e.g. In the backing model, I'm trying to intercept the save and validation. The validation appears to be on the load and it's looking at the most recent comment in the list. I'm sure I'm doing this wrong, but I can't seem to find a way to intercept and validate it first. At the very minimum, how can I trap the error and gracefully display a message? [PostType(Title = "Standard Post")]
public class StandardPost : Post<StandardPost>
{
public StandardPost() : base()
{
Intercepts();
}
public void Intercepts()
{
App.Hooks.Comments.RegisterOnValidate(comment =>
{
if (comment.Author.Trim().Length == 0) comment.Author = "guest";
if (comment.Email.Trim().Length == 0) comment.Email = "guest@guest.com";
});
App.Hooks.Comments.RegisterOnBeforeSave(comment =>
{
if (comment.Author.Trim().Length == 0) comment.Author = "guest";
if (comment.Email.Trim().Length == 0) comment.Email = "guest@guest.com";
});
} |
I ended up coming up with this pattern. It works, but I'd like some feedback on how you would implement it. [PostType(Title = "Standard Post")]
public class StandardPost : Post<StandardPost>
{
}
public class PostModel: SinglePostWithComments<StandardPost>
{
public string Errors { get; set; }
public PostModel(Piranha.IApi api, Piranha.AspNetCore.Services.IModelLoader loader): base(api, loader)
{
}
public override async Task<IActionResult> OnGet(Guid id, bool draft = false)
{
CheckForErrorMessages();
return await base.OnGet(id, draft);
}
public override async Task<IActionResult> OnPostSaveComment(Guid id, bool draft = false)
{
try
{
return await base.OnPostSaveComment(id, draft);
}
catch (Exception ex)
{
var uri = $"{HttpContext.Request.Headers["Referer"]}?error={ex.Message}&#comments";
return Redirect(uri);
}
}
private void CheckForErrorMessages()
{
try
{
Request.Query.TryGetValue("error", out var error);
Errors = error;
}
catch { }
}
} |
Add some basic input validation to the templates. Would not only prevent Piranha from crashing if invalid input is made but also provide a good example of how to do it.
For instance, an invalid e-mail address of a comment will cause an exception that will crash the application. I assume this is the proper behavior of Piranha.Core but it would be both pedagogical and better if application/templates won't crash upon quite normal 'misbehavior' of a site visitor.
Steps to reproduce the exception:
Output of application:
The text was updated successfully, but these errors were encountered: