Wrong type being displayed - laravel

I am just trying to figure out why this is happening. I have a form with the following input
{!! Form::input('number', 'numTemplates', '1', ['id' => 'numTemplates', 'class' => 'form-control', 'min' => '1']) !!}
Now I presume number would be treated as an integer? Within my Controller I am doing the following
$templates = $request->input('numTemplates');
dd(gettype($templates));
The output of this is "string". So why would a number input be displayed as a string when using gettype?
Thanks

It's a string because it's still a value of HTML form input. Just do type conversion, something like intval($request->numTemplates) or (int)$request->numTemplates

Related

Default value for laravel collective select

I have a Laravel collective select as follows:
{!! Form::select('phone', $numbers, $title, ['class' => 'phone-select2 col4', 'id' => 'phone']) !!}
I want to pass default value for dropdown as separate variable from $numbers. So, have passed it as $title but it is didn't work due to it is not in the $numbers. So, how can I pass default value for it?
What you're looking for is a placeholder.
https://laravelcollective.com/docs/6.x/html#drop-down-lists
According to the documentation, you can set the default value to null and provide placeholder attribute as part of the fourth argument:
['class' => 'phone-select2 col4', 'id' => 'phone', 'placeholder' => 'Pick an option']

After an ajax need to load a field value and create a select_from_array with the range value

Iam trying and not finding a way to load the value from a field and generate a select_from_array based on its range.
For example:
I have 2 select box
Brand -> loads -> Model (using backpack field types, and its working good)
`'type' => 'select2_from_ajax',
'name' => 'camera_model_id',
'entity' => 'camera_model',
'attribute' => 'name',
'data_source' => url('camera-brands'),
'placeholder' => 'Selecione o Modelo',
'minimum_input_length' => 0,
'dependencies' => ['camera_brand_id'],`
But, after the user selects this last selectBox, I need that another field was modified
`'name' => 'channel',
'label' => "Canal da Câmera",
'type' => 'select2_from_array',
'options' => ['' => '',
'01' => '01',
'02' => '02', ...`
So, the options could be filled with the maximum of the field I registered in the Model field database.
Is it possible? or maybe another approach to achieve the solution?
Thanks in advance!
To have an input that depends on the value of another input, you can make both your fields select2_from_ajax.
That way:
you will have the value of all inputs in the controller (the controller that returns the ajax results; then you can return a filtered set of results depending on how the form is filled so far - CategoryController::index() in the documentation example);
you can use the "dependencies" attribute on the selec2_from_ajax fields, so that when one field is reset, both are;
I hope the answer helps someone. Cheers!

Laravel/Eloquent: Issues with date mutation and form model binding

I have a column with datatype of date which is date mutated. I date mutated it so that Laravel will convert it to a Carbon instance and I can use it easily at other places, where I need to convert it into Carbon instance. I am using Model binding in the edit form. As the field is date mutated, on the edit form it appears as "2015-07-29 00:00:00". I need it to be in this format instead: "2015-07-29".
I can't use accessor, as I need it as a Carbon instance at many other places.
I can't explicitly pass value after converting, as I am using the input inside a form partial and I also use it for create.
My workaround is the following:
I am sending a flag while including the view partial in the edit page and using it a condition to have two different code for create and edit.
#if (isset($edit))
{!! Form::text('eta', $order->eta->format('Y-m-d'), ['class' => 'form-control', 'required']) !!}
#else
{!! Form::text('eta', null, ['class' => 'form-control', 'required']) !!}
#endif
Is there a better way?
I think you can avoid from #if and $edit as,
{!! Form::text('eta', ( $order->eta ? $order->eta->format('Y-m-d') : null ) , ['class' => 'form-control', 'required']) !!}

HTML in Laravel translated string

I am translating the following string:
<p> {{{ trans('myapp.signup_instructions', [ 'email' => '<strong>'.$invitation->email .'</strong>']) }}}</p>
However on our website we can still see the "<strong>user#domain.com</strong>" as text. We were aiming to convert the just the e-mail parameter in the string into bold text
How can I achieve that?
In Laravel 5 you should use {!! !!} to output variable without escaping:
{!! trans('myapp.signup_instructions', ['email' => '<strong>'.$invitation->email .'</strong>']) !!}
Read more: http://laravel.com/docs/master/upgrade#upgrade-5.0 (Blade Tag Changes section)
PS. This does not related to AngularJS

Multiple forms with field name arrays fail vaildation

I have multiple forms in a tabbed layout and post to the same resource controller. These fields have the same field names with a few as arrays. Use case is password storage where depending on the type of password depends on the fields needed.
Input field
<?= Form::text('name', Input::old('name'), array('class' => ($errors->has('name')) ? 'invalid' : '', 'placeholder' => 'Application Name')) ?>
<?= Form::text("data['username']", Input::old('data["username"]'), array('class' => ($errors->has('data.username')) ? 'invalid' : '', 'placeholder' => 'Username')) ?>
<?= Form::text("data['password']", Input::old('data["password"]'), array('class' => ($errors->has('data.password')) ? 'invalid' : '', 'placeholder' => 'Password')) ?>
Validation rules
protected $rules = array(
'name' => 'required',
'data.username' => 'required',
'data.password' => 'required'
);
I have more data fields but this illustrates the use case.
I get errors properly with posting with empty fields and the old input is populated but I also get errors for filled fields. My post data shows populated fields.
Tested with just one form (could have done that the first time) and still had the error. My issue here was with the how the data['username'] was used.
Should be data[username] without the quotes. While post data and everything works with the quotes, the validator was not liking it.

Resources