This is my html blade code
{{Form::checkbox('remember_me', '', array('id'=>'remember_id'))}}
<label for="remember_id">Remember me</label>
This is my controller code:
echo Input::get('remember_me');exit;
The result is always empty, why please?
The checkbox is always checked when I run the page, why please?
Thanks
Please have a look on the [parameter list of the Form::checkbox() method][1].
The second parameter is your checkbox value. You manually set it to an empty string. Set it to null in order to keep the browsers default values (laravel default is 1). The third parameter is a boolean. Set it to true to check the box and false to uncheck it.
The fourth parameter is your options array where you can specify your id. So the correct method call should be:
{{Form::checkbox('remember_me', null, false, array('id'=>'remember_id'))}}
Update:
Checkboxes that are not checked, will not be included in your POST data. So the only reliable way to verify that a checkbox has been checked is to check if it is set. That can be done using isset() with regular PHP functions, or if laravel is being used, by using Input::has() which returns a boolean dependent on whether your input data contains a given key.
You did not add a value to the checkbox
{{Form::checkbox('remember_me', 'value goes here', true, array('id'=>'remember_id'))}}
The second param is the value
Normally I write the checkbox without blade and I can do with it whatever I want, like normal HTML. I don't see why you can do it the normal HTML way, because it always ends up doing the same thing you expert.
{!! Form::label('Test-2') !!} {!! Form::checkbox('ch[]', 'value-2', false); !!}
Related
I have ActiveForm checkbox:
<?= $form->field($model, 'is_necessary')->checkbox(['uncheck'=> 0]) ?>;
I want to make it checked by default and when I check, it's value become 1 and when uncheck - 0. Can I achieve this without any javascript?
I tried :
<?= $form->field($model, 'is_necessary')->checkbox(['uncheck'=> 0, 'value'=>false]) ?>;
option 'value'=>false made my checkbox checked by default but then in controller I receive NULL nor either 1 or 0.
just add in your controller or view (which is not recommended) below code
$model->is_necessary = true;
above code works fine. but you should add this code before your
$model->load(Yii::$app->request->post)
method or assigining post data to your model. Otherwise your checkbox will be checked any time;
The best approach is to override init() inside your model
public function init() {
parent::init ();
$this->is_necessary = 1;
}
and you don't need to pass the 'uncheck'=> 0, as per the DOCS
uncheck : string, the value associated with the unchecked state of the
radio button. If not set, it will take the default value 0. This
method will render a hidden input so that if the radio button is not
checked and is submitted, the value of this attribute will still be
submitted to the server via the hidden input. If you do not want any
hidden input, you should explicitly set this option as null.
So I have a form in my view:
{{Form::file('projectPicture', ['class' => 'uploadedImage', 'data-some-attribute' => ''])}}
with the attribute data-some-attribute.
And in my route I retrieve it like so:
$request->file('projectPicture');
How do I get a data-some-attribute in the route? Is it even possible?
I know I can use ajax to pass any data, but can it be avoided in this case?
Thank you!
It is not possible how you intend it to work, only because it's not how form data is working under the hood. The second argument in your sample Form::file is just decorating the rendered form element. It has no correlation with the form data that is transferred between the server and client.
For all intents and purposes form data is just a glorified set of key value pairs. If you wanted to pass some-data-attribute to your route controller, you have two options -
Add another form field, and make it empty using Form::hidden. In this case, you would just name the field some-data-attribute.
If your form is submitted through a POST method, you can tack on some-data-attribute onto the form's route and retrieve it from the request.
ie - your/route becomes your/route?some-data-attribute=whatever, and you can retrieve it later with something like $request->input('some-data-attribute').
I'm working with forms where the checkboxes are present, but the target model's fields must be in boolean type, since they are defined in my migrations as boolean. For example:
$table->boolean('is_active')->default(true);
The Model's values are filled in this way:
foreach (static::getFillableFields() as $field) {
$entry->$field = $request->input($field);
}
So I added the cast to make this field boolean:
class Entry extends Model
{
protected $casts = [
'is_active' => 'boolean',
];
But now what I see: when the form's checkbox is checked and I have '1' string in the request, it works well - '1' gives 'true' when I access $entry->is_active then. But when checkbox isn't checked, it gives the 'null' value, and - I don't know why - when the model's field is set to null, then it returns null (not 'false', as I expected).
Why is it so? This makes casts useless in my case. Can I change this behavior?
I'm not too inspired with idea of adding this (accessors/mutators) for every boolean field (but in fact this results in what I need):
public function setIsActiveAttribute($value)
{
$this->attributes['is_active'] = (bool)$value;
}
public function getIsActiveAttribute(bool $value): bool
{
return $value;
}
As #Devon mentioned, checkboxes that are not checked are not included in the request data sent to the controller. The HTML spec deems unchecked checkboxes as unsuccessful, and therefore does not submit them.
One trick that is used to get around this "limitation", however, is to add a hidden input to your HTML that has the same name as your checkbox, but contains the false value. This hidden input must come before your checkbox input.
This will allow you to continue to use your mass-assignment functionality.
So, your form should look something like:
<input type="hidden" name="is_active" value="0" />
<input type="checkbox" name="is_active" value="1" />
Now, when the form is submitted with an unchecked checkbox, the hidden input will ensure the input value exists in the request data with the false value (0).
When the form is submitted with a checked checkbox, both inputs will submit successfully with the same name, but the server side will only take the last value it sees. This is why the checkbox must come after the hidden input field, so that the last value the server sees is the successful value defined on the checkbox (1).
As a side note, this is also how the Ruby on Rails form helper handles checkboxes. From their documentation.
The HTML specification says unchecked check boxes are not successful, and thus web browsers do not send them. Unfortunately this introduces a gotcha: if an Invoice model has a paid flag, and in the form that edits a paid invoice the user unchecks its check box, no paid parameter is sent. So, any mass-assignment idiom like
#invoice.update(params[:invoice])
wouldn’t update the flag.
To prevent this the helper generates an auxiliary hidden field before the very check box. The hidden field has the same name and its attributes mimic an unchecked check box.
This way, the client either sends only the hidden field (representing the check box is unchecked), or both fields. Since the HTML specification says key/value pairs have to be sent in the same order they appear in the form, and parameters extraction gets the last occurrence of any repeated key in the query string, that works for ordinary forms.
This is a controller issue, not a model issue. HTML checkboxes will not have a value if they are unchecked, this is how they work.
Therefore, when retrieving the value from your Request object in the controller, you should set the default value as false.
Example in controller method:
$model->is_active = $request->input('is_active', false);
If you leave the second argument of input() empty, it will default to null.
Suddenly: casts don't work for Model::save() method.
laravel eloquent model casts on save?
So all that's left for me is to use accessors/mutators..
I'm using Laravel Collective for Forms and having an issue with checkbox.
Here is what I'm doing :
{!! Form::checkbox('independent',null,['class'=>'form-control', 'required' => 'required'])!!}
I've tried changing values for "null", added one more parameters as suggested by many while googling for solution but nothing seems to be working.
If anyone know the solution or having same issue, please share.
The documentation states that the third parameter is a boolean that determines if the checkbox is checked, you have an array as the third parameter. Php interprets an array as true, this is why your checkbox is always checked.
You should add true or false as the third parameter and add the options array as a fourth parameter. This can be found in the source code on GitHub.
{!! Form::checkbox('independent', null, false) !!}
I'm trying to use the checkField() for the first time, it's found the checkbox but doesn't check it.
The html:
<label>
<input class="sr-only" value="on" type="checkbox">
<span>I want an open return</span>
<label>
Look at the code:
$this->getSession()->getPage()->checkField("I want an open return");
I also try it:
$this->getSession()->getPage()->find("css", "input[type=checkbox].sr-only")->check();
Boths doesn't return any error but I can see the checkbox isn't check when I run the test.
The follow code return bool(false) as expected:
$this->getSession()->getPage()->find("css", "input[type=checkbox].sr-only")->isChecked();
checkField() might not work, because this relies on the value attribute of the element, if is true/false and you have on/off which will fail in evaluating the status of the checkbox, same thing for check().
As an alternative to check you can find the element and click():
find("css", "input[type=checkbox].sr-only")->click();
Checking if is checked with isChecked() will fail for the same reason.
You might need to do a custom validation, for example get the value of the value attribute and check it if is on or off and throw exception or return status as needed.
Things to keep in mind:
- inspect the page and check if selector is ok
- make sure the selector finds only one element, or the first element found in the list is the one you need
- make sure you wait for the element if needed
- use page objects
- the class that contains this click/check should extend the Page object and the line should look like this:
$this->find("css", "input[type=checkbox].sr-only")->click();
Finally, it's works, after change "input" to "label" using the follow code line:
$this->getSession()->getPage()->findAll("css", ".div-name
.second-div-name label")[0]->click();
My checkbox input was inside two divs, because that I have this two divs names above.
=)