Passing form button data - laravel

I wondered, how to pass some kind of data from form button (id, or anything else) to use it in if statement, to regulate what form elements are showing.
Can i pass ID some how, can't seem to find documentation about this, and i dont want to use javaScript so much.
{{Form::button('Change form data', ['id'=>"izvele",'class' => 'btn btn-success'])}}
#if(?????????????)
#endif

You can use code like this,
if (request()->has('izvele')) {
...
}
but only after submitting the form.

Related

laravel keep form data from refresh of after button pressed

I have a form based on selects for a search with filters (picture attached below).
When i pressed the search button, the page reloads and it reset all the selects.
I try to use the old helper, but it does not work properly, it just takes the first option of the select( and it gets rid of the default option as you can see in the pictures below)
Things to know:
it is a form with a get action, it is not intended to store or edit something, only for search. So, how can i properly get the old data when refresh or after pressing the search button?
This is my first attempt and it did not work
same for this one too.
as you can see in this image, it deletes the default option, and it shows the first option
The old() function will get data by specific key from the flash data. That means you must set the data as a flash before using the old().
Source: https://github.com/laravel/framework/blob/e6c8aa0e39d8f91068ad1c299546536e9f25ef63/src/Illuminate/Http/Concerns/InteractsWithFlashData.php
Documentation: https://laravel.com/docs/5.8/requests#old-input
In the case, if you want to refresh and keep the input, I suggest using the front-end handling way: When you change the input of drop-down list, push the new query string param into the URL by javascript (How can I add or update a query string parameter?) . Until the user tries to refresh the page, the URL was kept your old value. In the blade form, you can use the function to get the param from GET request with a priority higher than the old() function, I don't actually remember the function to get param from URL, but it will seem like:
{{ Request::get('yourInput') ?? old('yourInput') ?? $realEntity->yourInput }}
Use the following in your blade files. You can check if the name exists in the url (query string). Hope this helps :)
{{ Request::get('name') }}
I think, that old() function should work just fine, but you should compare the value of option in foreach.
<select name="tipo_propiedad">
#foreach ($tipos_propiedades as $tipo_propiedad)
<option value="{{$tipo_propiedad->id}}" #if($tipo_propiedad->id == old('tipo_propiedad') selected #endif(>{{$tipo_propiedad->name}}</option>
#endforeach
</select>

How to dynamically load/render Laravel Collective form?

I'm devising a small CMS for the in-house development team, where all have experience working with laravel.
The CMS requires a small feature where the services that are listed can have a quotation form attached. Instead of creating a completely separate module to add elements & other separately, I wanted to have the system in a manner that the developer adds the laravel collective form code, which we store in the database.
While retrieving, we render the form server side.
Here's my implementation
Controller
public function show($id)
{
$data['page'] = Service::where('slug', $id)->first();
if ($data['page']) {
....
$data['form'] = $data['page']->quoteform()->first();
....
And in the view
{!! $form['html'] !!}
But this is definitely won't help, so I tried this approach of rendering the collective form
$data['form'] = View::make('website.includes.render-form',['form'=>$data['page']->quoteform()->first()]);
But I'm not sure if this should work, as I couldn't make it work.
Looking forward to a solution if at all the approach I choose is possible, if yes or can be done, would like to know more on the same.
TIA
Edit 1:
I used the following blade command Blade::compileString('string here') which helped to a certain extent.
where I'm getting the following result
You can render the HTML of a view using render() method. So you can update to this :
$data['form'] = View::make('website.includes.render-form',
['form' => $data['page']->quoteform()->first()]
)->render();
Now the $data['form'] will have rendered HTML which you can use in blade using {!! $data['html'] !!} if you are passing $data to your main view.

What's the proper way to have a default value only for CREATE view?

I would like to have a default value for my model attribute:
_form.blade.php code: {!! Form::text('attribute','Default Value') !!}
As _form.blade.php is shared for both create.blade.php and edit.blade.php, both views are always displaying the default value: Default Value.
What's the properly way to have a default value only for CREATE view?
So EDIT view should always display the value of the saved model.
It seems like a dumb question, but I'm a long time puzzling over this and I would like to know the properly way to do that.
I have some ideas, such as:
Do not use a shared file for create/edit (_form.blade.php) (I think it's not a good idea).
Set the default variable in the controller (also not good).
{!! Form::text('name', (isset($savedModel)) ? $savedModel->name : "your default value") !!}
I haven't tried a ternary inside a Form facade element, but it may work.
I believe what you want is this:
<input name="field" value="{{ old('field', isset($model->field) ? $model->field : '') }}"/>
Here's the breakdown:
If they edited/saved(first time) the form and the save failed, but was back()->withErrors(), this value will be used.
If they just invoked the "edit" view with no changes, the $model->field will be used.
If they are creating the model but have not attempted a save yet, no value will be used. ('')
This should cover all scenarios.
From my experience it's typically better not to share views between create/edit operations. You run into a lot of conditional logic that can be avoided and you get a cleaner user experience when displaying form errors and such.
As far as displaying a default value for the Create view, I use the old('attribute', 'Default Value') helper method to achieve this.

Yii - Ajax Form with validations

I am a Yii Newbie, and I have the following problem.
I have a form that is going to be like an admin - backend form. It will have loads of buttons each having their own "action" in the controller class. Now all I want to do is to validate the form elements, depending upon a "scenario" and to display appropriate error messages if all the parameters needed for the action is not filled in properly.
Can some one show me how I can do this without me having to reload the page?
[ I have found out a way, but I dont know if what I am doing is "technically" correct. I have submit buttons for all the actions that I want to perform in the form, and in the respective actions, I perform the validations and renderPartial form data back. OnSuccess of each button replaces the data of the entire "form-div" by the data that was retured from the controller. It works, but I want to know if this is the only way to achieve this.]
you should enable CActiveFom ajaxformvalidation property to true see the following example
in your controller action
you should uncomment the following lines
$this->performAjaxValidation($model);
in your view
<?php
$form = $this->beginWidget('CActiveForm', array(
'id' => 'test-form',
'enableAjaxValidation' => true,
));
?>

How to display the query result without reloading the page?

I want to show my users data from my mysql database without reloading the page.
I am using Codeigniter. I have a dropdown menu like the following, - when the page loads I want it to show all data by default. But when any user selects any name it will query to database and show results immediately without having the page reloaded.
I already have a controller, model and view to display all data from database but I don't know how to get the dropdown menu work after a person has selected a value, to fetch data from database and show immediately without reloading the page.
I have some basic knowledge of PHP but I have no idea about AJAX. Would you please kindly give me an example or idea on how to do this?
I am not expecting you to write the code for me, I am just asking for an example or a guideline. :)
Thanks in Advance.
<form>
<select name="info">
<option value="">Select a person:</option>
<option value="11080101">John</option>
<option value="11080102">Bon Jovi</option>
</select>
</form>
It sounds like with what you are aiming to achieve this isn't really a codeigniter related question but more a HTML, JQUERY question.
Using jQuery To Manipulate and Filter Data over at Net-tuts shows a jquery solution to sorting and filtering data. Their solution is based on a table but the principals are there so a modification can get it to do what you want it to do.
well i can give you the flow of how can you try it
trigger a jquery event on selecting an dropdown menu item. if you don't have any idea how to do it try reading the documentation of jquery for using selectors .
once you have the selected element , extract its value and send an ajax call to your path
like this (for jquery ajax visit jqapi it has all the documentation for jquery ajax functions and its derivatives)
$.post('/user/data/' + id , function(response) {
console.log(response)
})
or
$.post('/user/data/', 'id=' + id , function(response) {
console.log(response)
})
and now you have your data in reponse so you can do whatever you wish to do with it

Resources