Render a field based on boolean without package - laravel

I am trying to make a Text field visible when a boolean is toggled true WITHOUT having to install a dependency. Does anyone know how to implement this? Is there a way to watch the request for changes to a particular field and conditionally render based on that?
Boolean::make('Blocked', 'isBlocked')
->hideFromIndex(),
Textarea::make('Reason', 'blockedReason')
->withMeta([
'extraAttributes' => [
'placeholder' => 'Make it less than 255 characters',
]
])
->rules('required', 'max:255')
->rows(3)
->hideFromIndex(),

Bad solution
One solution you could use is to update the resource with the boolean to true or false. Once the page is reloaded depending on the boolean the text field is displayed.
This, however, would invalidate Nova's dynamism, I strongly advise against it.
Probably a bad solution
Another solution could be to create an event emitter in the change of the boolean value. Once the event is fired change the readonly value of the text field.
Probably the best choice
You haven't specified the reason why you can't use a package, but if you need to use the nova-dependecy-container package.

Related

Vuetify default rules implementation

I am using Vuetify and use its default way of adding rules to input fields.
I know there is this rule:
v => !!v
This checks if the the form input isn't empty. But how can I make it in such a way that it only accepts alphabetical letters, numbers or even apply a regex to it? I can't find anything in the docs. Can someone with any experience help me out?
So I assume that you've probably sorted this now but for anyone finding this from google etc.
To add a new rule, you need to add it to your vue component, either through an import or just adding it straight to your data object. You name it as you would any other data property and it's an array of the tests like the v => !!v one you mentioned. You then add the OR operator followed by the text to show on a failed validation.
So to add a regex that only allows letters you would have this:
data () {
return {
alphaRule: [
v => /[a-zA-Z]+$/.test(v) || 'Field must only contain letters'
]
}
}
then on your form field you would have <v-text-field :rules="alphaRule"></v-text-field>
That said, I would highly recommend adding all of your rules to a Rules.js file and binding the rules globally so that you can access them anywhere, have a centralised repository for them, and it helps keeps your code DRY too.
I have just made a big ol list of rules inspired by Laravel's Validation rules and will edit my answer to include them oncce I have finished testing them.
EDIT
Here are all the rules I'm currently using in production. Hopefully they'll help someone else! You'll need to import them into your component to use them, or you can globally include them through a vue mixin.

Laravel Add Column Boolean Nullable

I'm currently building a Laravel 5.7 app where I have multiple boolean columns that indicate if some facilities are available for a building (model), eg, toilet yes/no. This works fine, but I was wondering what happens when I add more of these boolean columns later when I deploy the app.
Say I add a boolean column 'lights,' I could give it a default value of 0, but not NULL. So now all my existing buildings will say that there are no 'lights' (because the value is 0), where in reality it should be something like 'don't know' or 'undefined' Like a third state.
Should I use ENUM columns with yes/no/undefined instead? What are best practices for this scenario?
What I would do, is create separate table, with object_id, and facility_id. Now, you can have dynamic facilites table, and connect them with object. Connection will only have what it needs, so not every object "light" or something else.
You can certainly create them them as nullable()! It is a common practice, IMO.
As far as best practices go, it depends on how your application should be used. Do you want a user to notice that the state has not been selected one way or the other yet? Maybe you are displaying a prompt to configure the ones that are null. On the other hand, it may be safer to assume that the options should default to false in order to be rendered properly. If this is the case, maybe a notification can go out to your users to update this property.
This worked to me
$table->boolean('lights')->nullable();
Yes Yo are Right this could be a problem some times
But the Boolean CAN BE USED SUCH AS TRUE (OR) FALSE ie) 0 (OR) 1
where in reality it should be something like 'don't know' or 'undefined' Like a third state.
So in this Situation use Can use Enum
For Example Your table will have ups_availablity
Scenario One :
If you want to add NotAvailable by default just pass the value inside default method
$table->enum('ups_availablity', ['Available', 'NotAvailable'])->default('NotAvailable');
Scenario Two:
If you want to add Null add nullable method
$table->enum('ups_availablity', ['Available', 'NotAvailable'])->nullable();
If You have any clarification Kindly Comment Below
Hope its helps

Codeigniter form validation, custom check doesn't work if the field is not required

Gah.. I have spent way to long on this, but I believe I have found the problem.
Essentially I have a hidden field which is populated when a user clicks on an image.
It is required that the user has clicked the image but I do not want the generic form error message for a 'required' check with the CI form validation class.
As such I quickly made a image_required function in my extended form validation class, and set a rule such that this rule was applied to the hidden field.
function image_required($str)
{
$CI =& get_instance();
$CI->form_validation->set_message('image_required','Please click the image above.');
if($str != '')
{
return TRUE;
}
else
{
return FALSE;
}
}
If the hidden field was blank no error was being called.
I am led to believe now that this is because CI says this field is empty yet it is not 'required', therefore we will ignore all the other validation rules for the field. Is this correct?
If so how can i go about requiring this field be set but having a custom error message?
bangs head
Thanks
If you look at the source code (v2.1.3) for the '_execute' routine (system/libraries/Form_validation.php) you will see on line 486
// If the field is blank, but NOT required, no further tests are necessary
So you are correct, it needs to be required and then it will process your rule.
In order to fix it so you can have a non-required blank field that still processes rules, you should override the '_execute' method by creating a file called 'MY_Form_validation.php' in the application/libraries folder (I think, you might need to check exactly how you extend an existing library) and then copy the '_execute' method and alter the code to continue on a non-required but blank entry.
I do love CI, but I have to say this does not allow the flexibility required. It is perfectly reasonable to have a field that cannot be empty, but is NOT required. As in, you wouldn't enforce "user MUST enter a value", but they cannot submit a blank. I think someone got confused between EMPTY and REQUIRED.
1) REQUIRED: User MUST put a value in the field and it cannot be empty (i.e. '')
2) EMPTY: User does not HAVE to enter a value, BUT, if they do, it's cannot be empty. This not the same as REQUIRED... Looks like I'll be using a callback again.
REQUIRED incorporates two logical steps (1->Must enter a value, and 2->Cannot be empty) these two steps should be separated logically to allow either / or.
In constraint terms it would be either, REQUIRED, NOT NULL. Or NOT REQUIRED, NOT NULL.

sfWidgetFormInputCheckbox do not work properly

I have a problem with a sfWidgetFormInputCheckbox. It do not save false in the database when the checkbox is unchecked.
In my schame my field is a boolean, and i don't have bug in the value displayed.
Set the default value like this : $this->setDefault('status', false); don't work either.
My widget :
$this->widgetSchema['SUSPENSION_TEMP'] = new sfWidgetFormInputCheckbox();
$this->validatorSchema['SUSPENSION_TEMP'] = new sfValidatorBoolean(array('required' => false));
The default :
$this->setDefault('SUSPENSION_TEMP', false);
Any ideas ?
Edit:
It save 1 when the checkbox is checked. And do not change the database value when you unchecked and save. So once you checked once, value is always 1 in database (true for my code).
Edit2:
I have two clues to add. If the field is an integer in the schema.xml, both values work, bu we got a poblem on value displayed (checkbox checked qih the 0 value). Here, he field is a boolean.
The setter receive two kinds of value, 'on' when checkbox is checked and true when it's not. I tried to set default value to false in form, and to overide he setter. Both don't work.
I mean the setter work, but for an unknown reason the basic seer is called right after with a wrong value.
Edit3:
Ok, i understand part of the problem. The setter is not called, when the checkbox is unchecked. I thought it was because, m code displayed a var_dump(). It was because later i copy the old version of this object in archive purpose.
But symfony should detect that the field has been rendered and it doesn't. I don't know why. I know why in html, but obviously symfony should be able to tell himself "hey, i add a checkbox here".
Is there an elegant way o do this, or should i checked it by hand ?
The problem came from my form handling. My legacy code, did no render all fields (ex : some stamp field, updated by a behavior). It was using fromArray, then save. This update only the fiels rendered.
The traditionnal symfony form, give null to all non-rendered fiels in order to chcuk for required validation.
So you need to check by yourself and set to 0, when you are using a fromArray then save. Still, i find the form->save really dumb, because you need to send useless(in this case) or confidentials data to your client in order to use it.

Yii : form values retention for CHtml::checkBoxList on form validaton

I am using CHtml::checkBoxList for my form. For some reason I cannot use CHtml:activeCheckBoxList or CActiveForm::checkBoxList. Everything works fine only problem is that I loose checkbox values on form validation error. What could be the easiest way to fix this ?
If you're making a form, you probably want to use CActiveForm's checkboxlist, which is a form-specific wrapper of CHtml::activeCheckBoxList) instead. Something like
echo $form->checkBoxList(
$model,
'condiments',
array(
'ketchup'=>'Ketchup',
'mustard'=>'Mustard',
'relish'=>'Relish',
'onions'=>'Onions'
)
);
should give you a persistent check box list of hot dog condiments, for example.

Resources