Validate another field if file field is not empty - php

I'm trying to validate a field if a file fields is not empty. So if someone is trying to upload a file, I need to validate another field to make sure they selected what they are uploading, however I don't know how to check to see, or run a rule only if the field is not empty.
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('full_name, gender_id','required'),
array('video', 'file', 'types'=>'mp4', 'allowEmpty' => true),
array('audio', 'file', 'types'=>'mp3', 'allowEmpty' => true),
array('video','validateVideoType'),
);
}
public function validateVideoType() {
print_r($this->video);
Yii::app()->end();
}
So this->video is always empty whether I just uploaded something or not. How do I check to see if that variable is set?

Custom validation function must be defined properly. It has two parameters always $attribute & $params.
public function validateVideoType($attribute, $params) {
print_r($this->video);
Yii::app()->end();
}
Now in this you should write your custom way to validate.
I am sure that would work fine.

You can check it with jQuery/javascript, where 'new_document' is the name of the input file field.
if ($("#new_document").val() != "" || $("#new_document").val().length != 0) {
//File was chosen, validate requirements
//Get the extension
var ext = $("#new_document").val().split('.').pop().toLowerCase();
var errortxt = '';
if ($.inArray(ext, ['doc','docx','txt','rtf','pdf']) == -1) {
errortxt = 'Invalid File Type';
//Show error
$("#document_errors").css('display','block');
$("#document_errors").html(errortxt);
return false;
}
//Check to see if the size is too big
var iSize = ($("#new_document")[0].files[0].size / 1024);
if (iSize / 1024 > 5) {
errortxt = 'Document size too big. Max 5MB.';
//Show error
$("#document_errors").css('display','block');
$("#document_errors").html(errortxt);
return false
}
} else {
//No photo chosen
//Show error
$("#document_errors").css('display','block');
$("#document_errors").html("Please choose a document.");
return false;
}
This code is obviously not perfect for your needs but may have the requirements to piece together what you need.

Related

return message if input is in array

I have a form validation that, so far, returns an error message if either of two defined words/phrase are present in the input area:
add_filter('gform_validation_3', 'custom_validation');
function custom_validation($validation_result){
$form = $validation_result["form"];
foreach($form['fields'] as &$field){
/* Check that the value of the field that was submitted. e.g. the name="input_1" that is generated by Gravity Forms */
if($_POST['input_4'] == "Your First Name" || "SEO"){
// set the form validation to false
$validation_result["is_valid"] = false;
//The field ID can be found by hovering over the field in the backend of WordPress
if($field["id"] == "4"){
$field["failed_validation"] = true;
$field["validation_message"] = "This field needs to be your actual first name.";
}
}
}
//Assign modified $form object back to the validation result
$validation_result["form"] = $form;
return $validation_result;
}
I'm not sure now how to create an array to define the words that are not allowed, so that I can have a much longer list?
First of all, the first "if" is incorrect, I think you meant:
if($_POST['input_4'] == "Your First Name" || $_POST['input_4'] =="SEO")
A good way to achieve what you long is:
$forbidden_words = ["Your First Name", "SEO"];
$is_valid = !in_array($_POST['input_4'], $forbidden_words); //false if the word is in array
After that you may go:
if($is_valid)
//do magic
You can use function in_array()
<?php
$blacklisted = ['some', 'ugly', 'bad', 'words'];
if(in_array('ugly', $blacklisted)){
echo('bad word spotted');
}
demo: https://repl.it/#kallefrombosnia/DarkvioletDeepPolygons

Parameters for activation page

I am just completely stumped at this and so is my buddy who created this template system.
I have a registration page that sends the user an email with a link to the account activation page in which they must fill out there password to confirm. Inside the link is their user_id and a random string for a registration key.
Here's what I normal url would look like :
kansasoutlawwrestling.com/kowmanager/activate/10000/da54d6fad5fa5fadf
What I want to do is if either of these statements are true then it shows my 404 error page:
Doesn't have the user_id in the url
Doesn't have the registration key in the url
Doesn't have either the two parameters in the url
Activate Controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Activate extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library('kow_auth');
}
public function index($param1 = NULL, $param2 = NULL)
{
//Config Defaults Start
$msgBoxMsgs = array();//msgType = dl, info, warn, note, msg
$cssPageAddons = '';//If you have extra CSS for this view append it here
$jsPageAddons = '<script src="http://www.kansasoutlawwrestling.com/kowmanager/assets/js/activatevalidate.js"></script>';//If you have extra JS for this view append it here
$metaAddons = '';//Sometimes there is a need for additional Meta Data such in the case of Facebook addon's
$siteTitle = '';//alter only if you need something other than the default for this view.
//Config Defaults Start
//examples of how to use the message box system (css not included).
//$msgBoxMsgs[] = array('msgType' => 'dl', 'theMsg' => 'This is a Blank Message Box...');
/**********************************************************Your Coding Logic Here, Start*/
$x = 0;
if(($param1 !== NULL)&&($param2 !== NULL))
{
//params not null yay..
if((isset($param1))&&((trim($param1) !== '')||(!empty($param1))))
{
if(!is_numeric($param1))
{
$x++;
}
}
if((isset($param2))&&((trim($param2) !== '')||(!empty($param2))))
{
if(!is_string($param2))
{
$x++;
}
}
}
else
{
$x++;
}
if($x !== 0)
{
$bodyContent = "error_page";
}
else
{
$bodyContent = "activate_form";
}
$bodyType = "full";//type of template
/***********************************************************Your Coding Logic Here, End*/
//Double checks if any default variables have been changed, Start.
//If msgBoxMsgs array has anything in it, if so displays it in view, else does nothing.
if(count($msgBoxMsgs) !== 0)
{
$msgBoxes = $this->msgboxes->buildMsgBoxesOutput(array('display' => 'show', 'msgs' =>$msgBoxMsgs));
}
else
{
$msgBoxes = array('display' => 'none');
}
if($siteTitle == '')
{
$siteTitle = $this->metatags->SiteTitle(); //reads
}
//Double checks if any default variables have been changed, End.
$this->data['msgBoxes'] = $msgBoxes;
$this->data['cssPageAddons'] = $cssPageAddons;//if there is any additional CSS to add from above Variable this will send it to the view.
$this->data['jsPageAddons'] = $jsPageAddons;//if there is any addictional JS to add from the above variable this will send it to the view.
$this->data['metaAddons'] = $metaAddons;//if there is any addictional meta data to add from the above variable this will send it to the view.
$this->data['pageMetaTags'] = $this->metatags->MetaTags();//defaults can be changed via models/metatags.php
$this->data['siteTitle'] = $siteTitle;//defaults can be changed via models/metatags.php
$this->data['bodyType'] = $bodyType;
$this->data['bodyContent'] = $bodyContent;
$this->load->view('usermanagement/index', $this->data);
}
function activate_submit()
{
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|min_length[6]|max_length[12]|alpha_numeric');
$user_id = $this->uri->segment(3);
$registration_key = $this->uri->segment(4);
if (($registration_key == '') OR ($user_id == ''))
{
echo json_encode(array('error' => 'yes', 'message' => 'URL was not complete!'));
}
else
{
if (!$this->form_validation->run())
{
echo json_encode(array('error' => 'yes', 'message' => 'There was a problem submitting the form! Please refresh the window and try again!'));
}
else
{
if ($this->kow_auth->activate_user($user_id, $registration_key, $this->input->post('password')))
{
echo json_encode(array('sucess' => 'yes', 'message' => 'Your account has been successfully activated!'));
}
else
{
echo json_encode(array('error' => 'yes', 'message' => 'The activation code you entered is incorrect or expired!'));
}
}
}
}
}
/* End of file activate.php */
/* Location: ./application/controllers/activate.php */
Routes:
$route['activate/:num/:any'] = 'activate/index/$1/$2';
$route['404_override'] = 'error';
Here's what I'm getting for each of those instances:
kansasoutlawwrestling.com/kowmanager/activate - correct
kansasoutlawwrestling.com/kowmanager/activate/10000/ - correct
kansasoutlawwrestling.com/kowmanager/activate/10000/271cce33ab11ced5fd10aeca41323a3c - incorrect should be showing the activate form
EDIT : Anybody have any ideas because it just seems like nothing is working.
I'll start by simplifying a bit the params checking:
$this->error = FALSE;
if(NULL != $param1 AND NULL != $param2)
{
if(!is_numeric($param1) OR (string)trim($param2)!= '')
{
$this->error = TRUE;
}
}
else
{
$this->error = TRUE;
}
$this->data['bodyContent'] = $this->error? 'error_page' : 'activate_form';
It's late here so I might messed up something, but basically:
if both params are null, set $error to TRUE (they don't have to be null);
if at least one isn't null:
- if param1 isn't numeric (userid) or
- if param2 isn't a string (nor even an empty one), $error is again TRUE.
In the end, if error is FALSE (as initialized), we pass the "activate_form" value to the view, else (i.e. if any of the above condition caused the error to be set to TRUE), we pass the "error_page" value.
Also, as per documentation, custom routes should go after fixed ones:
$route['404_override'] = 'error';
$route['activate/(:num)/(:any)'] = 'activate/index/$1/$2';
Out of curiosity...what happens if you remove the following line?
if(!is_string($param2))
And you just have:
if((isset($param2))&&((trim($param2) !== '')||(!empty($param2))))
{
$x++;
}
You dont need to create a new controlller/module for account activation, simply add a new method inside your existing auth controller/module.
IF you setup a route with conditions and they fail, your shown an error or 404.
class Auth extends CI_Controller
{
public function __construct(){parent::__construct();}
/**
* Activate user account
* $route['activate/(:num)/(:any)'] = 'auth/activate/$1/$2';
*/
public function activate($uid, $code)
{
//if need be, double check
if(!$uid OR !$code){show_404();} //BOTH need to exists
//if $route['activate/(:num)/(:any)'] = 'auth/activate/$1/$2'; FAILS CI will show error or 404
//grab $code and $uid and seek a match from DB, if failure do your own errors.
}
}
I would suggest removing the user id from the uri segment and make the activation code a UNIQUE db constraint so you only have to query for that.
Take a look at Tank Auth
It is a CI library that already does this, but with a key difference, you don't want to pass more than you have to. So just generate a HASH (encrypted for instance), that lets you find the userid & activate at the same time.
It is less checking and less issues with copy & pasting URL. Also eliminates having to do all this extra checking of ID validity + hash validity.
But as I said, look at the tank auth code, and pull out what you need for the activation part, it's fairly straight forward, and already for CI.

Cant load Validation Rules from both config file and using set_rules method

Is there any way to load validation rules from both : config file and using set rules method ?
Without modifying CodeIgniter's Form Validation class (CI_Form_validation), there is no way to load validation rules from a config file AND using the set rules method. As the form validation code currently operates, config file rules are only checked if no rules have been otherwise defined
You can extend the form validation class and get this to work, however, relatively simply. Create a file called MY_Form_validation.php and put it in the application/core/ directory.
class MY_Form_validation extends CI_Form_validation {
// You only need to change the run() method, so we'll define a (modified) version.
// This will override the existing run() method so that it uses rules set from
// set_rules() AND from the config file.
function run($group = '')
{
if (count($_POST) == 0)
{
return FALSE;
}
// If there are any configuration rules defined, go ahead and use them
if (count($this->_config_rules) != 0)
{
// Is there a validation rule for the particular URI being accessed?
$uri = ($group == '') ? trim($this->CI->uri->ruri_string(), '/') : $group;
if ($uri != '' AND isset($this->_config_rules[$uri]))
{
$this->set_rules($this->_config_rules[$uri]);
}
else
{
$this->set_rules($this->_config_rules);
}
}
// Load the language file containing error messages
$this->CI->lang->load('form_validation');
// Cycle through the rules for each field, match the
// corresponding $_POST item and test for errors
foreach ($this->_field_data as $field => $row)
{
// Fetch the data from the corresponding $_POST array and cache it in the _field_data array.
// Depending on whether the field name is an array or a string will determine where we get it from.
if ($row['is_array'] == TRUE)
{
$this->_field_data[$field]['postdata'] = $this->_reduce_array($_POST, $row['keys']);
}
else
{
if (isset($_POST[$field]) AND $_POST[$field] != "")
{
$this->_field_data[$field]['postdata'] = $_POST[$field];
}
}
$this->_execute($row, explode('|', $row['rules']), $this->_field_data[$field]['postdata']);
}
// Did we end up with any errors?
$total_errors = count($this->_error_array);
if ($total_errors > 0)
{
$this->_safe_form_data = TRUE;
}
// Now we need to re-set the POST data with the new, processed data
$this->_reset_post_array();
// No errors, validation passes!
if ($total_errors == 0)
{
return TRUE;
}
// Validation fails
return FALSE;
}
Note, I haven't tested this on a CodeIgniter installation. But it should work. Also note, this will prioritize config file rules over rules defined using the set_rules() method.

Checkbox on Kohana form in gallery3 software

I am using gallery3 php software, which is based on the kohana framework. Does anybody know how to add a checkbox to the album information form?
I tried like this:
static function get_edit_form($parent) {
$form = new Forge(
"albums/update/{$parent->id}", "", "post", array("id" => "g-edit-album-form"));
$form->hidden("from_id")->value($parent->id);
$group = $form->group("edit_item")->label(t("Edit Album"));
$group->input("title")->label(t("Title"))->value($parent->title)
->error_messages("required", t("You must provide a title"))
->error_messages("length", t("Your title is too long"));
$group->textarea("description")->label(t("Description"))->value($parent->description);
/* MPK: information fields for albums */
$group->textarea("information")->label(t("Information text"))->value($parent->information);
$group->checkbox("info")->label(t("Informational"))->value($parent->info);
if ($parent->id != 1) {
$group->input("name")->label(t("Directory Name"))->value($parent->name)
->error_messages("conflict", t("There is already a movie, photo or album with this name"))
->error_messages("no_slashes", t("The directory name can't contain a \"/\""))
->error_messages("no_trailing_period", t("The directory name can't end in \".\""))
->error_messages("required", t("You must provide a directory name"))
->error_messages("length", t("Your directory name is too long"));
$group->input("slug")->label(t("Internet Address"))->value($parent->slug)
->error_messages(
"conflict", t("There is already a movie, photo or album with this internet address"))
->error_messages(
"not_url_safe",
t("The internet address should contain only letters, numbers, hyphens and underscores"))
->error_messages("required", t("You must provide an internet address"))
->error_messages("length", t("Your internet address is too long"));
} else {
$group->hidden("name")->value($parent->name);
$group->hidden("slug")->value($parent->slug);
}
AND
public function update($album_id) {
access::verify_csrf();
$album = ORM::factory("item", $album_id);
access::required("view", $album);
access::required("edit", $album);
$form = album::get_edit_form($album);
try {
$valid = $form->validate();
$album->title = $form->edit_item->title->value;
$album->description = $form->edit_item->description->value;
/* MPK: information fields for albums */
$album->information = $form->edit_item->information->value;
$album->info = $form->edit_item->info->value;
$album->sort_column = $form->edit_item->sort_order->column->value;
$album->sort_order = $form->edit_item->sort_order->direction->value;
if (array_key_exists("name", $form->edit_item->inputs)) {
$album->name = $form->edit_item->inputs["name"]->value;
}
$album->slug = $form->edit_item->slug->value;
$album->validate();
} catch (ORM_Validation_Exception $e) {
// Translate ORM validation errors into form error messages
foreach ($e->validation->errors() as $key => $error) {
$form->edit_item->inputs[$key]->add_error($error, 1);
}
$valid = false;
}
if ($valid) {
$album->save();
module::event("item_edit_form_completed", $album, $form);
log::success("content", "Updated album", "view");
message::success(t("Saved album %album_title",
array("album_title" => html::purify($album->title))));
if ($form->from_id->value == $album->id) {
// Use the new url; it might have changed.
json::reply(array("result" => "success", "location" => $album->url()));
} else {
// Stay on the same page
json::reply(array("result" => "success"));
}
} else {
json::reply(array("result" => "error", "html" => (string)$form));
}
}
The field does show up on the form, but the field value does not get saved to the DB. In the DB it is a tinyint(1).
Kohana uses models to save data in the database. Because of $album->save(); you should have a model somewhere in the application, depending of the version of Kohana.
Go to /modules/gallery/models. There is a file called item.php. This is the model used by the application to save/load/create items (and also albums). At line 447 there is the command which actually saves the contents of the album in the database. You need to change that line in order to save the value of the checkbox.
Solved. The problem was that you have to use 'checked' field of the check-box and not the value field in the assignment.
In album.php
$group->checkbox("info")->label(t("Informational"))->value($parent->info)->checked($parent->info);
In albums.php:
$album->info = $form->edit_item->info->checked;
The field in the DB is also named 'info' and can be a bit.

Do return false; halt the script in user defined functions (PHP)

What does return true; and return false; exactly responds in user defined functions?
function valid_image($image, $target, $width, $height = 0) {
if($image["type"] !== "image/jpeg") {
alert('File must be of type image/jpeg');
return false;
}
if(file_exists($target.$image['name'])) {
alert('File Already Exists, Please Choose a Different Name for the File');
return false;
}
return true;
}
Considering the above example if the first condition if($image["type"] !== "image/jpeg" returns true. Does the return false; statement right below it stop the script from executing the below code ?
If the first statement evaluated to true, then the return false immediately under it will end the function (I should say alert() will be called first).
No it does not stop script executing, it will just return from the function (stop execution of the function). You should use it in this fashion:
function alert($message){
//display error message to user
}
if(valid_image($_FILES['image'], '/var/www/uploads', 100, 100){
//valid image
}
else{
//invalid image
}
This is not a good validation function, the 'type' property can easily be spoofed, and is determined by the visitors Operating System. A better function would at least validate the extension, and filter the name.

Categories