adding class and id in form_dropdown - codeigniter

Is there any possible way of adding class and id attributes in form_dropdown of CodeIgniter?
I tried form_dropdown('name',$array,'class','id') and it's not changing anything, please tell me how to implement it?
Edited:
with my dropdown form like this form_dropdown('name',$array,set_value('someValue'),'id="myId"'); if I see my source from my browser it's look like this <select name="provinsi" id="provinsi_id">, but if I write like your way form_dropdown('name',$array,set_value('someValue'),'class="myClass"','id="myId"'); than like this in my browser source <select name="provinsi" class="myClass">
that was I mean
thank you

Like this:
form_dropdown('name', $array, '', 'class="my_class" id="my_id"')
The third parameter is for the value you wish to be selected and the fourth is for the additional data. Read more.

You can defined class, id or any other HTML attribute in the forth parameter of form_dropdown() function as an associative array like below:
form_dropdown('name', $array, set_value('someValue'), ['class' => 'myClass', 'id' => 'myId']);

Related

Laravel passing 2 parameters for update method

I have an EDIT button
<td>Edit</td>
And when i press it, it should edit an item based on ID and then when i update it, it should update it based on ID which works, but when I want to redirect back to index page I have to pass argument for that index method. So i added that inventory_id parameter to be passed along ID parameter but it wont recognize my inventory_id parameter in mine form.
<form method="put" action="{{action('InventoryItemController#update', $id, $inventory_id)}}">
But i get this error
Undefined variable: inventory_id
my route is like this
Route::post('inventory-items/{id}/{inventory_id}', 'InventoryItemController#update');
Error is here:
<form method="put" action="{{action('InventoryItemController#update', $id, $inventory_id)}}">
The route should look like:
route('InventoryItemController#update', ['id' => $id, 'inventory_id' => $inventory_id ])
Good luck!
I found easier way to do it, I just used one of the parameters ($id) in controller to find second parameter. Thanks for answers.

Convert Laravel validation rules to html form fields

Say I have a validation rules for some Model, for example validation for a person model will be:
'first_name' => ['required', 'string'],
'last_name' => ['required', 'string'],
'birthday' => ['before:today', 'date'],
'salary' => ['min:0', 'max:2000', numeric],
....
So if I wrote that rules, it feels wrong to write the same rules manually but for the HTML form fields like:
<input type="text" name="first_name" required />
<input type="text" name="last_name" required />
<input type="date" name="birthday" max="2016-06-09"/>
<input type="number" name="salary" min="0" max="2000"/>
So if the product owner ask me for change the rules like changing the mandatory fields, or even change the maximum salary from 2000 to 5000, I have to change it manually in the validation rules and the form itself.
So it makes me wonder, is there any automatic way to convert Laravel validation rules to the HTML form fields?
You have to parse your rules, then loop on the parsed datas for building a form. And then, I suggest you to use partial views for doing the trick.
I already did this for building automatic forms and documentations. So i wrote a Laravel package here : https://github.com/Ifnot/ValidationParser.
In the example of my package you just have to create two files :
A form blade view (contains the code for parsing the validation)
A field blade view (used for display a form item)
To have the validation rules be in one place, set the rules to variables. Then, pass the variables into the laravel validation page and in your blade template (html).
So, where you are setting the variable:
$MaxSalary = 2000;
Next, pass in your variable to the Laravel form validation rules:
'salary' => ['min:0', "max:$MaxSalary", numeric],
Then, pass it into your blade template form:
return view('form', ['MaxSalary' => $MaxSalary]);
Then, in your blade template, use the variable:
<input type="number" name="salary" min="0" max="{{ MaxSalary }}"/>
I had problems finding some one else thinking about this same idea. I must have not been using the right terms for the search. I implemented this same concept today 2023 in the following project: https://github.com/arielenter/ValidationRulesToInputAttributes
In it, I'm using laravel's Illuminate\Validation\ValidationRuleParser to explode and parse the rules, and later I used a 'translator' that convert applicable rules to input attributes.
My concern is why it seems nobody has made a laravel package that can do this on 2023. I'm not sure if I'm up to the task, but if nobody has done it I'll try. I just think it'll be very odd if nobody more capable has done it. The most difficult part I think would be to make an extend dictionary for every possible attribute that could be apply depending of the rule. I might end up leaving to the user to provide its own dictionary or something, but some cases are conditional so I'm not sure if that could work. For now I'll just keep adding translation every time I need it.

Laravel 5 routing within blade

up until this point I have essentially been using resource routing. One of my routes is for projects. If I create a project and then SHOW it, I see a URL in the form of
myUrl/projects/1
On the show page for a project, I want to be able to add a document. I have set up the relationships so a project can have one document and a document belongs to a project. I then set up the following route to handle the saving of the documents data
Route::post('projects/{id}/docOne', 'DocOneController#store');
So I add an a form in projects/show.blade.php, which opens like so
{!!
Form::model(new App\DocOne, [
'class'=>'form-horizontal',
'route' => ['docOne.store']
])
!!}
I then have my form fields and a save data button. Because of this new form within my projects show page, when I now show a project, it complains that the route for this new form is not defined.
How can I get this route to work within the projects show page?
Thanks
First of all you need to define a route name to your route, if you want to call it by his name.
So your route would be like:
Route::post('projects/{id}/docOne', [ //you need an array to set a route name
'as' => 'docOne.store', //here define the route name
'uses' => 'DocOneController#store' //here the callback
]);
Second you need to change your laravel form to use your route name and set the id
{!! Form::model(new App\DocOne, [
'route' => ['docOne.store', $project], //if you have setted the id variable like $id blade it gonna retturn it automatically only by passing the object, else, you can set $project->id
'method' => 'POST']
) !!}
EDIT:
You can't get an instance of a model on your view.
So the part:
{!! Form::model(new App\DocOne,
gonna fails every time you trye, also, the form:model needs an instance of a class that should have your vars filled with the info that the inputs should have (when you edit it).
You have two solutions:
If it's a new Doc and never before exist on your dataBase
I recomend to change your
Form::model
to:
Form::open
if it's a Doc thath already exist on your DB, like an edit, so in your controller you need to pass your existing Docas $docand remplace the: {!! Form::model(new App\DocOne, to:
{!! Form::model($doc,
and it works.
Form model was created to fill the input values with the data existing in your object instance, like when you edit someting.
So you need to have a correct instance.
Another think it's the MVC scope, a view shouldn't have acces to models, except if are passed by the controller.
Ok that's all.

How can i put HTML5 required field using form_input() function in Codeigniter framework

I am new in codeigniter.But now i am developing a project using codeigniter.
My Html code like this:
<input type="text" class="get_started_frm_reg" name="first_name" required />
Now i want to convert it through function form_input() function.I wrote my code like that
$first_name=array(
"name"=>"first_name",
"class"=>"get_started_frm_reg",
"type"=>"text"
);
But i don't understand how can i put required field.Please help me.
$first_name=array("name"=>"first_name",
"class"=>"get_started_frm_reg",
"type"=>"text",
"required"=>"required");
This is working
I would recommend the following:
form_input('first_name', $value, 'class="get_started_frm_reg" required');
// if you don't want to pass a variable for value, pass 'null'
form_input('first_name', null, 'class="get_started_frm_reg" required');
The above will output exactly how you were asking in your question.
I like using this method better then passing an array to form_input because you have better control over boolean input values, like required. Also, you don't need to pass text="type" since it is the default on form_input.
The best is to use it like this:
<?=form_input(['name'=>'first_name', 'class'=>'get_started_frm_reg'],'','required');?>
Produces:
<input type="text" class="get_started_frm_reg" name="first_name" required />

How to set the value for the textarea in Codeigniter?

echo form_textarea('general4', set_value('general4'), 'class="general"');
the set_value function doesn't seem to work with the textarea so I tried this:
<textarea name='general4' class="general"><?=set_value('general4')?></textarea>
But still not working, any ideas?
to use form_textarea() in CI you pass parameters rows and coloumns as below
$data = array(
'name' => 'txt_area',
'id' => 'txt_area',
'value' => 'johndoe',
'rows' => '5',
'cols' => '10',
'style' => 'width:50%',
);
echo form_textarea($data);
for more details refer CI user guide https://www.codeigniter.com/user_guide/helpers/form_helper.html#form_textarea
What you did is set the name of the textarea field to: 'general4'. I think what you meant to do is return an actual string to your textarea to pre-populate it with data from a post request or a MySQL database or something like that. There are a number of ways to achieve this.
Method 1:
Set a second parameter in the set_value() function eg:
<textarea name='general4' class="general"><?=set_value('general4', $foo)?></textarea>
Method 2:
You could always use the built in form_textarea() function. Docs found here
Examples:
Generic
<?=form_textarea('name', 'value', 'attributs')?>
Case
<?=form_textarea('general4', $general4, "class = 'foo'")?>
From the CI Documentation:
set_value()
Permits you to set the value of an input form or textarea. You must supply the field name via the first parameter of the function. The second (optional) parameter allows you to set a default value for the form.
<input type="text" name="quantity" value="<?php echo set_value('quantity', '0'); ?>" size="50" />
The problem was that I didn't need the textfield to be required. So I didn't set any rules in the action url. So I added this:
$this->form_validation->set_rules('general4', 'general question' , 'trim|xss_clean');
And it worked fine!

Resources