Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PHP - Validation Add validation that checks: $title and $message both have a min

ID: 3800920 • Letter: P

Question

PHP - Validation

Add validation that checks:

$title and $message both have a minimum length of 3.

title has a maximum length of 32 characters.

$message has a maximum length of 52 characters.

If any of the three validation rules fail, print a status of 432 and an error message of what is wrong with the user input. The JSON response should look like:

If the user submitted a successful post, save the record using the provided Thread class. Set the timeposted set to when the item was submitted. Make the thread visible as soon as its submitted. Print a status code of 200. The JSON response should look like:

Add your code to newthread.php. Test your script by running:

newthread.php

Explanation / Answer

<?php

if ($argc != 3)
{
throw new Exception('Invalid number of arguments provided');
}

$title = $argv[1];
$message = $argv[2];

$thread = new Thread();

// Actual implementation below

if(strlen($title) < 3)
{
$json = '{"status" : 432,"error" : "title field too short"}';
}
else if(strlen($title) > 32)
{
$json = '{"status" : 432,"error" : "title field too long"}';
}
else if(strlen($message) < 3)
{
$json = '{"status" : 432,"error" : "message field too short"}';
}
elseif(strlen($message) > 52)
{
$json = '{"status" : 432,"error" : "message field too long"}';
}
else
{
//$timestamp = time(). ' '. date('Y-m-d');
$timestamp = date("m/d/y G:i:s");
echo($timestamp." UTC");
$json = '{"status" : 200}';
}

echo ' ' . $json;