Ok I don't know what's not working. I know my form validation is definitely working because all my other functions work properly, but I am setting messages whether it's true OR false and none of them show up so I feel like it's skipping right over the validation rule.. which is weird...
$this->form_validation->set_rules('region', 'required|valid_region');
The rule in MY_Form_validation.php in my libraries folder. The library IS loaded first. As I said all my other validations work properly such as my reCaptcha and everything.
function valid_region($str) {
$this->load->database();
if($this->db->query('SELECT id
FROM region
WHERE name = ?
LIMIT 1', array($str))->num_rows() == 0) {
//not a valid region name
$this->set_message('valid_region', 'The %s field does not have a valid value!');
return false;
}
$this->set_message('valid_region', 'Why is it validating?');
}
None of the messages will set so I have a feeling nothing is validating!
set_rules() function takes 3 parameters
The field name - the exact name you've given the form field.
A "human" name for this field, which will be inserted into the error message.
For example, if your field is named "user" you might give it a human
name of "Username". Note: If you would like the field name to be
stored in a language file, please see Translating Field Names.
The validation rules for this form field.
You put the validation rules as second parameter. That is why the validation is not running. Try this instead:
$this->form_validation->set_rules('region', 'Region', 'required|valid_region');
instead of
$this->form_validation->set_rules('region', 'required|valid_region');
try
$this->form_validation->set_rules('region', 'required|callback_valid_region');
when using custom validation rules you should use
callback to prepend the function name.
UPDATE
and use
$this->form_validation->set_message
instead of
$this->set_message
and in function valid_region
use return true when validation is successfull
$this->form_validation->set_rules('region', 'Region', 'required|valid_region');
function valid_region() {
$str = $this->input->post('name_of_input');
$this->load->database();
if($this->db->query('SELECT id
FROM region
WHERE name = ?
LIMIT 1', array($str))->num_rows() == 0) { // why compare "=" between `name` field and array() ?
//not a valid region name
$this->form_validation->set_message('valid_region', 'The %s field does not have a valid value!');
return false;
}
$this->form_validation->set_message('valid_region', 'Why is it validating?');
return true;
}
Related
There's a way to add custom error messages to CodeIgniter validation_errors();?
Example, if I wanted a field with a 123456 value, and the user inputs 12345 I'd want to set a message to say:
The number 6 is required!
And any other custom rules I may want to add. Like a specific pattern or any other things.
Sorry for my english.
Yes, that is possible.
Set rules with callback like,
$this->form_validation->set_rules('field_name', 'Number', 'callback_custom_validation');
and define callback in the same controller like,
public function custom_validation($str)
{
if ($str != '123456')
{
$this->form_validation->set_message('field_name', 'The %s field requires 123456');
return false;
}
else
{
return true;
}
}
Display your errors in view with <?php echo form_error('field_name')?>
More info on callbacks here.
I'm using Laravel for a project and want to know how to validate a particular scenario I'm facing. I would like to do this with the native features of Laravel if this is possible?
I have a form which has two questions (as dropdowns), for which both the answer can either be yes or no, however it should throw a validation error if both of the dropdowns equal to no, but they can both be yes.
I've check the laravel documentation, but was unsure what rule to apply here, if there is one at all that can be used? Would I need to write my own rule in this case?
very simple:
let's say both the fields names are foo and bar respectively.
then:
// Validate for those fields like $rules = ['foo'=>'required', 'bar'=>'required'] etc
// if validation passes, add this (i.e. inside if($validator->passes()))
if($_POST['foo'] == 'no' && $_POST['bar'] == 'no')
{
$messages = new Illuminate\Support\MessageBag;
$messages->add('customError', 'both fields can not be no');
return Redirect::route('route.name')->withErrors($validator);
}
the error messge will appear while retrieving.
if you get confuse, just dump the $error var and check how to retrieve it. even if validation passes but it gets failed in the above code, it won't be any difference than what would have happened if indeed validation failed.
Obviously don't know what your form fields are called, but this should work.
This is using the sometimes() method to add a conditional query, where the field value should not be no if the corresponding field equals no.
$data = array(
'field1' => 'no',
'field2' => 'no'
);
$validator = Validator::make($data, array());
$validator->sometimes('field1', 'not_in:no', function($input) {
return $input->field2 == 'no';
});
$validator->sometimes('field2', 'not_in:no', function($input) {
return $input->field1 == 'no';
});
if ($validator->fails()) {
// will fail in this instance
// changing one of the values in the $data array to yes (or anything else, obvs) will result in a pass
}
Just to note, this will only work in Laravel 4.2+
I'm trying to make a validation for 2 field that must have a different value. I only know how to set the rules for validate matching value.
$this->form_validation->set_rules('book1','Book1','required|matches[book2]');
$this->form_validation->set_rules('book2','Book2','required|matches[book1]');
if I input book1=novel, and book2=novel, the code above will return TRUE.
How can I validate 2 field where the value of each field is not matching each other? So if I input book1=novel and book2=comic, it will return TRUE.
You should use callback_ method for custom validation, CI form validation library does not provide notMatch type validation rule, see below sample code.
$this->form_validation->set_rules('book1','Book1','required');
$this->form_validation->set_rules('book2','Book2','required|callback__notMatch[book1]');
AND place method in controller class
function _notMatch($book2Value, $book1FieldName){
if($book2Value != $this->input->post($book1FieldName){
$this->form_validation->set_message('_notMatch', 'book1 and book2 values are not matching');
return false;
}
return true;
}
In codeigniter 3 you can use the differs[] set rule, to enforce that field values don't match.
$this->form_validation->set_rules('book1', 'Book 1', 'required|differs[book2]');
$this->form_validation->set_rules('book2', 'Book 2', 'required|differs[book1]');
This means you don't need to create an unnecessary callback. However, for older versions you will.
See the documentations for more: Codeigniter 3 Documentation
You can use differs like so:
$this->form_validation->set_rules('password', 'current password', 'max_length[25]|min_length[5]|required');
$this->form_validation->set_rules('new_password', 'new password', 'max_length[25]|min_length[5]|required|differs[password]');
$this->form_validation->set_rules('confirm_password', 'confirm password', 'required|max_length[25]|min_length[5]|matches[new_password]');
Well here is a example error Codeigniter will shoot out at the user if they don't type in their email.
email: The Email field is required.
Now the "email" part specifically. How can I change it to whatever I like? It seems its using the inputs name/id.
Example of how I setup form validation for email.
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[user.email]');
So maybe a better example .. say I wanted to change it from saying "email" to "The Email"
Thanks in advnace.
You should use set_message function in form_validation library.For example;
$this->form_validation->set_message('email', '%s is entered email adress, it's wrong!');
CodeIgniter's user-guide is very easy and interactive.You should check the documentation for these type of basic questions.
CodeIgniter user-guide
This is how I do it:
If you want to alter the message use set_message:
$this->form_validation->set_message('required', 'Your custom message here');
Using a Callback is a better way to do it:
$this->form_validation->set_rules('username', 'Username', 'check_user_name');
public function username_check($str)
{
if (strlen($str)<0)
{
$this->form_validation->set_message('check_user_name', 'The %s field is empty"');
return FALSE;
}
else
{
return TRUE;
}
}
I have a Profile form that inherits from sfGuardRegisterForm
I have these fields:
$this->useFields(
array('first_name',
'last_name',
'email_address',
'country',
'language',
'current_password',
'new_password',
'password_again',
)
);
Required fields are:
email_address, country and language
And the conditions are:
If the email_address is not equal with the current email_address
then check if it's unique then save it
If the current_password is the actual password of the user then verify if new_password and password_again are equals and verify that the new_password is not equal to the actual password of the user
I just can't figure out in how implement this
EDIT
Thanks 1ed your example works but the problem is that I load the user Profile and I fill the fields: 'first_name', 'last_name', 'email_address', 'country', 'language' with the actual logged user so the email_address field will show the email address:
//...
$this->widgetSchema['email_address']->setDefault($this->_user->getEmailAddress());
//...
If the user dont change the email it will always show this message:
An object with the same "email_address" already exist.
I just want to skip that
Also this $this->getObject()->checkPassword() does not works, always show this message:
Incorrect current password.
I use:
$this->_user = sfContext::getInstance()->getUser()->getGuardUser();
To get actual user profile
EDIT2
Thanks again 1ed
This is very weird and I'm getting frustated, this is the situation
I have a "workaround" for this but it does not follow the standard, I can make it works but using sfContext::getInstance()->getUser()->getGuardUser(); and it will be more unnecesary code
If I use new ProfileForm($user) automatically fills all the fields, that's very good but I can't setDefault() I can't set null or empty any field so I can't use doUpdateObject() because this function only works when the current data is updated, also I have tested overriding bind(), save() etc. without results
email_address uniqueness: you should set unique: true in schema, in sfDoctrineGuardPlugin that's the case by default, so in BasesfGuardUserForm you should see a unique validator already: sfValidatorDoctrineUnique(array('model' => 'sfGuardUser', 'column' => array('email_address'))
current_password: you should create a callback type post validator for this
// in sfGuardRegisterForm::configure()
// these fields can be blank
$this->getValidator('current_password')->setOption('required', false);
$this->getValidator('new_password')->setOption('required', false);
$this->getValidator('password_again')->setOption('required', false);
// check the current password (this validator is not `required` by default)
$this->mergePostValidator(new sfValidatorCallback(array(
'callback' => array($this, 'checkPassword'),
), array(
'invalid' => 'Incorrect current password.'
)));
// add this method to the same form class
public function checkPassword(sfValidatorBase $validator, array $values, array $arguments)
{
// if a new password is given check whether the old one is correct or not and rise an error if not correct
if(0 != strlen($values['new_password']) && !$this->getObject()->checkPassword($values['current_password']))
{
throw new sfValidatorErrorSchema($validator, array(
'current_password' => new sfValidatorError($validator, 'invalid')
));
}
return $values;
}
Alternatively you can create a custom post validator, but I think it's not necessary.
EDIT:
If you would like to display empty email address just like the password fields add these to your form class:
// at form load remove the default value
protected function updateDefaultsFromObject()
{
parent::updateDefaultsFromObject();
if (isset($this['email_address']))
{
$this->setDefault('email_address', '');
}
}
// before save remove empty email address
protected function doUpdateObject($values)
{
if (isset($values['email_address']) && 0 == strlen($values['email_address']))
{
unset($values['email_address']);
}
parent::doUpdateObject($values);
}
I'll try and explain this in methodical terms instead of giving you a big block of code....
So first, you want to if (email_addr != current_email) and if that's true, go on to do
if (new_pass != current_pass) then follow on to make sure if (new_pass == new_pass_again)
Inside all of these IFs, you can return a true/false or some kind of flag, or just //do code inside the brackets :p
EDIT: encase these IFs in: if (country != NULL && language != NULL && email_addr != NULL)