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 SulfurAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.