REST API : Codeigniter 3 validation in model - avenirer/CodeIgniter-MY_Model - php

I need to perform validation in model. So I have defined rules like below in model (Model_lead).
public $rules = array(
'create_put' => array(
'leadname' => array(
'field'=>'leadname',
'label'=>'Username',
'rules'=>'trim|required'
),
'emailid' => array(
'field'=>'emailid',
'label'=>'Email',
'rules'=>'trim|valid_email|required',
'errors' => array ('required' => 'Error Message rule "required" for field email',
'trim' => 'Error message for rule "trim" for field email',
'valid_email' => 'Error message for rule "valid_email" for field email'
)
)
)
);
And in controller (lead) I validate the input data and insert.
function create_put() {
$this->load->model('Model_lead');
$this->load->library('form_validation');
$rules = array('leadname'=>'Avenirer','lastname'=>'Row','emailid'=>'email#example.com');
$this->form_validation->set_rules($this->Model_lead->rules);
if ($this->form_validation->run('create_put') == TRUE) {
$id = $this->db->insert($this->Model_lead->table, $rules);
echo $id;
echo "true";
}else{
echo validation_errors();
echo "false";
}
}
When I try with the above code it does not perform any validation and throws no error. Can anyone help me with how to perform validation in model.
I'm using the library https://github.com/avenirer/CodeIgniter-MY_Model

Related

CI3 / Validation always returns false upon initial load

I do not understand why upon load the validation always returns false. Here is part of my controller:
// load up the validation rules for blog Info form
$this->config->load('mh_blog_validate');
$this->form_validation->set_rules($this->config->item('validate_blog_update'));
if ($this->form_validation->run('validate_blog_update') === FALSE) {
$errors = array('message' => $this->upload->display_errors());
$message = array('message' => 'Warning - '.$errors['message'],
'class' => 'danger',
);
$this->data['alert'] = bootstrap_alert($message);
}
Here is my validation config from mh_blog_validate:
$config['validate_blog_update'] = array(
'title' => array(
'field' => 'title',
'label' => '',
'rules' => 'required|trim|xss_clean|min_length[5]|callback_is_slug_unique_on_update[]',
'errors' => array(
'required' => 'The title cannot be blank.',
'min_length' => 'The title must be 5 charaters or more.',
'is_unique' => 'The title must be unique.',
'is_slug_unique_on_update' => 'The new title needs to be unique'
),
),
'body' => array(
'field' => 'body',
'label' => '',
'rules' => 'required|trim|xss_clean|min_length[5]',
'errors' => array(
'required' => 'The body cannot be blank',
'min_length' => 'The body must be 5 charaters or more.',
)
),
); // end validate_blog_create
This is the callback function I use in the validate:
function is_slug_unique_on_update() {
$new_slug = url_title($this->input->post('title'));
if ( $new_slug == $this->input->post('slug')) {
// no change in slug so update
// echo "no change in title";
return TRUE;
} elseif ( $new_slug !== $this->input->post('slug')) {
// new slug
$result = $this->Blog_model->is_slug_unique_on_update($new_slug);
return $result; // returns FALSE if the title is not unique
}
}
The output I receive in the view is "Warning - " and this is placed in the view:
if (isset($this->data['alert']){
echo $this->data['alert'];
}
I was expecting the validation not to produce an error because I have not submitted the form. It runs the validation maybe(?) even when I have not submitted the form I think.
+++ new edit +++
Added code below that works and wish to know why mine code doesn't. I thought my code follows the same pattern, no?
class Form extends CI_Controller {
public function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required',
array('required' => 'You must provide a %s.')
);
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
$this->form_validation->set_rules('email', 'Email', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');
}
else
{
$this->load->view('formsuccess');
}
}
}
The problem is you are setting $this->data['alert'] values, whether the form is submitting data or not. Of course you could prevent this variable assignment by adding conditional so it will set only when there are any $_POST data is submitted :
// load up the validation rules for blog Info form
$this->config->load('mh_blog_validate');
$this->form_validation->set_rules($this->config->item('validate_blog_update'));
if ($this->form_validation->run('validate_blog_update') === FALSE) {
if ($_POST)
{
$errors = array('message' => $this->upload->display_errors());
$message = array('message' => 'Warning - '.$errors['message'],
'class' => 'danger',
);
$this->data['alert'] = bootstrap_alert($message);
}
}

phalcon validate form fields without saving to database

Am in a need to validate form fields and manipulate them with out saving to database.
This is what i have done
In controller
<?php
use Phalcon\Mvc\Model\Criteria;
use Phalcon\Paginator\Adapter\Model as Paginator;
use Phalcon\Mvc\View;
class UsersController extends ControllerBase {
public function loginAction() {
if($this->request->isPost()) {
$user = new Users();
$validates = $user->validation($this->request->getPost());
// now validation works fine, but cancelOnFail in model doesn't seems to work,
if($validates) {
echo 'valid inputs';
}
else {
print_r($user->getMessages());
// now how can we show these error messages below the corresponding input fields in the view.
// we would also like to show error message as follows, if a field has more than one validation conditions,
// Eg: say username have notempty and valid e-mail validation set in model so if username is empty show only not empty message,
// similarly if username is not empty and if its not a valid e-mail , show not valid email message.
}
exit();
}
}
}
?>
Am trying to validate from the Model and it looks like as follows
<?php
use Phalcon\Mvc\Model\Validator;
use Phalcon\Mvc\Model\Validator\PresenceOf;
use Phalcon\Mvc\Model\Validator\Email;
class Users extends \Phalcon\Mvc\Model {
public function validation() {
$this->validate(new PresenceOf(
array(
'field' => 'username',
'message' => 'Username is required.',
'cancelOnFail' => true
)
));
$this->validate(new Email(
array(
'field' => 'username',
'message' => 'Username must be a valid e-mail.'
)
));
$this->validate(new PresenceOf(
array(
'field' => 'password',
'message' => 'Password is required.'
)
));
return $this->validationHasFailed() != true;
}
}
?>
My view file is as follows
<?php
echo $this->tag->form(array("users/login", "role" => "form"));
echo $this->tag->textField(array('username', 'class' => 'form-control', 'placeholder' => 'E-mail', 'type' => 'email', 'tabindex' => 1));
echo $this->tag->passwordField(array('password', 'class' => 'form-control', 'placeholder' => 'Password', 'type' => 'password', 'tabindex' => 2));
echo $this->tag->submitButton(array('Login','class' => 'btn btn-sm btn-success btn-block', 'tabindex' => 5));
?>
</form>
How can i achieve the following,
1) Check if form fields validates correctly as given in the Model from the Controller.
2) Am not looking to save the form data, only validate it.
3) Show the corresponding error messages below the input field in the view.
Thankz
You need to create the form, bind your entity and then validate on post request. See http://docs.phalconphp.com/en/latest/reference/forms.html#validation
EDIT: To display error messages you can do this in your controller
// ...
$messages = array();
foreach ($user->getMessages() as $message) {
$messages[$message->getField()] = $message->getMessage();
}
$this->view->messages = $messages;
//...
now you have $messages in your view.
I think you really should use a form in this case. You trying to validate a user login in model, but is a simple form validation. In model, you validates business rules of users in the app.

cakephp update more field unique

I have a site developed in cakephp.
I have a model called User like this:
class User extends AppModel {
public $name = 'User';
public $validate = array(
'username' => array(
'not_empty' => array(
'rule'=> 'notEmpty',
'message'=> 'Username not empty'
)
),
'email' => array(
'email_invalid' => array(
'rule' => 'email',
'message' => 'Invalid mail'
),
'email_unique' => array(
'rule' => 'isUnique',
'message' => 'Mail already exist inside database'
)
)
);
public function beforeSave(){
if (isset($this->data['User']['password'])){
$this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
}
}
}
Into my validate I have the rules email_unique that check if inside the database is already present another email equal.
When I update a user I make this inside my controller:
$this->User->id = $this->request->data['User']['id'];
if ($this->User->save($this->request->data)) {
$this->redirect (array ('action'=>'index'));
}
else{
$this->Session->write('flash_element','error');
$this->Session->setFlash ('Error');
}
It always fail because email isn't unique but is the same record!
I would like to know what is the best method to escape the validation if the save is an update not a create?
Or something like: check if the page is edit escape validation or I don't know.. maybe there are many system, I would like to know what is the more correct for my problem.
Thanks
You can adjust your validation rules to only apply when a new record is created, not when an existing record is updated. You can do this by setting the on key in your validation rule to create, so it will look like this:
'email_unique' => array(
'rule' => 'isUnique',
'message' => 'Mail already exist inside database',
'on' => 'create' // Only apply this rule upon creation of a new record
)
See the documentation on this for further details.
If you also want to block duplicate e-mails upon updating, create a beforeSave method in your User model, looking for the e-mail address:
public function beforeSave($options = array()) {
// If the email key is set in the data to be saved...
if (isset($this->data[$this->alias]['email'])) {
// Make sure the email is not already in use by another user
if ($this->find('count', array(
'conditions' => array(
$this->alias . '.id !=' => $this->data[$this->alias]['id'],
$this->alias . '.email' => $this->data[$this->alias]['email']
)
)) > 0) {
// The email is found for a user with another id, abort!
return false;
}
}
}

Display Error Message for Custom Validation in Laravel 4

I have created a custom error function by creating a class;
<?php
class CoreValidator extends Illuminate\Validation\Validator
{
public function validatePostcode($attribute, $value, $parameters = null)
{
$regex = "/^((GIR 0AA)|((([A-PR-UWYZ][0-9][0-9]?)|(([A-PR-UWYZ][A-HK-Y][0-9][0-9]?)|(([A-PR-UWYZ][0-9][A-HJKSTUW])|([A-PR-UWYZ][A-HK-Y][0-9][ABEHMNPRVWXY])))) [0-9][ABD-HJLNP-UW-Z]{2}))$/i";
if(preg_match($regex ,$value)) {
return true;
}
return false;
}
}
I reference it in my model;
public static $rules = array(
'first_name' => 'required|Max:45',
'surname' => 'required|Max:45',
'address_line_1' => 'required|Max:255',
'address_line_2' => 'Max:255',
'address_line_3' => 'Max:255',
'town' => 'required|Max:45',
'county' => 'Max:45',
'postcode' => 'required|Postcode',
'phone_number' => 'required|Max:22'
);
It has been registered in my global.php;
Validator::resolver(function($translator, $data, $rules, $messages) {
return new CoreValidator($translator, $data, $rules, $messages);
});
It all works well, but the error message it returns is
validation.postcode
How/where do I set a custom error message for this?
I have tried setting app/lang/en/validation.php with (neither work);
'custom' => array(
"validation.postcode" => "my error message 1",
"postcode" => "my error message 2"
)
P.S. I know that there is a regex validation method already, but this problem is more generic for me.
I think I have cracked it.
I added the message to the main array in app/lang/en/validation.php, not into the custom sub-array.
return array(
...
"url" => "The :attribute format is invalid.",
"postcode" => "my error message 2",
...
)
If this isn't the correct way, then someone is free to correct me.
You can use setCustomMessages() method to assign custom messages like the bellow code
<?php
class CoreValidator extends Illuminate\Validation\Validator
{
private $custom_messages = array(
"customvalidation" => "my error message.",
);
public function __construct($translator, $data, $rules, $messages = array(), $customAttributes = array())
{
parent::__construct($translator, $data, $rules, $messages, $customAttributes);
$this->setCustomMessages($this->custom_messages);
}
public function validateCustomvalidation($attribute, $value, $parameters = null)
{
// validation code here
}
}
maybe this code more better :
// for example I am using sub-array custom at default validation file, but you can do it in other file as you wishes.
..other..
'custom' => array(
'email' => array(
'required' => 'We need to know your e-mail address!',
),
"required" => "Hey!!! don't forget at :attribute field is required.",
),
..other..
// you can determine your custom languages at your wishes file
$messages = \Lang::get('validation.custom');
Validator::make($input, $rules, $messages);
From documentation:
In some cases, you may wish to specify your custom messages in a language file instead of passing them directly to the Validator. To do so, add your messages to custom array in the app/lang/xx/validation.php language file.
'custom' => array(
'email' => array(
'required' => 'We need to know your e-mail address!',
),
),
That means, in your case,
'custom' => array(
'postcode' => array(
'PostCode' => 'error message for PostCode rule',
'required' => 'error message for required rule',
),
),
If you want to utilize the custom validation messages array in app/lang/xx/validation.php, the correct way is as follows:
'custom' => array(
'formFieldName' => array(
'postcode' => 'error message for PostCode rule',
'iamalwayslowercase' => 'error message for this rule'
),
),
Note that you use the name of the form field and then in the array you use the lowercased name of the rule.
The code below also works perfectly, take note of the underscore on the index of the $customValidatorMessages array. Hope it helps someone :-)
class CoreValidator extends Illuminate\Validation\Validator
{
/**
* The array of custom validator error messages.
*
* #var array
*/
protected $customValidatorMessages = array();
public function validatePostcode($attribute, $value, $parameters = null)
{
$regex = "/^((GIR 0AA)|((([A-PR-UWYZ][0-9][0-9]?)|(([A-PR-UWYZ][A-HK-Y][0-9][0-9]?)|(([A-PR-UWYZ][0-9][A-HJKSTUW])|([A-PR-UWYZ][A-HK-Y][0-9][ABEHMNPRVWXY])))) [0-9][ABD-HJLNP-UW-Z]{2}))$/i";
if(preg_match($regex ,$value)) {
return true;
}
$this->customValidatorMessages['post_code'] = 'Postcode error message.';
$this->setCustomMessages($this->customValidatorMessages);
return false;
}
}

CakePHP - Password confirmation not allowing user to submit registration

I'm trying to set up validation for user registration but I'm having troubles. When I only have the email,role and password fields in the $validation array (remove the others) it works and will save a new user. When I try to add the other fields it fails and gives the flash message error "The user could not be saved. Please, try again."
I'm pretty sure it's the re_password check. When I remove the validation for that it works. However, the re_password validation does display an error when the passwords are different, so I'm not sure where to look
Here's my users table
id | email | password | location | website | role | created | modified
Here's the validation requirements. To get it to save a new user I have to remove everything but email, password and role.
public $validate = array(
'email' => 'email'
,
'password' => array(
'required' => array(
'rule' => array('minLength', '8'),
'message' => 'A password with a minimum length of 8 characters is required'
)
),
're_password' => array(
'required' => array(
'rule' => array('equalTo', 'password' ),
'message' => 'Both password fields must be filled out'
)
),
'role' => array(
'valid' => array(
'rule' => array('inList', array('admin', 'author')),
'message' => 'Please enter a valid role',
'allowEmpty' => false
)
),
'location' => array(
'valid' => array(
'rule' => array('notEmpty'),
'message' => 'Please select a location'
)
)
);
Here's the form (the options array is above, figured it's not necessary to show)
echo $this->Form->input('email');
echo $this->Form->input('password');
echo $this->Form->input('re_password', array('type'=>'password', 'label'=>'Re-Enter Password', 'value'=>'', 'autocomplete'=>'off'));
echo $this->Form->input('location', array('options' => $options, 'label' => 'Select Nearest Location'));
echo $this->Form->input('website',array('label'=>'Enter your website, such as www.example.com. '));
echo $this->Form->input('role', array('type' => 'hidden', 'default' => 'user'));
Here's the re_password checking function in the User model
function check_user_password($userid) {
$salt = Configure::read('Security.salt');
$this->User->id = $userid;
$hashed_password = $this->User->field('password');
// check password
if($hashed_password == md5($data['User']['re_password'].$salt)) {
return true;
} else {
return false;
}
}
And finally, here's the add function in UsersController
public function add() {
if ($this->request->is('post')) {
$this->User->create(); //create initiates a form on User/add.ctp
if ($this->User->save($this->request->data)) { //save the form data
$this->Session->setFlash(__('The user has been saved'));
$this->redirect(array('controller' => 'demos', 'action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
}
}
}
Please let me know if there's anything else you need to see
I believe that your re_passwords valiadtion rule equalTo compares its value to string password and not the actual field. I like to use custom functions for this.
so try replacing re_passwords rule array
//'rule' => array('equalTo', 'password' ),
'rule' => array('equalToField', 'password'),
and declare equalToField function in that model
function equalToField($array, $field) {
return strcmp($this->data[$this->alias][key($array)], $this->data[$this->alias][$field]) == 0;
}
** Also in the future when you seem to have a problem with validation rules
try this in your controllers action (its faster than removing every single rule)
if ($this->User->save($this->request->data)) {
...
} else {
debug($this->User->validationErrors);
...
}
I hope this helps.
Hi Please use following code for your requirement :
override equalTo function by putting your own method in user model:
function equalTo( $field=array(), $compare_field=null )
{
foreach( $field as $key => $value ){
$v1 = $value;
$v2 = $this->data[$this->name][ $compare_field ];
if($v1 !== $v2) {
return FALSE;
} else {
continue;
}
}
return TRUE;
}
Attention, in #luboss answer, where he declares:
function equalToField($array, $field) {
return strcmp($this->data[$this->alias][key($array)], $this->data[$this->alias][$field]) == 0;
}
That cannot work as we are comparing inconsistent fields:
the left member of strcmp has already been hashed, but not the right member.
This happens as a CakePHP automation because the field is called password.
The way I got this to work was to reuse the hashing function in the equalToField helper:
public function equalToField($array, $field) {
$valueFirstOccurrence = $this->data[$this->alias][$field];
$valueSecondOccurrence = Security::hash($this->data[$this->alias][key($array)], $type = 'sha1', $salt = true) ;
return !strcmp($valueFirstOccurrence, $valueSecondOccurrence);
}
Other point :
If you are interested in adding a minLength validation field for your password field, you want to read this good post first:
minLength data validation is not working with Auth component for CakePHP

Categories