Skip to content

Conversation

@Sagar-Sharma19-cmd
Copy link

This PR adds a server-side validation check in PetController to prevent creating a Pet with an empty name.

  • Prevents invalid pet entries
  • Provides clear error message to users
  • Ensures consistent validation with the update form

@abdurraffaysyed
Copy link

@Sagar-Sharma19-cmd
Is it possible to handle such validations in the model class instead of Controller. I think that would be a simple and clean way to define the constraints. What do you say?

public String processCreationForm(Owner owner, @Valid Pet pet, BindingResult result,
RedirectAttributes redirectAttributes) {
// Validate empty name
if (!StringUtils.hasText(pet.getName())) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why here, not in the Pet model?

RedirectAttributes redirectAttributes) {
// Validate empty name
if (!StringUtils.hasText(pet.getName())) {
result.rejectValue("name", "required", "Pet name must not be empty");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like "required" already has a message associated with it, and it doesn't start with a capital letter - the third argument here is probably redundant?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants