NotEmpty validator doesn't work with custom form element - php

I've created a custom form element for phone numbers. It is composed of a select for the international prefix and of a text for the actual phone number.
class Mylib_Form_Element_Phone extends Zend_Form_Element_Xhtml
{
public $helper = 'formPhone';
protected $prefix;
protected $phone;
public function setValue($value)
{
if (is_null($value)) {
return;
}
if (!is_array($value) || !isset($value['prefix']) || !isset($value['phone'])) {
throw new Exception('Invalid phone value provided');
}
$this->prefix = $value['prefix'];
$this->phone = $value['phone'];
}
public function getValue()
{
return array(
'prefix' => $this->prefix,
'phone' => $this->phone
);
}
public function isValid($value, $context = null)
{
$valid = parent::isValid($value, $context);
if ($valid && !empty($this->phone) && empty($this->prefix)) {
$this->addError('PHONE_PREFIX_REQUIRED');
return false;
}
return $valid;
}
}
My custom validation error works fine but if the element is created with 'required' => true and i left both fields empty, no error are triggered.
How to you make the NotEmpty validator works with custom array element?

Related

PHP class has two methods with different parameters, how to take advantage of the constructor?

I want to make a class let's say it's called validation, it has two functions, either registrationVal or loginVal.
I want to pass data from two different pages that will use the same class. I want to initialize the variables in a constructor so it looks cleaner... how can I do this?
How can I call the constructor from one page when it doesn't have all the variables to be passed to the constructor?
For example:
registration page
$obj = new validation($password, $password2, $email, $phone);
$obj->registrationVal();
login page
$obj = new validation($email, $password);
$obj->loginVal();
You can make something like this. It's not the best code - but for start is not bad.
<?php
class Validator
{
private array $params;
private array $validated = [];
private array $errors = [];
public function __construct(array $params)
{
$this->params = $params;
$this->validate();
}
private function validate(): void
{
foreach ($this->params as $field => $param){
$validationMethod = 'validate' . ucfirst(strtolower($field));
if(! method_exists($this, $validationMethod)){
continue;
}
if($error = $this->{$validationMethod}($param)){
$this->errors[$field] = $error;
}else{
$this->validated[$field] = $param;
}
}
}
public function validateOrFail(): void
{
if($this->hasErrors()){
throw new ValidationException($this->getErrors());
}
}
public function validated(): array
{
return $this->validated;
}
private function validateLogin($value): ?string
{
//validation logic - return null on success or error string
return $validation_result;
}
public function __get($name)
{
return $this->params[$name] ?? null;
}
public function getErrors(): array
{
return $this->errors;
}
public function hasErrors(): bool
{
return count($this->errors) > 0;
}
}
You must write validation methods such validateLogin() for login field or validatePassword() for password field.
How it working:
$params = [
'login' => 'login',
'password' => 'password'
...
];
$validator = new Validator($params);
//it will throw exception when validation failing.
$validator->validateOrFail();
//or you can manually working with errors
if($validator->hasErrors()){
...
}
$password = $validator->password;
//or you can get only validated fields
$params = $validator->validated();

Array to string conversion error -Slim Twig-

Setup: I have a slim application and I pulled in Illuminate DB and Twig view.
if (!$validator->passed()) {
$errors = $validator->errors();
$users = User::all();
return $this->view($response, 'auth.login', compact('errors','users'));
}
Problem: When I run the above code, I am able to retrieve the users variable in my view, but the errors variable throws the following error.
Notice: Array to string conversion in /Users/davidchen/Documents/sites/slim.com/vendor/twig/twig/lib/Twig/Environment.php(378) : eval()'d code
on line
70 Array
The errors variable returns a multidimensional array, below you'll find the result that I get from print_r($errors).
Array (
[username] => Array (
[0] => username already exists
)
[password] => Array (
[0] => password must consist of at least 6 characters
)
)
Here are my related project files:
Twig Setup File (app.php)
$c = $app->getContainer();
$capsule = new \Illuminate\Database\Capsule\Manager;
$capsule->addConnection($config['config']['db']);
$capsule->setAsGlobal();
$capsule->bootEloquent();
$c['db'] = function($c) use ($capsule){
return $capsule;
};
$c['view'] = function($c){
$options['cache']=false;
$view = new \Slim\Views\Twig(__DIR__."/../views", $options);
$view->addExtension(new \Slim\Views\TwigExtension(
$c->router,
$c->request->getUri()
));
$view->getEnvironment()->addGlobal('flash', $c->flash);
return $view;
};
$c['flash'] = function($c){
return new Slim\Flash\Messages();
};
Validator Class
namespace App\Models\Auth;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Capsule\Manager as DB;
use Carbon\Carbon;
use DateTime;
class Validator extends Model
{
protected $field_name,
$data,
$errors = [];
/*
* Main validator
*/
public function __construct($request, $fields = []){
$data = $request->getParams();
$this->data = $data;
foreach ($fields as $field => $constraints) {
$this->field_name = $field;
if (isset($data[$field])) {
$field_value = $data[$field];
foreach (explode("|", $constraints) as $constraint) {
$obj = explode(":", $constraint);
$function_name = $obj[0];
if (isset($obj[1])) {
if(method_exists($this, $function_name))
{
$this->$function_name($obj[1],$field_value);
}
}
}
}else{
if (strpos($constraints, 'required') !== false) {
$validator->report($validator->field_name.' field is requried');
}
}
}
return $this;
}
/*
* Object Interface Methods
*/
private function report($message){
$this->errors[$this->field_name][]= $message;
}
public function errors(){
return $this->errors;
}
public function passed(){
if (!count($this->errors)) {
return true;
}
}
/*
* Validation Rules
*/
public function max($length,$value){
if (strlen($value)>$length) {
$this->report("{$this->field_name} must consist of less than {$length} characters");
}
}
public function min($length,$value){
if (strlen($value)<$length) {
$this->report("{$this->field_name} must consist of atleast {$length} characters");
}
}
public function distinct($tableName,$value){
if (DB::table($tableName)->where($this->field_name, $value)->exists()) {
$this->report("{$this->field_name} already exists");
}
}
public function date($format,$date){
if (!preg_match("/\d{4}-\d{2}-\d{2}\b/",$date)) {
$this->report("incorrect {$this->field_name} values");
}else{
$d = DateTime::createFromFormat($format, $date);
if ($d && $d->format($format) !== $date) {
$this->report("{$this->field_name} format should be {$format}");
}
}
}
public function match($matchField,$value){
if (isset($this->data[$matchField])) {
$valueTwo = $this->data[$matchField];
if ($value !== $valueTwo) {
$this->report("{$this->field_name} does not match {$matchField}");
}
}else{
$this->report("{$matchField} is required");
}
}
public function format($type,$value){
switch ($type) {
case 'noWhiteSpace':
if (preg_match("/\s/",$value)) {
$this->report("{$this->field_name} may not contain any spaces");
}break;
case 'alpha':
if (preg_match("/[^a-zA-Z]/",$value)) {
$this->report("{$this->field_name} may only contain letters");
}break;
case 'alphaNum':
if (preg_match("/[^a-zA-Z0-9]/",$value)) {
$this->report("{$this->field_name} may only contain letters");
}break;
case 'email':
if (!filter_var($value, FILTER_VALIDATE_EMAIL)) {
$this->report("in correct {$this->field_name} format");
}break;
default:
# code...
break;
}
}
}
Base Controller
namespace App\Controllers;
/**
*
*/
class Controller
{
protected $c;
function __construct($container)
{
$this->c = $container;
}
public function view($response, $path,$variables = []){
$this->c->view->render($response, str_replace(".","/",$path).".twig", $variables);
}
public function pathFor($routeName,$data = []){
return $this->c->router->pathFor($routeName,$data);
}
}
Auth Controller
namespace App\Controllers\Auth;
use App\Models\User\User;
use App\Controllers\Controller;
use App\Models\Auth\Validator;
/**
*
*/
class AuthController extends Controller
{
public function getLogin($request, $response){
return $this->view($response, 'auth.login');
}
public function postLogin($request, $response){
$validator = new Validator($request,[
'username'=>'required|min:3|max:64|format:alphaNum|distinct:users',
'password'=>'required|min:6|max:64|',
]);
if (!$validator->passed()) {
$errors = $validator->errors();
$users = User::all();
return $this->view($response, 'auth.login', compact('errors','users'));
}
return $this->view($response, 'home.login');
}
}
login.twig file
login.twig file
Hope one of you can shed some light on this problem. I've been struggling with this all morning.
You could try to loop over each item in a sequence. For example, to display a list of users provided in a variable called users:
<h1>Members</h1>
<ul>
{% for user in users %}
<li>{{ user.username|e }}</li>
{% endfor %}
</ul>
Read more

Zend- 1048 Column 'member_login' cannot be null

I tried to set up a sign up logic but suffer a problem said
Message: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'member_login' cannot be null, query was: INSERT INTO members (member_login) VALUES (?)enter image description here
After struggling for hours, still no ideas which go wrong. Here is my source code.
Anyone can give me some ideas?
My Model.php
<?php
class Application_Model_Member
{
protected $_id;
protected $_member_login;
public function __construct(array $options = null)
{
if (is_array($options)) {
$this->setOptions($options);
}
}
public function __set($name, $value)
{
$method = 'set' . $name;
if (('mapper' == $name) || !method_exists($this, $method)) {
throw new Exception('Invalid member property');
}
$this->$method($value);
}
public function __get($name)
{
$method = 'get' . $name;
if (('mapper' == $name) || !method_exists($this, $method)) {
throw new Exception('Invalid member property');
}
return $this->$method();
}
public function setOptions(array $options)
{
$methods = get_class_methods($this);
foreach ($options as $key => $value) {
$method = 'set' . ucfirst($key);
if (in_array($method, $methods)) {
$this->$method($value);
}
}
return $this;
}
public function setId($id)
{
$this->_id = (int) $id;
return $this;
}
public function getId()
{
return $this->_id;
}
public function setMemberLogin($text)
{
$this->_member_login = (string) $text;
return $this;
}
public function getMemberLogin()
{
return $this->_member_login;
}
}
My MemberMapper.php
<?php
class Application_Model_MemberMapper
{
protected $_dbTable;
public function setDbTable($dbTable)
{
if (is_string($dbTable)) {
$dbTable = new $dbTable();
}
if (!$dbTable instanceof Zend_Db_Table_Abstract) {
throw new Exception('Invalid table data gateway provided');
}
$this->_dbTable = $dbTable;
return $this;
}
public function getDbTable()
{
if (null === $this->_dbTable) {
$this->setDbTable('Application_Model_DbTable_Members');
}
return $this->_dbTable;
}
public function save(Application_Model_Member $member)
{
$data = array(
'member_login' => $member->getMemberLogin(),
);
if (null === ($id = $member->getId())) {
unset($data['member_id']);
$this->getDbTable()->insert($data);
} else {
$this->getDbTable()->update($data, array('member_id = ?' => $id));
}
}
public function find($id, Application_Model_Member $member)
{
$result = $this->getDbTable()->find($id);
if (0 == count($result)) {
return;
}
$row = $result->current();
$member->setId($row->member_id)
->setMemberLogin($row->member_login);
}
public function fetchAll()
{
$resultSet = $this->getDbTable()->fetchAll();
$entries = array();
foreach ($resultSet as $row) {
$entry = new Application_Model_Member();
$entry->setId($row->member_id)
->setMemberLogin($row->member_login);
$entries[] = $entry;
}
return $entries;
}
}
DbTable:
class Application_Model_DbTable_Members extends Zend_Db_Table_Abstract
{
protected $_name = 'members';
}
Form: Registration.php
<?php
class Application_Form_Auth_Registration extends Zend_Form
{
public function init()
{
$this->setMethod('post');
$this->addElement(
'text', 'member_login', array(
'label' => 'Username:',
'required' => true,
'filters' => array('StringTrim')
));
$this->addElement('submit', 'register', array(
'ignore' => true,
'label' => 'Sign up'
));
}
}
Signup controller:
public function signupAction()
{
$request = $this->getRequest();
$regform = new Application_Form_Auth_Registration();
if ($this->getRequest()->isPost()) {
if ($regform->isValid($request->getPost())) {
$member = new Application_Model_Member($regform->getValues());
$mapper = new Application_Model_MemberMapper();
$mapper->save($member);
return $this->_helper->redirector('/books/view');
}
}
$this->view->regform = $regform;
}
Finally I fix the bug. It go wrong with the naming of Element. For example, in your database you have "member_login", then the Element name should be sth like memberLogin.

PHP Form validation returning strange value

I recently made a very simple MVC'ish framework for my own. Now im trying to expand that framework with some classes, so i can save some time in the future. The class i am trying to make now is a form validation class. I thought it was working quite fine, untill i made a function to check for the minimum length of a string.
The class :
<?php
/**
* Form Validation Class
*/
class formValidation
{
private $errorMessages = array();
private $method;
public $errorString;
public $validationRules = array();
public function setMethod($method)
{
$this->method = $method;
}
public function setRules($rules)
{
$this->validationRules = $rules;
}
public function run()
{
if (!empty($this->method)) {
foreach ($this->method as $fieldname => $fieldvalue) {
foreach ($this->validationRules as $rule) {
if ($rule['field'] == $fieldname) {
foreach ($rule as $option => $rulevalue) {
switch ($option) {
case 'required':
if (!$this->checkRequired($fieldvalue)) {
$this->errorMessages[] = $rule['name'] . " is a required field";
}
break;
case 'letters_numbers':
if (!$this->checkLettersNumbers($fieldvalue)) {
$this->errorMessages[] = $rule['name'] . " may only contain letters and numbers";
}
case 'min_length':
if (!$this->checkMinLength($fieldvalue, $rulevalue)) {
$this->errorMessages[] = $rule['name'] . " must contain at least $rulevalue characters";
}
}
}
}
}
}
if (!empty($this->errorMessages[0])) {
return false;
} else {
return true;
}
} else {
return false;
}
}
// Checks
private function checkRequired($value) // Makes a field mandatory
{
if ($value == "") {
return false;
} else {
return true;
}
}
private function checkLettersNumbers($value) // Allow Letters and Numbers only
{
return preg_match('/^[A-Za-z\s ]+$/', $value);
}
private function checkMinLength($value, $minlength) // Check input for minimum length
{
if (strlen($value) < $minlength) {
return false;
} else {
return true;
}
}
// Error Handling Functions
private function makeErrorString($prelimiter = '<li>', $delimiter = '</li>') // Build all the errors into a string
{
if (!empty($this->errorMessages[0])) {
$errors = "";
foreach ($this->errorMessages as $message) {
$errors .= $prelimiter;
$errors .= $message;
$errors .= $delimiter;
}
$this->errorString = $errors;
return true;
}
}
public function ValidationErrors() // Return the error string
{
if ($this->makeErrorString()) {
return $this->errorString;
}
}
/**
* End of Class
**/
}
/**
* End of formValidation.php
**/
?>
And my controller file :
<?php
/**
* Homepage Controller
* #copyright 2012
*/
class home extends SimpleController
{
public function index()
{
$view = new view('home');
$val = new formValidation();
$val->setMethod($_POST);
$rules = array(
array(
'field' => 'test',
'name' => 'Test',
'required' => true,
'letters_numbers' => true,
'min_length' => '5'
),
array(
'field' => 'bla',
'name' => 'Trololol',
'required' => true
)
);
$val->setRules($rules);
if (!$val->run()) {
echo $val->ValidationErrors();
}
}
}
?>
In my view i have a form with 2 fields, test and bla. When i submit the form when it's empty, the checkMinLength() functions returns 2 value's :
Test must contain at least 1 characters
Test must contain at least 5 characters
First of all, where is it getting that 1 from, and 2nd why is it showing 2 messages for the same function ? I just can't find out why.
Hope you guys can help me out!
(The echo in the controller is just for demo)
Thanks!
You forgot a break in your switch, so you're falling through from the letters_numbers case into the min_length case.
The "rule value" for your letters_numbers rule is true, which converts to a string as 1.

Zend Framework validation and/or filtering issue

I am having a problem with the filtering and validation of the form below. If the two mobile numbers are entered in national format (07777654321) the form validation fails, but if they are entered in international format (447777654321) it validates. My understanding is that the filters will be applied first so when identical is applied both mobile and mobile_confirm should have been processed with the same set of filters.
So if I enter 07777654321 into both fields it will get filtered to 447777654321 in both fields which should then validate. Where am I going wrong.
I am using ZF 1.11.3.
<?php
class Test_Form_Check extends Zend_Form
{
public function init()
{
$mobile= new Zend_Form_Element_Text( 'mobile' );
$mobile->addFilter(new Test_Filter_MobileFilter('44'))
->addValidator(new Test_Validate_Mobile())
->setRequired( true );
$mobile1= new Zend_Form_Element_Text( 'mobile_confirm' );
$mobile1->addFilter(new Test_Filter_MobileFilter('44'))
->addValidator(new Test_Validate_Mobile())
->addValidator('Identical', false, array('token' => 'mobile', 'messages' => 'Mobile numbers do not match.'))
->setRequired( true );
$Add = new Zend_Form_Element_Submit('Add');
$Add->setLabel('Submit');
$this->addElement($mobile)
->addElement($mobile1)
->addElement( $Add );
}
}
class Test_Validate_Mobile extends Zend_Validate_Abstract
{
const NOT_DIGITS = 'notDigits';
const STRING_EMPTY = 'digitsStringEmpty';
const INVALID = 'digitsInvalid';
const INVALIDPHONE = 'phonenumberinvalid';
protected static $_filter = null;
protected $_messageTemplates = array(
self::NOT_DIGITS => "'%value%' must contain only digits",
self::STRING_EMPTY => "'%value%' is an empty string",
self::INVALID => "Invalid type given. String, integer or float expected",
self::INVALIDPHONE => "Invalid number, try with country code",
);
public function isValid($value)
{
if (!is_string($value) && !is_int($value) && !is_float($value)) {
$this->_error(self::INVALID);
return false;
}
if (!preg_match('/^(447)[0-9]{9}$/', $value)) {
$this->_error(self::INVALIDPHONE);
return false;
}
$this->_setValue((string) $value);
if ('' === $this->_value) {
$this->_error(self::STRING_EMPTY);
return false;
}
if (null === self::$_filter) {
require_once 'Zend/Filter/Digits.php';
self::$_filter = new Zend_Filter_Digits();
}
if ($this->_value !== self::$_filter->filter($this->_value)) {
$this->_error(self::NOT_DIGITS);
return false;
}
return true;
}
}
class Test_Filter_MobileFilter implements Zend_Filter_Interface
{
/**
* Default country code
*
* #var string
*/
protected $_def_country = '44';
public function __construct($options = null)
{
if ($options !== null) {
if (is_string($options) && is_numeric($options)) {
$this->_def_country = $options;
}
else {
require_once 'Zend/Filter/Exception.php';
throw new Zend_Filter_Exception('Options not valid country code');
}
}
}
public function filter($value)
{
if (!empty($value)) {
$replace = array(' ','+','-','(',')');
$value = str_replace($replace, '', $value);
$value = preg_replace('/\A0/', $this->_def_country, $value);
}
return $value;
}
}
I will be very grateful for any help with this. It is driving me round the bend.
I guess problem is with Zend_Validate_Identical validator , it uses unfiltered raw value from $_POST. Just quick fix:
class My_Validate_Identical extends Zend_Validate_Identical
{
/**
*
* #var Zend_Form_Element
*/
protected $_element = null;
public function __construct($token = null)
{
if (is_array($token) && array_key_exists('element', $token)) {
$this->_element = $token['element'];
}
parent::__construct($token);
}
public function isValid($value, $context = null)
{
$context[$this->getToken()] = $this->_element->getValue();
return parent::isValid($value, $context);
}
}
Attaching validator:
$mobile1->addValidator(new My_Validate_Identical(array(
'token' => 'mobile',
'messages' => 'Mobile numbers do not match.',
'element' => $mobile
)))
Hope this helps :)

Categories