How to set the value for the textarea in Codeigniter? - 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!

Related

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 Blade Form, is it possible to create a textbox input if a user selects the 'other' option?

This may not be possible with blade, but I was wondering how to generate a textbox input from an 'other' option in a select dropdown. Is this possible?
{{ Form::select('showType', array(
'Theater' => 'Theater',
'Club' => 'Club',
'Festival' => 'Festival',
'Arena' => 'Arena',
'Closed Show' => 'Closed Show',
'College Show' => 'College Show'
'Other' => 'some kind of text input appears instead'
)}}
You'll need to use javascript to do that, Blade can't help much. But you can create, in Blade, your form input and set a hidden class to it and, when your user select the 'other' option you just have to remove that class.
You need to use JavaScript to do this because it happens on the client side. PHP and Laravel and Blade all happens on the server. An simple jQuery example:
$('select').on('change', function(){
$('body').append("<input type='text' value='whatever'/>");
});

How do I repopulate fields BEFORE validation is passed?

The fields I am making required to fill out, should repopulate the correctly filled out fields, while NOT submitting the form and posing errors for the incorrectly filled out fields. What is the best way to do that?
Please note that with this code, I am repopulating the fields as they should be upon submitting the form correctly and they are all displaying when page is reloaded.
<div class="dropdown_structure">
<?php
if($user['location'] == "empty")
{
echo country_dropdown('location');
}
else
{
echo country_dropdown('location',$user['location']);
}
?>
</div>
Also please note that I've tried inserting the value in the input fields.
$data = array( 'name' => 'location', 'value' => $this->input->post('location'));
echo relation_dropdown('location');
Thanks in advance
Hi if you are using the following country dropdown helper you can set value validation in following way
Country dropdown helper
country_dropdown('location',array(),set_value('location'))
even in your second dropdown use set_value('field_name') will work. if validation is failed your selected value will be remain.

adding class and id in form_dropdown

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']);

Cakephp 1.3, Weird behavior on firefox when using $this->Html->link

Greetings,
I am getting a very weird and unpredictable result in firefox when using the following syntax:
$this->Html->link($this->Html->div('p-cpt',$project['Project']['name']) . $this->Html->div('p-img',$this->Html->image('/img/projects/'.$project['Project']['slug'].'/project.thumb.jpg', array('alt'=>$project['Project']['name'],'width'=>100,'height'=>380))),array('controller' => 'projects', 'action' => 'view', $project['Project']['slug']),array('title' => $project['Project']['name'], 'escape' => false),false);
OK I know it is big but bear with me.
The point is to get the following output:
<a href="x" title="x">
<div class="p-ctp">Name</div>
<div class="p-img"><img src="z width="y" height="a" alt="d" /></div>
</a>
I'm not sure if this validates correctly both on cakephp and html but it works everywhere else apart from firefox.
You can actually see the result here: http://www.gnomonconstructions.com/projects/browser
To reproduce the result use the form with different categories and press search. At some point it will happen!!
Although most of the time it renders the way it should, sometimes it produces an invalid output like that:
<div class="p-cpt">
name
</div>
<div class="p-img">
<img src="x" width="x" height="x" alt="x" />
</div>
Looks like it repeats the link inside each element.
To be honest the only reason I used this syntax was because cakephp encourages it.
Any help will be much appreciated :)
I am guessing that the name of some projects is null. According to the documentation, if you pass null as the second argument to the div() method, it will not generate the closing tag (and the resulting markup will be invalid).
The example of invalid markup that you pasted above appear to have come from Firebug rather than Page Source. Use Page Source to view the actual markup sent to the browser. The anchor tag is not repeated.
I rewrote your code to better see what happens. Copy it into one of your views, change 'My Project' to null, and notice how it will affect the $name_div variable:
<div class="p-cpt">My Project</div> will turn into <div class="p-cpt">.
<?php
$project['Project']['name'] = 'My Project';
$project['Project']['slug'] = 'my-project';
$image = $this->Html->image(
'/img/projects/' . $project['Project']['slug'] . '/project.thumb.jpg',
array(
'alt' => $project['Project']['name'],
'width' => 100,
'height' => 380
)
);
$name_div = $this->Html->div('p-cpt', $project['Project']['name']);
$image_div = $this->Html->div('p-img', $image);
$link = $this->Html->link(
$name_div . $image_div,
array(
'controller' => 'projects',
'action' => 'view',
$project['Project']['slug']
),
array(
'title' => $project['Project']['name'],
'escape' => false
)
);
var_dump($image);
echo 'Notice what happens below if project name is null.';
var_dump($name_div);
var_dump($image_div);
var_dump($link);
echo $link;

Resources