-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduction form object pour les proches + ajout de la validation
- Loading branch information
1 parent
7a72d04
commit 9501ebe
Showing
6 changed files
with
76 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
class RelativeUserForm | ||
include ActiveModel::Model | ||
|
||
attr_reader :user, :ants_pre_demande_number_required | ||
|
||
DELEGATED_ATTRIBUTES = %i[first_name last_name birth_date ants_pre_demande_number].freeze | ||
|
||
delegate(*DELEGATED_ATTRIBUTES, to: :user) | ||
delegate :persisted?, to: :user # pour que form_for utilise la bonne route PUT ou POST | ||
|
||
validate :validate_user | ||
validates_with AntsPreDemandeNumberValidation, if: :ants_pre_demande_number_required | ||
|
||
def initialize(user:, **params) | ||
@user = user | ||
assign_attributes_from_params(params) | ||
end | ||
|
||
def submit(**params) | ||
assign_attributes_from_params(params) | ||
valid? && user.save | ||
end | ||
|
||
private | ||
|
||
def assign_attributes_from_params(params) | ||
@ants_pre_demande_number_required = params[:ants_pre_demande_number_required] | ||
@user.assign_attributes(params.slice(*DELEGATED_ATTRIBUTES)) | ||
end | ||
|
||
def validate_user | ||
errors.merge!(user) if user.invalid? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
.form-row | ||
.col-md-6= f.input :first_name | ||
.col-md-6= f.input :last_name | ||
- if params[:ants_pre_demande_number_required].to_b | ||
= f.input :ants_pre_demande_number, required: true, hint: t("simple_form.hints.user.ants_pre_demande_number_html"), input_html: {style: "text-transform: uppercase;"} | ||
- if f.object.ants_pre_demande_number_required | ||
= f.input :ants_pre_demande_number, label: "Numéro de pré-demande ANTS", required: true, hint: t("simple_form.hints.user.ants_pre_demande_number_html"), input_html: {style: "text-transform: uppercase;"} | ||
= hidden_field_tag :ants_pre_demande_number_required, "true" | ||
/ on veut un nom niveau racine, et pas imbriqué sous user[..] | ||
- if current_domain != Domain::RDV_MAIRIE | ||
= f.input :birth_date, as: :string, input_html: { type: "date" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters