Object Oriented PHP #13 - Validation Class (part 2)
Вставка
- Опубліковано 19 вер 2024
- Hey gang, in this PHP OOP tutorial we'll flesh out the rest of our form validation class with methods for validating different form fields.
----------------------------------------
🐱💻 🐱💻 Course Links:
Course files - github.com/iam...
🐱💻 🐱💻 Other Related Courses:
+ PHP Tutorial for Beginners - • PHP Tutorial for Begin...
This is so useful for beginners! Great job, on making custom validations!
Thanks shaun for this wonderful tutorial. I used this class validators and add field to check using your pizza project in your PHP for beginners tutorial. Also, I added an update page so that users can update the pizza they added.
The most excited and clean explanation for beginners.
salut et un grand merci pour tes videos , je mrts juste une mise à jours : En raison de compatibilité ascendante, la fonction array_key_exists() peut aussi retourner true si key est une propriété définie dans un object, fourni comme paramètre array. Ce comportement est obsolète à partir de PHP 7.4.0, et a été retiré à partir de PHP 8.0.0.
Pour vérifier si une propriété existe dans un objet, property_exists() devrait être utilisé.
A real life example here, thank you!
why if data is also an array you don't have to declare that when you give the class properties? like $data = []; ?
is there a difference between trigger_error and just echoing the string saying there's an error?
same thought
Thank you very much brother . For your effort and clear explanation. Thumbs up :)
thank you very much for this, but i didn't understand how you're passing values to addError function and the constructor? asking as a beginner. thank you agian!
You are going to have a problem with that regex if you have weird letters, like "â" or "ë". But English doesn't use many of those. Through me for a loop once.
I think I went to a website the other day that used your preg_match for their password requirements. I could not enter a password that had more than 12 characters. :)
brother thank you very much you are doing a great job.I am a big fan of yours.
If submitted value is ok then what is process to save it into Database? Coulde show that too?
I have a question about the array-key-exists method. does it convert the values of the $_POST data or the $field array values into keys? or am I looking at the $_POST array wrong. I'm thinking the values inside the $_POST array come from the inputs that are submitted by the form... and the keys are zero indexed. or should I be asking what part of the submitted data becomes the key? is it the $_POST array or is it the $field array... i'm not too sure. other than my question, I really learn a lot from your tutorial and I can't thank you enough... I subscribed a long time ago and always a thumbs up from me.
I have a similar doubt.... If the function of that 'array_key_exists' is to check if it exists then why check if its empty???... I don't even know if I have understood these correctly... Explain... Thanks in advance @Son of Troy
I have the same question, I dont quite get how that function is working but I think each $field represents each key (email and username) and I guess that $data (that stores $post_data) is an array like ('username' => 'irina123', 'email' => 'irina@gmail.com') but I don't know where that array comes from.
UPDATE: It's been 45 minutes since I posted this and I think the best way to understand it is before doing anything, in the index.php file, var_dump($_POST) so you get what's inside, I did it and what I got is:
array(3) { ["username"]=> string(6) "irina123" ["email"]=> string(21) "irina@gmail.com" ["submit"]=> string(4) "Send" }
meaning that there are keys called username, email and submit and each one has a value
Irina Lyrics: yea I took a closer look at the functionality, php says array-key-exist checks the key of one array for existence . However in this situation it’s checking two separate arrays so it’s sort of a hack. The form input name fields are passed into the construct then a separate array has matching string element. The index of the separate array get converted into the form name input field somehow showing existence of a key... yea it’s crazy but now we know array-key-exist can be hacked just by having matching string elements that comes from a separate array
Very nice explained
Thank you so much!
You're very welcome Issa!
Sensei, this is Awesome!!
What if I have fields that belong to the same name let's say I have multiple options (radio buttons) which are named with the same input field name (i.e.: "gender"), is the array_key_exists() method gonna yell at me that one of these fields is not present in data even if I plug it into the $fields variable array as the keys would have a the same name?
filter_var($username, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW)
THANK YOU VERY MUCH !
Please add some tutorial for file handling using oop
why he put return; alone with no following code?
what is the construct for?
what is the difference between "return;" and "reuturn true;"?
🙏🙏🙏🙏🙏
So if a form contains 20 fields, you have to do this 20 times? How is this the best approach?
In a way it's the only approach, assuming that every field is unique. if they're not then you can probably get away with using the same method, but that's usually not the case if you have 20 fields
🥷 🥷 🥷
public function validateForm(){
foreach(self::$fields as $field){
if(!array_key_exists($field, $this->data)){
trigger_error("'$field' is not present in the data");
return; // *****why it is necessary ?******
}
}
$this->validateUsername();
$this->validateEmail();
return $this->errors;
}
what is the role of the return statement?