Is there best practice about display errors in during controller action executing on view? I mean, how do you display errors when ‘login was not found in database’ or ‘input value has incorrect format’, or ‘field require value’?
Is there array with errors or anything else? Now my code looks like:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$siteName = isset($_POST['SiteName']) ? trim($_POST['SiteName']) : null;
$siteUrl = isset($_POST['SiteUrl']) ? trim($_POST['SiteUrl']) : null;
if(IsNullOrEmptyString($siteName) || IsNullOrEmptyString($siteUrl)) {
exit('Site name or site url could not be empty');
} elseif (!preg_match('/^http\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?$/', $siteUrl)){
exit('Site url has wrong format');
}
$filters = array('SiteName' => $siteName, 'SiteUrl' => $siteUrl);
if($sitesRepository->select($filters)) {
exit('Site is already exist');
}
}
But if i use exit, my page doesn’t continue to display.
If you mean you want to store all the errors and keep processing, you can put them into an array. Before you process define an array and then add each error to that array. For example:
Then replace your exit statements with:
When you’re done processing, you can check for and output any errors:
or