• Status: Solved
  • Priority: High
  • Security: Private
  • Views: 48
  • Last Modified:

Form validation with ajax/php mvc and json

This validation is working but I just wanted to check if it is actually acceptable practice to do what I am doing.

I have a Ajax.php file which is a controller within my controllers folder.

In there I have an Ajax class with an addAccom method which looks like:

public function addAccom()
		$response = array();
		$message = '';
		if($_POST['category'] == '') {
			$message .= "Category required <br />";
		if(empty($_POST['name'])) {
			$message .= "Name required <br />";
		if(empty($_POST['description'])) {
			$message .= "Description required";
		if($message) {
			$response['success'] = false;
			$response['message'] = $message;
		} else {
			$response['success'] = true;
			$response['message'] = "All good";
		echo json_encode($response);

Open in new window

I then have some jQuery in my scripts.js file being called in the footer. The jQuery appends the data to divs I have in the view. It is working fine but I just wanted to check that this is okay using PHP and MVC or if I am breaking some rules?

I do plan on sending data to the model and performing a database insert as well.
Black Sulfur
Black Sulfur
  • 3
  • 2
2 Solutions
zephyr_hex (Megan)DeveloperCommented:
Your validation is fine.  However, you may want to consider using jQuery validation.  It's far more robust than trying to write comprehensive validation yourself.  You can also use "required" attributes on your HTML elements.


You might also consider unobtrusive validation.  https://github.com/aspnet/jquery-validation-unobtrusive

Don't forget that you will still need to do server side validation after the form is submitted, before you do your database insert.
Black SulfurAuthor Commented:
Thanks Zephyr_hex,

I was more asking if doing it like how I did was okay using php MVC and OOP, because it doesn't look very OOPy.
zephyr_hex (Megan)DeveloperCommented:
You could tuck these validation rules inside your model class.  That would leave the Controller to make the call to the method / function in the class, so it's not performing any "business logic"

this is client side validation (i.e.  called via AJAX), so "MVC" isn't really in the picture... you're in the VIEW / client side.

I generally keep my server side validation in the model.
Black SulfurAuthor Commented:
Interesting. I am new to MVC so my opinions or 2 cents don't really matter, but I thought the model was meant to be just for the database interaction? Without ajax, I do my validation in the controller and if the validation passes I then send the data to the model. But like I said, I am new to this so I probably don't know what I am talking about.
zephyr_hex (Megan)DeveloperCommented:
There are two forms of validation:  client side and server side.  Client side validation happens with HTML attributes or jQuery.  It is considered an optional thing, to help improve user experience.  Server side validation happens in your server side code.  I've seen it in the Controller, but that's really not maintaining separation of concerns because the Controller should just be a mechanism for working between the Model and View.  Server side validation should go in the Model.

If you decorate your Model properties with Data Attributes, MVC will load the proper HTML attributes.  So, while the Data Attributes are declared in the Model, they are used in client side validation.  They are also used in server side validation.  That being said, there are cases when server side validation should be done with a method in the Model -- when the Data Attribute declarations are not thorough enough.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now