Laravel foreach input all update fails because of form token - php

I recently found my problem what is causing my form error.
I create a form post and loop throuh them
public function update()
{
$input = Input::all();
foreach ($input as $key => $value) {
$update = Setting::find($key);
$update->value = $value;
$update->save();
}
return Redirect::back();
}
The problem is i get the following error
Creating default object from empty value
Because the token is included in the form post what Laravel automaticly renders to a form
if i stop using Laravel form open and use the html form tag it all works fine.
Is there any way to bypass this with laravel form open or should i use the html form tag?

change
$input = Input::all();
to
$input = Input::except('_token');

Make sure your $update = Setting::find($key); returns a valid object, because, that error should be triggered when $update is NULL or not defined and you are trying to use this in your code
$update->value = $value;
This warning Creating default object from empty value occurs when E_STRICT is on in the system, but this is not the real problem, instead, you are not getting the desired result, most probably Setting::find($key) is not getting the thing you asked for and creating a new Setting object instead, check your model and make sure you are passing the right value in ::find($key), key should be primary key.
Update :
Also remember, when your using
$input = Input::all();
foreach ($input as $key => $value) { ... }
In this case, $key will be the name of your input/field used in the form and it may contain hidden _token field but _token is probably not available in the database as a field/column. So, you may try to get everything except _token
$input = Input::except('_token'); // get everything without _token
to get everything without _token field (but not sure if this solves the problem or not).

Related

Using variable name in Laravel request object

I need to be able to loop through a list of laravel request variables and do something with them. I want to be able to use a variable when calling the request object so that I can run it in a loop instead of writing a line of code for every one.
For example, my text inputs may have names that look something like this
contact_main_name
contact_main_telephone
contact_main_email
contact_sub_name
contact_sub_telephone
contact_sub_email
contact_backup_name
contact_backup_telephone
contact_backup_email
In my request, I don't want to have to write
$request->contact_main_name
$request->contact_main_telephone
For each different type of contact I may have, I want to be able to loop through them like so
$contactTypes = [
'main',
'sub',
'backup',
'head'
];
foreach($contactTypes as $type){
//Start a new contact
$contact = new Contact;
$contact->type = $type;
$contact->name = $request->${"contact_".$type."_name"};
$contact->telephone = $request->${"contact_".$type."_telephone"};
$contact->email = $request->${"contact_".$type."_email"};
$contact->save();
}
How would i use a variable name when calling a laravel $request so that I can just build an array of possible types and loop through them all?
Note
I know i can edit the input fields themselves to look something like name="contact[type][name]" and then loop through them, but I cant be changing the input names, I have to do it via php in the controller itself.
As answered in comments, to do this, change the method of calling the input and use the actual input() function itself.
$contactTypes = [
'main',
'sub',
'backup',
'head'
];
foreach($contactTypes as $type){
//Start a new contact
$contact = new Contact;
$contact->type = $type;
$contact->name = $request->input("contact_".$type."_name");
$contact->telephone = $request->input("contact_".$type."_telephone");
$contact->email = $request->input("contact_".$type."_email");
$contact->save();
}
As an aside, you could also modify it slightly to use array indices matching the field names; this would allow you to add fields later by adding the appropriate field to the database and HTML without touching the code, and use array_keys() to retrieve the types submitted to allow seamless addition of types. As long as your validations are tight, this is probably the most automated way to allow future expansion...
Ex. Field Names:
contact[main][name]
contact[main][telephone]
...
contact[backup][email]
Ex. Code:
foreach(array_keys($request->input('contact')) as $type) {
$contact = Contact::create($request->input('contact.'.$type));
$contact->type = $type;
$contact->save();
}

Gravity Forms custom validation filter

I have a function that processes sales via a third-party service, processes the result and returns an array with the Status "Success" or "Invalid." This sales call is made using the gform_after_submission hook applied to the specific form.
What I need to do is store the "Success" or "Invalid" result in the array as a variable that I can later pass to a function to validate or invalidate the credit card field, using gform_validation hook.
I'm declaring the variable in a function, like so:
function foo {
...code to sell product through API...
$status = $checkoutShoppingCartRequest['Result']['Status'];
}
When I print the variable $status within the function, it is showing either Success or Invalid like it should.
Here is other function where I need to use this variable, passed to gform_validation, which fails every time regardless of Success or Invalid result:
function MBvalidate( $validation_result ) {
$form = $validation_result['form'];
if ( $status !== "Success") {
$validation_result['is_valid'] = false;
foreach( $form['fields'] as &$field ) {
if ( $field->id == '34' ) {
$field->failed_validation = true;
$field->validation_message = 'Your credit card could not be processed.';
break;
}
}
}
//Assign modified $form object back to the validation result
$validation_result['form'] = $form;
return $validation_result;
}
add_filter( 'gform_validation_47', 'MBvalidate' );
I have tried passing the variable a number of different ways, via globals and sessions, etc.
I am new to GF development so I am sure I'm missing something. I'd appreciate any direction.
The gform_after_submission action hook runs after gform_validation.
Anyway, assuming you can find a hook that runs earlier, what I would do is store a unique variable for each submitted form using the Transients API's set_transient() and get_transient() functions. For example you can create a hidden field in every form which you populate with a random ID. Use this random ID as a key to store and retrieve the Success/Invalid result.
$status here is a local variable which has never been defined before you try to use it in if-condition. So, it's always null.
Maybe you missed
$status = $validation_result['Result']['Status'];
or something like this before checking the condition.

Gii model generation not getting past first step - strange bevavior, what's causing this?

I've installed the latest version of yii2 using the advanced template. The website is working fine. For some reason the Gii generation tool is stuck and does not react as expected after clicking the preview button. Instead of showing a new form with the "Generate" button, it shows the same form unchanged without any messages as to what is happening.
Using xdebug I can see in the "actionView" method of the DefaultController that the array value $_POST['preview'] is not set, i.e. it doesn't exist in the $_POST array. I have not changed anything in the Form of the view and everything looks OK. The submit button has the name "preview" and the form is submitted but the $_POST array is not being filled with the value of the submit button. Therefore the controller does not proceed with the next steps of the generation process.
public function actionView($id)
{
$generator = $this->loadGenerator($id);
$params = ['generator' => $generator, 'id' => $id];
// ###############################################################################
// ### THIS IF STATEMENT IS NOT TRUE BECAUSE $_POST['preview'] IS NOT SET !!! ###
// ###############################################################################
if (isset($_POST['preview']) || isset($_POST['generate'])) {
// ###############################################################################
if ($generator->validate()) {
$generator->saveStickyAttributes();
$files = $generator->generate();
if (isset($_POST['generate']) && !empty($_POST['answers'])) {
$params['hasError'] = !$generator->save($files, (array) $_POST['answers'], $results);
$params['results'] = $results;
} else {
$params['files'] = $files;
$params['answers'] = isset($_POST['answers']) ? $_POST['answers'] : null;
}
}
}
return $this->render('view', $params);
}
Does anyone have an idea what could be causing this? I have a hunch that it is something quite simple that I'm overlooking, but I've never had a situation where POST variable from a Form are not being sent to the server.
False Alarm. I've found the problem. The Gii view was creating the HTML Form incorrectly.

Laravel - both input values can't be no how to validate?

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+

set_value() not working without having a validation rule

I have found that when I use set_value() in a Codeigniter 2 form, to repopulate a form field, it only returns a value if I have a validation rule set for this input.
Which is fine if I am validating a field, but not fine if it is an optional input with no validation.
So what I plan to do now as standard for every single form I process, is add this little hack to create 'empty' rules before adding any genuine validation rules;
//iterate over every posted value and create an empty rule for it.
foreach ($this->input->post() as $key => $value) {
$this->form_validation->set_rules($key);
}
Then if I have any 'real' rules I will add them afterwards so they overwrite the previous empty rule.
What I want to know is, is there another way to do this, built into the framework? It seems like a bit of an omission that these values are not available to the set_value() function until they have a rule, which makes me wonder if I have missed a configuration option?
Instead of set_value, you can use $this->input->post() to get its submitted value.
For ex:
<input name='test_name' type='text' value = '<?php echo $this->input->post("test_name");?>' />
If you're using the validation library, you have to set at least a
validation rule for your field to get set_value() form helper
function to work.
If you're interested to know the reason behind this and/or how set_*() functions work, you can refer to my answer here:
set_select() helper function is not working
In this particular instance, I suggest using $_POST[$key] to fetch the posted value of the field. Because Input::post() method doesn't do anything more than reading the value from $_POST.
Thus, you could get the value as follows:
$value = isset($_POST[$field]) ? $_POST[$field] : 'default value';
// Or echo off the errors
$value = #$_POST[$field]) ?: 'default value'; // PHP 5.3+ syntax
(Check the result).
You could also extend the CI form helper in order to to add a new helper function:
application\helpers\MY_form_helper.php
if (! function_exists('get_value')) {
function get_value($field = '', $default = '')
{
if (! isset($_POST[$field]))
{
if (count($_POST) === 0 AND $default !== '')
{
return $default;
}
return '';
}
return $_POST[$field];
}
}
Then could use the helper function as follows:
<input name="my_field" type="text" value="<?php echo get_value('my_field', 'Default Value'); ?>">

Categories