HTML input array elements with numeric keys - html

I'm trying to have some html like this:
<input name="list_item[0][name]" />
<input name="list_item[1][name]" />
<input name="list_item[2][name]" />
When I view the raw source of my document, I see these correctly. But when I do inspect element in chrome or firefox, the numbers are incrementing by one! So I see:
<input name="list_item[1][name]" />
<input name="list_item[2][name]" />
<input name="list_item[3][name]" />
And when I inspect the submitted data, the keys start at 1, not 0 which is causing my code to misbehave:
'list_item' =>
array
1 =>
array
'name' => string 'title 1' (length=7)
2 =>
array
'name' => string 'title 2' (length=7)
3 =>
array
'name' => string '' (length=0)
Why is this happening? O_o

I don't know what exactly caused that, but seems like it was some js ;-)

Related

symfony numberType does not work in [mapped=>false]

I add in a form a mapped => 'false' fields. I assign the NumberType::class but when it's rendered it's created with type="text" parameter. This is the code in the controller:
->add('debe', NumberType::class,
array( "attr" => array("pattern" => "[0-9]*", "size" => 10),
"mapped" => false ) )
The HTML code generated is this:
<input type="text" id="scg_debe" name="scg[debe]" required="required" pattern="[0-9]*" size="10" />
Thanks for any help.
So, I assume you want type="number", which is a type introduced by html5.
So essentially, you have to set the html5 option on your form field.
->add('debe', NumberType::class, [
'attr' => [
'pattern' => '[0-9]*',
'size' => 10,
],
'mapped' => false,
'html5' => true, // <-- this one
])

How do I validate a checkbox array in laravel?

I've been trying to validate all 10 checkbox array items but I cannot seem to find or understand how to use Laravels validator to validate a checkbox array.
The below code is my array HTML.
<div id="CLAagree" style="display: none;">
<div class="form-group form-check">
<div class="col-sm-2">
<div class="checkbox checkbox-green ck-button">
<input type="checkbox" class="form-check-input" oninput="this.className = ''" name="claAgree[]" id="CLAagreeCB1" onclick="saveOnboard()" >
</div>
</div>
</div>
I have tried to get all info that I can about this but I just can't seem to understand arrays in validation for some reason.
I know adding "required" made the inputs required but I have 10 so at least one will be required but I need 10 to be required. Does anyone have any options? I'm just lost at this point lol
$rules = array(
"claAgree" => "required",
"claAgree.*" => "required",
);
$validation = Validator::make($request->all(),$rules);
if($validation->fails()) return back()->with('error',$validation->messages()->first());
Here is a link to show how the array is posted
You can do like that ,check you request array name (claAgree.*.claAgree) ,
then define the input value , if same then you can use same like that
$rules = array(
"claAgree" => "required",
"claAgree.*.claAgree" => "required",
);
$validation = Validator::make($request->all(),$rules);
if($validation->fails()) return back()->with('error',$validation->messages()->first());

HTML Form: POST an array of objects

Submitting a class roster. Adding 3 students at once. Each student has first, last, age.
Question: How can we get all of the students in an array of arrays?
students[0] => Array (
["first"] => "first name for 0",
["last"] => "last name for 0",
["age"] => "age for 0"
),
students[1] => Array (
["first"] => "first name for 1",
["last"] => "last name for 1",
["age"] => "age for 1"
),
...
Details
For one student:
<input type="text" name="first">
<input type="text" name="last">
<input type="text" name="age">
We can return multiple students in separate arrays like this:
<input type="text" name="students[first][]">
<input type="text" name="students[last][]">
<input type="text" name="students[age][]">
which returns an array of firsts, lasts and ages
students["first"] = [array of first names]
students["last"] = [array of last names]
students["age"] = [array of ages]
Theoretically we can get all the info for a student by accessing the same index (say "3" for each array).
We do not want to programatically add an index in the form.
Do not want:
<input type="text" name="students[hardcoded_index][first]">
<input type="text" name="students[hardcoded_index][last]">
<input type="text" name="students[hardcoded_index][age]">
If for any reason it matters, we are using Rails for views but can use form helpers or HTML.
tl;dr: Add empty brackets ([]) after students to the input names.
Fiddling with Rack::Utils.parse_nested_query it seems you can get the payload you want like this:
<!-- first student -->
<input type="text" name="students[][first]">
<input type="text" name="students[][last]">
<input type="text" name="students[][age]">
<!-- second student -->
<input type="text" name="students[][first]">
<input type="text" name="students[][last]">
<input type="text" name="students[][age]">
Note the empty brackets ([]) after students. This tells Rack you want the students param to be an array. Subsequent params encountered (with the same name) will start a new element.
POST /myroute?students[][first]=foo&students[][last]=bar&students[][age]=21&students[][first]=baz&students[][last]=qux&students[][age]=19
Gets parsed like this:
{"students" => [
{
"first" => "foo",
"last" => "bar",
"age" => "21"
},
{
"first" => "baz",
"last" => "qux",
"age" => "19"
}
]}
Further reading: http://codefol.io/posts/How-Does-Rack-Parse-Query-Params-With-parse-nested-query
I know the question is old , but I would like to add my experiences
also for future readers.
For those of you who want to process the data in a PHP enviroment ,
#messanjah's method won't work ,
The methods for parsing data like the built-in serializeArray or serialize are not parsing it as expected either.
This is what I tried so far ...
students[name]
students[][name] - Very Strange since it was meant to automatically index the array
students[name][]
Neither of them worked, but this students[<hardcoded-index>][name] worked for PHP ,
I know that although the question is against this method , but it
will be useful for PHP users who will land here in the nearby future
as the asker needed it for Ruby On Rails.
The method you choose to hard code the indexes is upto you , you can use a cleaner method by using javascript or you can manually hard code them initially in your form element.
Cheers

cakePHP input with characters counting

I have the HTML code like this:
<label>Main Text (<span class="main_text_count">0</span><span>) Characters</span></label>
<textarea name="data[News][sentence]" id="main_text" class="form-control" required rows="8"></textarea>
And I don't know how to create a text area like this, using Form helper of cakePHP.
Simply done using the Form Helper (both for CakePHP 2 and CakePHP 3):-
echo $this->Form->textarea(
'News.sentence',
[
'id' => 'main_text',
'class' => 'form-control',
'rows' => 8,
'label' => 'Main Text (<span class="main_text_count">0</span><span>) Characters</span>'
]
);
In future make sure you've fully read Cake's documentation.

Yii2: how to stop inbuilt id generation by yii2 for each field in form

When i am adding form to view & specifying parameters as
<?= $form->field($model, 'form_name', ['options' => ['id' => 'formName', 'name' => 'formName']])->textInput(); ?>
But, when i run in the browser & check for view page source, there it shows me
<input type="text" id="submitform-form_name" class="form-control" name="SubmitForm[form_name]">
this disturbs my javascript calling for field input. How to stop yii2 from generating its own id???
You are passing options to ActiveField. If you want override id and name attributes, pass them in textInput() options like so:
<?= $form->field($model, 'form_name')->textInput(['id' => 'formName', 'name' => 'formName']) ?>
Generated html output will be:
<input type="text" name="formName" class="form-control" id="formName">
Note that after that client validation for this attribute will stop working and that attribute won't be massively assigned.