Validation

validation, both serverside and clientside, is essential for a satisfactory user experience, and for security. 

This application makes use of several forms, so it is important that the site be completely tamper-proof.

Thankfully, Laravel takes helps the cause, with the @csrf tag, which prevents csrf attacks, or cross-site request forgery. (explain this).

validation on the registration is handled largely by the livewire auth package, the only tweak made is validation for the username, which was covered in the auth blog post. which leaves 5 forms to validate:

  1. create subweddit
  2. create post
  3. edit post
  4. create comment
  5. create reply

Client side, all of the validation can be handled by laravel's @error directive, which will quickly determine if there is a validation error in an attribute.




The message can be displayed by calling the array $errors. With our Bulma framework, we can make things look a little prettier for the user by changing the form border to red and displaying the red error message.



1. create subwedddit

In accordance with Reddit.com, 

  • Subweddit names are required, must be unique, contain no spaces (much like usernames), with a max character count 20. 
  • Bio is required, and can have a max char count of 64,000
  • The logo must be an image, with an image mime type such as jpg, png or gif (laravel automatically detects the mime type, smart!)

The request can be validated in the store method by laravel's validate() function like so:




2. create post

create post is very similar to creating a subweddit except

  • A post can have a longer character limit, at 255
  • an image is not required, a post can simply have a title and body
like so:




3.edit post

edit post has the exact same validation as create, and as such the edit method has repeated code, the same as the store method.

4/5. create comment/reply

create comment, and create reply have the same validation but call two different store methods, as a reply will attach a parent_id.

The validation it does have is solely that the comment cannot be too long, about 10,000 characters to be precise, is what Reddit allows.





Comments

Popular posts from this blog

Final Database Design

Views and Policies