Reputation: 7291
I have the following class
class FormValidator{
public function __construct() {}
public function __destruct(){}
private $value;
public function Value($value){
$this->value = trim($value);
return $this;
}
public function Required(){
if(empty($this->value)){
return false;
}
else{
return $this;
}
}
public function MinLength($length){
$len = strlen($this->value);
if($len < $length){
return false;
}
else{
return $this;
}
}
}
In my php code, I'm calling -
$validator = new FormValidator();
$result = $validator->Value("")->Required()->MinLength(5)->SomeOtherMethod();
The above line gives the error Call to a member function MinLength() on a non-object ...
UPDATE: I require to stop to call MinLength() if Required() returns false.
How can I make my statement functioning?
Upvotes: 2
Views: 871
Reputation: 896
Instead of working with Exceptions (the solution of @Jeto), you can also work with an array holding all errors. A benefit of this solution is that you'll get multiple errors in one run, instead of breaking at the first error.
<?php
class FormValidator
{
private $value;
private $_errors = array();
public function Value($value)
{
$this->value = trim($value);
return $this;
}
/**
* @return $this
*/
public function Required()
{
if (empty($this->value)) {
$this->_errors[] = 'Value is required';
}
return $this;
}
/**
* @param int $length
* @return $this
*/
public function MinLength($length)
{
$len = strlen($this->value);
if ($len < $length) {
$this->_errors[] = "Value should be at least {$length} characters long.";
}
return $this;
}
public function hasErrors(){
return (count($this->_errors) > 0);
}
public function getErrors(){
return $this->_errors;
}
}
$validator = new FormValidator();
$validator->Value("1234")->Required()->MinLength(5);
if($validator->hasErrors()){
echo implode('<br>',$validator->getErrors());
}
Upvotes: 2
Reputation: 580
In the following methods you are calling to a method on a boolean value.
Required();
MinLength();
In order to solve this problem in the MinLength method:
if($len < $length){
// As a flag
$this->failed = true;
return $this;
}
and the SomeOtherMethod():
public function SomeOtherMethod() {
if (! $this->failed) {
// do something...
} else {
// do nothing...
}
}
Do the same for the
Requied()
method
Upvotes: 1
Reputation: 14927
You should make use of exceptions instead, which would handle errors while the methods themselves would always return the current instance.
This becomes:
<?php
class FormValidationException extends \Exception
{
}
class FormValidator
{
private $value;
public function Value($value): self
{
$this->value = trim($value);
return $this;
}
/**
* @return $this
* @throws FormValidationException
*/
public function Required(): self
{
if (empty($this->value)) {
throw new \FormValidationException('Value is required.');
}
return $this;
}
/**
* @param int $length
* @return $this
* @throws FormValidationException
*/
public function MinLength(int $length): self
{
$len = strlen($this->value);
if ($len < $length) {
throw new \FormValidationException("Value should be at least {$length} characters long.");
}
return $this;
}
}
Usage:
$validator = new FormValidator();
try {
$result = $validator->Value("lodddl")->Required()->MinLength(5);
} catch (\FormValidationException $e) {
echo 'Error: ', $e->getMessage();
}
Demo: https://3v4l.org/OFcPV
Edit: since OP is using PHP 5.2 (sadly), here's a version for it, removing \
s before root namespace, return type declarations and argument types.
Demo for PHP 5.2: https://3v4l.org/cagWS
Upvotes: 3
Reputation: 4826
cause method Required() returns false
not Class object
:
if(empty($this->value)){
return false;
}
You must change your code to.
$result = $validator->Value("");
if($result->Required()){ // if is not false do
$result->MinLength(5)->SomeOtherMethod();
}else{
// do anything if value is empty.
}
Upvotes: 1