Is there any way to pass the validation of NOT REQUIRED v-text-field rules? - forms

I tried to validate v-text-field for entering number only but it is not required but the rules deny to pass the validation.
I used v-form, v-text-field and rules of v-text-field.
<template>
<v-form ref="form">
<v-text-field
v-model="name"
:label="label"
:rules="rules"
#blur="changeValue"
clearable
></v-text-field>
<v-btn #click="send">submit</v-btn>
</v-form>
</template>
<script>
export default {
data() {
return {
name: "",
rules: [
v =>
v.length <= 50 || "maximum 50 characters",
v =>
(v.length > 0 && /^[0-9]+$/.test(v)) || "numbers only"
]
};
},
methods: {
changeValue(event) {
this.$emit("changeValue", event.target.value);
},
send() {
const valid = this.$refs["form"].validate(); // doesn't pass
if (valid) {
this.$store.dispatch("xxx", {
...
});
}
}
}
};
</script>
When the submit button was clicked, error message of v-text-field is shown and valid is false.
Clicked X(the clear icon), The error message is also shown on console:
"TypeError: Cannot read property 'length' of null"

A bit late, but i have solved it with this function:
rules: [
v => {
if (v) return v.length <= 50 || 'maximum 50 characters';
else return true;
},
],

i use this way one for email and one for name
nameRules: [
v => v.length <= 10 || 'Name must be less than 10 characters',
],
emailRules: [
v =>( v.length ===0 || /.+#.+/.test(v) ) || 'E-mail must be valid',
],

Related

Material Ui 5 Autocomplete does not return filtered options

I am using material ui V5,
Due to the default filtering in Autocomplete does not give proper result array, l have written my own filterOptions function.
const filterOpt = (options, state) => {
let result = options.filter(option => option.name.includes(state.inputValue))
return result }
The result returning from the function is exactly what l want. But still, l can see the undesired options.
Here is my Autocomplete component :
<StyledAutocomplete
disabled={disabled}
id="field1"
getOptionLabel={(option) => option.name || ""}
isOptionEqualToValue={(option, value) => option.id === value.id}
value={values[prop] || ""}
noOptionsText={"No options found"}
options={data}
style={{ width: "100%" }}
PopperComponent={PopperMy}
PaperComponent={CustomPaper}
onChange={(event, newValue) =>
setValues({ ...values, [prop]: newValue })
}
filterOptions={(options, state) => filterOpt(options, state)}
renderInput={(params) => {
const inputProps = params.inputProps;
inputProps.autoComplete = "new-password";
return (
<StyledTextField
{...params}
inputProps={{
...params.inputProps,
autoComplete: "new-password",
}}
name="field1"
id="field1"
autoComplete="off"
type="text"
label=""
variant="outlined"
error={error && !values[prop]}
helperText={error && errorStatus ? errorTexts[prop] : ""}
/>
);
}}
/>
Here are the options that l see after filtering
Here is the results array returned from the function:
Is there any solution to show the exact filtered array to users?
This is because of my data array which includes some items with the same key.

Open param doesn't work (InlineDatePicker) - material ui

import { MuiPickersUtilsProvider, InlineDatePicker } from 'material-ui-pickers'
<InlineDatePicker
shouldDisableDate={day => isSameDay(day, new Date())}
onlyCalendar
open={isOpen}
minDate={addYears(new Date(), -18)}
value={begin}
renderDay={renderDay}
onClose={() => {
onChange([begin, end].sort())
if (onClose) onClose()
}}
onClear={() => {
setBegin(undefined)
setEnd(undefined)
setHover(undefined)
onChange([])
}}
ref={picker}
labelFunc={(date, invalid) =>
labelFunc
? labelFunc([begin, end].sort(), invalid)
: date && begin && end
? `${formatDate(begin)} - ${formatDate(end)}`
: emptyLabel || ''
}
{...props}
/>
I try to open date picker by using open parameter , but it doesn't work, what do I do wrong ? Version is : "material-ui-pickers": "^2.2.4". Also I pass this param to prevent showing input text field TextFieldComponent={() => null}
const picker = useRef()
const onClick = e => {
return picker.current.open(e)
}
.....
<InlinePicker>
ref={picker}
</InlinePicker /
This will be working

Codeigniter - Form validation not works

I already try to googling but not solve, my code below is to check duplicate before insert, when "tagnumber" field already exists will go to specific page and not inserted to table, it works but the problem is although not insert to the table but it wont go to specific page I want.
below is my problem on conditional statements:
if data exists on form validation will execute not insert the data.
if data not exists form validation will execute insert data but go to wrong page (same page with exists data page).
my controoler :
function tambahSubmit()
{
$tagnumber = $this->input->post("tagnumber");
$this->myigniter_model->addData($tagnumber);
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
// field name, error message, validation rules
$this->form_validation->set_rules('tagnumber', 'tagnumber', 'trim|required|is_unique[inventorytag.tagnumber]');
$this->form_validation->set_rules('date', 'date', 'trim|required');
$this->form_validation->set_rules('employee', 'employee', 'trim|required');
$this->form_validation->set_rules('semnumber', 'semnumber', 'trim|required');
$this->form_validation->set_rules('quantity', 'quantity', 'required');
$this->form_validation->set_rules('area', 'area', 'trim|required');
if($this->form_validation->run() == false)
{
$this->load->view('YearEndStock/tampilan_input_gagal');
}
else
{
$this->myigniter_model->addData($tagnumber);
$this->load->view('YearEndStock/tampilan_input_sukses');
}
}
my model :
function addData($tagnumber)
{
// Added $this->db->escape() and limit 1 for Performance
$query = $this->db->query("SELECT tagnumber FROM inventorytag WHERE tagnumber = ".$this->db->escape($tagnumber)." limit 1");
$data = array(
'tagnumber' => $this->input->post('tagnumber'),
'date'=> date('Y-m-d H:i:s'),
'employee' => $this->input->post('employee'),
'semnumber' => $this->input->post('semnumber'),
'quantity' => $this->input->post('quantity'),
'area' => $this->input->post('area')
);
return $query->num_rows() == 0 ? $this->db->insert('inventorytag', $data) : false;
}
Then you just need to separate your true statement. Remove your is_unique validation.
if($this->form_validation->run() == false)
{
SHOW ERRORS
$this->load->view('YearEndStock/tampilan_input_gagal');
}
else
{
$row = $this->db->get_where('inventorytag', array('tagnumber' => $tagnumber))->row();
if (empty($row)) {
//insert
$this->myigniter_model->addData($tagnumber);
} else {
//row exists
}
$this->load->view('YearEndStock/tampilan_input_sukses');
}

Autocomplete - How can I set a default value?

Does anyone know how to add a default value to the Autocomplete component?
The component have a dataSource, and I'd like to load the page with a specific item already selected(e.g. fill the text field with the selected item's text and it's value already set).
Does anyone knows how? Big thanks for any help <3
You can achieve this by setting the appropriate state in componentDidMount, for example:
componentDidMount() {
// load your items into your autocomplete
// set your default selected item
this.setState({ allItems: [itemYouWantToSet], selectedItem: item.name, selectedItemId: item.id }
}
render() {
return (
<Autocomplete
value={this.state.selectedItem}
items={this.state.allItems}
getItemValue={(item) => item.name}
onSelect={(value, item) => {
this.setState({ selectedItem: value, selectedItemId: value, allItems: [item] });
}}
/>
)
}
Then your item is correctly selected from the list of available options when it loads.
I tried all the above solutions and nothing worked. Perhaps the API has changed since then.
I finally figured out a solution. It's not so elegant, but in principle it makes sense. In my case the options are objects. I just had to set the "value" prop using the exact item from my options array. This way componentDidMount and getOptionSelected aren't needed.
Autocomplete is wrapped inside another component in our case. This is the main code:
class CTAutoComplete extends React.PureComponent {
getSelectedItem(){
const item = this.props.options.find((opt)=>{
if (opt.value == this.props.selectedValue)
return opt;
})
return item || {};
}
render() {
return (
<Autocomplete
id={this.props.id}
className={this.props.className}
style={{ width: '100%' }}
options={this.props.options}
getOptionLabel={this.props.getOptionLabel}
renderInput={params => (
<TextField {...params} label={this.props.label} variant="outlined" />
)}
onChange={this.props.onChange}
value={this.getSelectedItem()}
/>
);
}
}
IMPORTANT: When setting "value", you have to make sure to put the null case " || {} ", otherwise React complains you are changing from an uncontrolled to controlled component.
you can provide the defaultValue prop for AutoComplete.
<Autocomplete
multiple
id="tags-outlined"
options={this.state.categories}
getOptionLabel={(option) => option.category_name}
onChange={this.handleAutocomplete}
defaultValue={'yourDefaultStringValue'} //put your default value here. It should be an object of the categories array.
filterSelectedOptions
renderInput={(params) => (
<TextField
fullWidth
{...params}
variant="outlined"
label="Add Categories"
placeholder="Category"
required
/>
}
/>
This approach works for me (using hooks):
First of all define the options you need in a variable:
const genderOptions = [{ label: 'M' }, { label: 'V' }];
Second you can define a hook to store the selected value (for example store it in session storage for when the page refreshes, or use useState directly):
const age = useSessionStorage('age', '');
Next you can define your Autocomplete as follows (notice the default values in value and getOptionLabel, if you omit those you'll get those controlled to uncontrolled warnings):
<Autocomplete
id="id"
options={ageOptions}
getOptionLabel={option => option.label || ""}
value={ageOptions.find(v => v.label === age[0]) || {}} // since we have objects in our options array, this needs to be a object as well
onInputChange={(_, v) => age[1](v)}
renderInput={params => (
<TextField {...params} label="Leeftijd" variant="outlined" />
)}
/>
It is tricky specially in case of you are using along with filter option which load API on every filter. I was able to load initial value by setting up within state and onInputChange option.
Below is code that work for me or click below link for full working demo.
https://codesandbox.io/s/smoosh-brook-xgpkq?fontsize=14&hidenavigation=1&theme=dark
import React, { useState, useEffect } from "react";
import TextField from "#material-ui/core/TextField";
import Typography from "#material-ui/core/Typography";
import Autocomplete from "#material-ui/lab/Autocomplete";
export default function CreateEditStrategy({ match }) {
const [user, setUser] = useState({
_id: "32778",
name: "Magic User's Club!"
});
const [filter, setFilter] = useState("");
const [users, setUsers] = useState([]);
const [openAutoComplete, setOpenAutoComplete] = React.useState(false);
useEffect(() => {
(async () => {
//Will not filter anything for testing purpose
const response = await fetch(
`https://api.tvmaze.com/search/shows?q=${filter}`
);
const shows = await response.json();
setUsers(
shows.map((a, i) => {
return { _id: a.show.id, name: a.show.name };
})
);
})();
}, [filter]);
return (
<div>
<Typography variant="h6">Autocomplete</Typography>
<Autocomplete
open={openAutoComplete}
onOpen={() => setOpenAutoComplete(true)}
value={user}
inputValue={filter}
onClose={() => setOpenAutoComplete(false)}
onChange={(event, user) => {
if (user) setUser(user);
else setUser({ _id: "", name: "" });
}}
onInputChange={(event, newInputValue) => setFilter(newInputValue)}
getOptionSelected={(option, value) => option.name === value.name}
getOptionLabel={(option) => option.name}
options={users}
renderInput={(params) => (
<TextField
{...params}
label="Asynchronous"
variant="outlined"
InputProps={{
...params.InputProps
}}
/>
)}
/>
</div>
);
}
Call your component like this
<SelectCountryAutosuggest searchText="My Default Value" />
Make sure you apply the default value to state on class load
class SelectCountryAutosuggest extends React.Component {
state = {
value: this.props.searchText, //apply default value instead of ''
suggestions: [],
};
...
}
The api docs suggest the best approach in the current version (June 2022) is to use value and isOptionEqualToValue.
So for example, if I have a list of users and am choosing which user this thing is assigned to:
const [assignedTo, setAssignedTo] = useState(initialOption);
return (<Autocomplete
options={users.map((i) => ({
label: i.name,
value: i._id,
}))}
isOptionEqualToValue={(o, v) => o.value === v.id}
value={assignedTo}
onChange={(evt, val) => setAssignedTo(val)}
renderInput={(params) => (
<TextField {...params} label="Assigned To" />
)}
/>);
We can setup initial value through value property of Autocomplete component
<Autocomplete
fullWidth={true}
label={'Location'}
margin={'noraml'}
multiple={false}
name={'location'}
getOptionSelected={useCallback((option, value) => option.value === value.value)}
value={formValues.location === '' ? {label: ''} : {label: formValues.location}}
options={location}
ref={locationRef}
onChange={useCallback((e, v) => handleInputChange(e, v))}
/>
You can use the searchText property.
<AutoComplete searchText="example" ... />
Try this...
componentWillReceiveProps(nextProps) {
let value = nextProps.value
if (value) {
this.setState({
value: value
})
}
}
onUpdateInput worked for me - for anyone looking through all this as I was
Have you tried setting the searchText prop dynamically? You can pass the value you want to set to the Autocomplete component as the searchText prop. Something like,
<Autocomplete
searchText={this.state.input.name} // usually value={this.state.input.name}
/>
By default, it'll have the initial value set into the TextField of the Autocomplete component but when the user makes any modifications, it calls up the Autocomplete options depending on the dataSource prop.

Yii2 requirement selection one or more checkboxes FormBuilder

I have a registration form created in the "Yii2 krajee FormBuilder". It contains two checkboxes. At least one of them must be selected. Validation must be carried out on the client side, in order not to overload the page. Everything would be easy if the Yii2 contain the option to assign the rules required and whenClient to checkboxes - unfortunately that is not possible. For other fields(textboxes, selectboxes, you can for example use this code:
Model:
$public $module1;
$public $module2;
'module1'=>[
'type'=>Form::INPUT_CHECKBOX,
'name'=>'ak1',
'label'=>'<b>'.Yii::t('app','register.module1').'</b>' ],
'module2'=>[
'type'=>Form::INPUT_CHECKBOX,
'name'=>'ak2',
'label'=>'<b>'.Yii::t('app','register.module2').'</b>' ]
'textbox1'=>[
'type'=>Form::INPUT_TEXTBOX,
'name'=>'tx1',
'label'=>'<b>'.Yii::t('app','register.tx1').'</b>' ]
[...]
[[ 'textbox1', 'texbox2', ],'required','whenClient'=> "function (attribute, value) { return $('#registerform-textbox1').is(':checked') == false && $('#registerform-textbox2').is(':checked') == false;}"
],
It works ..but for texboxes. Checbox can not be assigned to required
I used this but in this case, the page is reloaded
['module1',function ($attribute, $params) {
if ($this->module1 == 0 && $this->module2 == 0) {
$this->addError($attribute, 'you have to select at least one option');
}
}],
Typically checkboxes validation is carried out by this
public function rules ()
     {
         array ['checboxname', 'compare', 'compareValue' => true,
               'message' => 'You must agree to the terms and conditions'],
         ...
     }
but in this case you can not combine rule compare with the rule whenClient - which is responsible for the function-specifed validation on the client side.
How can I solve this problem?
I'm not pretty sure what you are trying, but I think that you would do:
['checkbox1', 'required', 'when' => function($model) {
return $model->checkbox2 == false;
}
],
['checkbox2', 'required', 'when' => function($model) {
return $model->checkbox1 == false;
}
],