Jump to content

Recommended Posts

I set up a class as a trial. I have 7 files in my project right now. I want to make them into 2 files. an index file and a class_lib file. So I set up my first class as a trial and it seems to be working, but there is a problem in my code. I don't get any errors right now, but what I think should happen isn't happening. I have a form that asks your name, when you put it in and press submit, it ads the name to a list. All is good. So now I want to add some validation. So I set up an error class as a test run.

 

class error {
var $error = "You must put in a name";
var $good = "good work, name has been entered";
function myError(){
$pattern = '/[A-Za-z]+/';


if(isset($_POST['go']) && !empty($_POST['name']) && preg_match($pattern, $_POST['name']))
{
  		 return $this->good;
}
else
{
	return $this->error;
}
}
}

 

my issue is that when I run the index page to view the form, the $error code is already visible in the div. It doesn't wait for me to put in a name or to match the $pattern. What is wrong with my logic here? I am calling the class in my index like this

 

[code

<div>

<?php

$errorCode = new error();

echo $errorCode->myError();

?>

</div>

[/code]

 

thanks for your help

Link to comment
https://forums.phpfreaks.com/topic/179272-help-with-class/
Share on other sites

Try wrapping the test in count($_POST) to be sure the form has been submitted, your condition is evaluating false because

 

a) isset($_POST['go']) is false until form is submitted

b) !empty($_POST['name']) is false until form submitted

c) preg_match($pattern, $_POST['name']) is false because form not submitted

 

EG:

 

<?php
class error {
   var $error = "You must put in a name";
   var $good = "good work, name has been entered";
function myError(){
   $pattern = '/[A-Za-z]+/';

if(count($_POST)>0){
// form submitted
   if(isset($_POST['go']) && !empty($_POST['name']) && preg_match($pattern, $_POST['name']))
   {
         return $this->good;
   }
   else
   {
      return $this->error;
   }
}else{
// form not submitted
return '';
}
}
}

 

 

Link to comment
https://forums.phpfreaks.com/topic/179272-help-with-class/#findComment-945851
Share on other sites

Well, the problem stems from a lack of clarity of what an object like this should do, and how it should be invoked.

 

First, if you have PHP 5+, use its OOP syntax.

 

Second, one of the main points behind OOP is to decouple the nitty-gritty code from the main system.  Unfortunately, your class does the opposite.  The class shouldn't be concerning itself with whether or not the submit button has been pressed, for example.  That's for the system code to worry about.

 

Without getting too far into OOP design (which is a topic too broad for this post), to get fast results with relatively good code, do something like:

 

class SimpleValidator
{
   private $regEx = '/[A-Za-z]+/';

   public function validate($data)
   {
      if (!empty($data) && preg_match($regEx, $data))
      {
         return "Name successfully entered.";
      }
      else
      {
         return false;
   }
}

//end of the class code

if (isset($_POST['submit']))
{
   $validator = new SimpleValidator();
   $result = $validator->validate($_POST['name']);

   if ($result)
   {
      echo $result
   }
   else
   {
      echo "Something went wrong";
   }
}

Link to comment
https://forums.phpfreaks.com/topic/179272-help-with-class/#findComment-945867
Share on other sites

Your problem is that if any of the conditions evaluate to false it'll trigger your error (so say if $_POST['go'] isn't set it'll trigger the error statement). You need to do something like this:

 

eg.

if(!isset($_POST['go']))
     return;
if(!empty($_POST['name']) && preg_match($pattern, $_POST['name']))
{
         return $this->good;
}
else
{
         return $this->error;
}

Link to comment
https://forums.phpfreaks.com/topic/179272-help-with-class/#findComment-945868
Share on other sites

@Nightslyr

 

I kinda see what you are saying. But at this point, your sollution is over my head. As you suggested, it's a problem of oop design. I do need to learn that. At this point, I will settle for making the code work the way I can understand it.

 

@AlexWD

 

How does my code trigger the false statement. Here is the way I understand what I have in plain english. Tell me how this is wrong.

 

if(isset($_POST['go'])

To me means that if the submit button is pushed

 

!empty($_POST['name']

 

To me means that if the textbox for the name isn't empty

 

preg_match($pattern, $_POST['name']

 

Means that if if the name submitted by the form matches $pattern then run the first if which says good work, name has been entered

 

The only way I think the error should be returned is if all the above turn out to be false. Which is happening, but how can they happen when the form is run the field is empty and the button has not been pushed, there is nothing to match the $pattern.

Link to comment
https://forums.phpfreaks.com/topic/179272-help-with-class/#findComment-945880
Share on other sites

But you're using && &&, so it means if any of them are false to trigger the else.

@AlexWD

 

I see what you are saying in your code, but I still get nothing with that. No error message or success message.

I thought that's what you wanted: not to display an error if the form hasn't been submitted..

Link to comment
https://forums.phpfreaks.com/topic/179272-help-with-class/#findComment-945885
Share on other sites

it is. The idea is that if they type in the wrong thing that doesn't match $pattern, then they get the error. If they type in a name that does, then they are told it worked. The issue to start was that it was putting up the error when I load the page. No matter if I did something or not. I understand the && But I thought it would have still been dependent on what came before. I guess it's not. But still I don't see why it's not working based on what is there.

 

if the button hasn't been pushed  {

    then return

}

 

if the name field isn't empty && the name field matches $pattern{

    then return $good

}

else

{

  return bad

}

 

I see what you are saying. Why the error would display. Now that I just typed this out. So I see the need to split them up.

Link to comment
https://forums.phpfreaks.com/topic/179272-help-with-class/#findComment-945892
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.